(function () {

    var _self = this,
        _data = null,
        _userDetails = null,
        _shell_wrapper = null,

        _shell_wrapper = $('#shell_wrapper');

    let _scoreArr = [0]
    let _score = 0;

    let _mcms_mcss = null;
    let cQueNo = 1;
    let tQuesNo = 1;
    let _cTime = 0.1;

    let _pageData = {
        'aD': {},
        'pr': "",
        'sr': 0,
        'sts': 0
    };

    let _btn_next = $(".btn_next");
    let _btn_prev = $(".btn_prev");


    _shell_wrapper.off("initPage").on("initPage", function (e) {
        _shell_wrapper.off("initPage");

        _shell_wrapper.off("takePageDetails").on("takePageDetails", function (e, pageData) {
            e.stopPropagation();
            _pageData = pageData || _pageData;
            updateDom();
        });

        _shell_wrapper.off("sentUserDetails").on("sentUserDetails", function (e, userData) {
            e.stopPropagation();
            _userDetails = userData;
            _data = getData(_userDetails)[0];
            _shell_wrapper.trigger('getPageDetails', _data.userDataRef.currentPageNo);
        });
        _shell_wrapper.trigger('getUserDetails');
    });


    var updateDom = function () {
        _shell_wrapper.off("componentDidMount").on("componentDidMount", function (e) {
            e.stopPropagation();
            pageInit();
        });

        Utils.loadTemplate('#templateHolder', 'simple_start', function () {
            var _simple_start = new Simple_Start();
            _simple_start.init(getData(_userDetails));
        });

    }

    var pageInit = function () {
        let _initData = {
            'callback': function () {
                clearTimeout(_timerInstance);
                addActivity();
            }
        }
        enabledDisable(_btn_next, false);
        enabledDisable(_btn_prev, false);

        _shell_wrapper.trigger('pageLoaded', _initData);

    }

    var addActivity = function () {
        _shell_wrapper.off("clearAlertTimerInstance").on("clearAlertTimerInstance", function (e) {
            clearTimeout(_timerInstance);
            _shell_wrapper.off("clearAlertTimerInstance");
        });
        clearTimeout(_timerInstance);

        addActivity_1();


    }

    var addAudio = function (aNo) {
        updateAudio(aNo, function () {

        })
        addEvents();
    }
    // activity 1
    var addActivity_1 = function () {
        cQueNo = 1;
        let _aHolder = $("#activityHolder");
        _aHolder.empty();
        _aHolder.addClass("disabled");

        Utils.loadTemplate('#activityHolder', "mcms_mcss_13", function () {
            _mcms_mcss = new initMCMS_MCSS_13();
            showActivity(0.1, cQueNo);
        });
    }

    var loadQuestion = function () {
        if (_mcms_mcss) {
            let _questionData = _data['question_' + cQueNo];
            _mcms_mcss.init(_questionData, function () { });
        }
        addAudio(cQueNo - 1);
        // $("#questionID").html("Question " + cQueNo);
        // pauseForActivity();
    }

    var pauseForActivity = function () {
        let _aHolder = $("#activityHolder");
        _aHolder.removeClass('d-hide')
        _aHolder.removeClass("disabled");
        _shell_wrapper.trigger('start_MCMS_MCSS_13');
        //_shell_wrapper.trigger('PauseForActivity');
    }

    var isLoadNext = 1;

    var _result;
    var addEvents = function () {
        _shell_wrapper.off("activityResult_3").on("activityResult_3", function (e, data) {
            _result = data.finalresponse;
            //  let _aD = _pageData['aD'];
            //  _aD[data.qNo] = data;
            // isLoadNext = 1;
            // if ((!data.result) && (data.qNo == 1)) {
            //     isLoadNext = 0;
            // }
            // _shell_wrapper.trigger("setActivityData", _pageData);
        });

        _shell_wrapper.off("closeActivity").on("closeActivity", function () {

            if ((cQueNo >= tQuesNo)) {
                lastStep();
            } else {
                let _aHolder = $("#activityHolder");
                _aHolder.addClass('d-hide');
                cQueNo++;

                showActivity(_cTime, cQueNo);
            }

        });
        pauseForActivity();

    }

    var loadNextQuestion = function (cTime, qNo) {
        //   console.log("loadNextQuestion")
        _cTime = cTime;
        cQueNo = qNo;

        if (cQueNo <= tQuesNo) {
            loadQuestion();
        } else {
            // loadResult();
            lastStep();
        }
    }

    var loadResult = function () {
        //    console.log("loadResult - ", _pageData);
        let _aD = _pageData['aD'];
        _score = 0;
        let aNo = 1;
        $.each(_aD, function (key, value) {
            if (_aD[key]['result'] == 1) {
                _score += parseInt(_scoreArr[_aD[key].attemptNo - 1])
            } else {
                aNo = 0
            };
        });

        _pageData['result'] = aNo;
        _pageData['sr'] = _score;
        _pageData['sts'] = 2;
        _shell_wrapper.trigger("setActivityData", _pageData);

        lastStep();
    }

    var lastStep = function () {

        /*let _data = {
            mNo: 6,
            pNo: 11
        }*/

        if (_result == "n") {
            var _data = {
                mNo: 1,
                pNo: 10
            }

            // page complete when question attempt wrong     
            var mytitle = `<a id=pTile_6_4 class=page-link pTile tileShow cTile active completed data-ptile=m01_l05_p03 data-bgclass=undefined href=# data-ptileno=24 aria-hidden=true disabled=disabled><span>24</span></a>`;
            _shell_wrapper.trigger("custmsetPageStatus", [6, 4, mytitle, 2]);

            var mytitle = `<a id=pTile_6_11 class=page-link pTile tileShow cTile active completed data-ptile=m01_l05_p03_05 data-bgclass=undefined href=# data-ptileno=24 aria-hidden=true disabled=disabled><span>24</span></a>`;
            _shell_wrapper.trigger("custmsetPageStatus", [6, 11, mytitle, 2]);

        } else {

            _shell_wrapper.trigger("updatebadge", [4, 1]);

            var _data = {
                mNo: 1,
                pNo: 10
            }
        }


        enabledDisable(_btn_next, false);
        enabledDisable(_btn_prev, false);

        _shell_wrapper.trigger("gotoModule", _data)
        _shell_wrapper.off("activityResult_3");
        _shell_wrapper.off("activityResult");
        _shell_wrapper.off("closeActivity");
        if ($("#tree_pTile_1_7").hasClass("completed")) {
            let _data = {
                mNo: 1,
                pNo: 3
            }
            let btn = $(".closebgtn");
            btn.off("click").on("click", function () {
                btn.off("click");
                _shell_wrapper.trigger("gotoModule", _data)
            }).addClass("active");
        }
        Utils.pageCompleted();
        // _shell_wrapper.trigger('jumpToNext');
    }

    var updateAudio = function (aNo, callback) {
        var _aData = _data.audio[aNo];
        _aData['callback'] = function () {
            callback();
        }
        if (_simple_audio) {
            _simple_audio.changeAudio(_aData);
        }
    }

    var showActivity = function (cTime, qNo) {
        loadNextQuestion(cTime, qNo);
    }

    var getData = function (_userDetails) {
        let _data = [{
            section: `
                <div class='closebgtn'>Close</div>
                `,
            id: "m01_l05_p03_04",
            userDataRef: {
                currentPageNo: 10
            },
            question_1: {
                id: 1,
                scenarioText: "",
                questionText: "<p>Which of the following statements correctly describes how a waiver of subrogation affects workers' compensation claims in cases where a client employee injures our Talent?​</p>",
                instructionText: "Select all the correct options and click <b>Submit.</b>",
                responseType: "genericResponse",
                maxAttempts: "2",
                type: "mcms",
                isNextQuestion: true,
                optionsGroup: [{
                    group: ["<p>With a waiver of subrogation, our workers' compensation insurance pays the full claim amount.​</p>", "<p>Without a waiver of subrogation, we can recover part of the claim costs from the client's insurance.​</p>", "<p>A waiver of subrogation reduces the branch's claims costs.</p>", "<p>Without a waiver of subrogation, our workers' compensation insurance cannot subrogate against the client’s insurance.</p>"],
                    correctAnswer: [1, 1, 0, 0]
                }],
                genericResponse: {
                    correct: {
                        description: "<p class='feedback_head'>Feedback</p> <p class='bold'>Yes, you are right!</p> <p>With a waiver of subrogation, our workers' compensation insurance pays the full claim amount. Also, without a waiver of subrogation, we can recover part of the claim costs from the client's insurance.</p>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './views/m01_l05_p03_04/m01_l05_p03_04_01.mp3',
                            timeline: [],
                            transcript: '<p>Yes, you are right!</p> <p>With a waiver of subrogation, our workers\' compensation insurance pays the full claim amount. Also, without a waiver of subrogation, we can recover part of the claim costs from the client\'s insurance.</p>'
                        }
                    },
                    partialCorrect: {
                        description: "<p class='feedback_head'>Feedback</p> <p>Try again!</p> <p></p>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './shell/audio/tryagain.mp3',
                            timeline: [],
                            transcript: '<p>Try again!</p> <p> </p>'
                        }
                    },
                    incorrect: {
                        description: "<p class='feedback_head'>Feedback</p> <p class='bold'>That’s not right!</p> <p>With a waiver of subrogation, our workers' compensation insurance pays the full claim amount. Also, without a waiver of subrogation, we can recover part of the claim costs from the client's insurance.</p>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './views/m01_l05_p03_04/m01_l05_p03_04_02.mp3',
                            timeline: [],
                            transcript: '<p>That’s not right!</p> <p>With a waiver of subrogation, our workers\' compensation insurance pays the full claim amount. Also, without a waiver of subrogation, we can recover part of the claim costs from the client\'s insurance.</p>'
                        }
                    },
                },
                "specificResponse": {
                    "response": [
                        {
                            "id": "1",
                            "isCorrect": "n",
                            "description": "That’s not right! The Limitation of Liability clause caps the company's liability, so the full cost of $200,000 is not owed. Your company is obligated to pay only $100,000, as this is the maximum liability stipulated in the contract."
                        },
                        {
                            "id": "2",
                            "isCorrect": "y",
                            "description": "Yes, you are right! With a waiver of subrogation, our workers' compensation insurance pays the full claim amount. Also, without a waiver of subrogation, we can recover part of the claim costs from the client's insurance.​"
                        },
                        {
                            "id": "3",
                            "isCorrect": "n",
                            "description": "That’s not right! The Limitation of Liability clause does not exempt the company from paying altogether. Your company is obligated to pay only $100,000, as this is the maximum liability stipulated in the contract. ​"
                        },
                        {
                            "id": "4",
                            "isCorrect": "n",
                            "description": "That’s not right! A partial payment can be a goodwill gesture, however, your company is legally obligated to pay $100,000, as this is the maximum liability stipulated in the contract. ​"
                        }
                    ]
                }
            },
            section: `
                 <h2 class="pagetitle"> </h2>
                 <div id = 'activityHolder' class = 'activityHolder h-100 page-section' > </div>
                `,
            audio: [{
                id: 1,
                autoplay: true,
                url: './views/m01_l05_p03_04/m01_l05_p03_04.mp3',
                timeline: [],
                transcript: '<p>Let us look at a scenario now.</p> '
            }]
        }];
        return _data;
    };

    var _shell_wrapper = $('#shell_wrapper');

    _shell_wrapper.trigger("initPage");


})();