(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: 3,
            pNo: 9
        }*/

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

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

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

        }else{

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

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

        enabledDisable(_btn_next, false);
        enabledDisable(_btn_prev, false);
        Utils.pageCompleted();
        _shell_wrapper.trigger("gotoModule", _data)
        _shell_wrapper.off("activityResult_3");
        _shell_wrapper.off("activityResult");
        _shell_wrapper.off("closeActivity");
        
        // _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_l01_p07_04",
            userDataRef: {
                currentPageNo: 5
            },
            question_1: {
                id: 1,
                scenarioText: "",
                questionText: "<p>Talent files a claim alleging a client’s employee has been harassing them at the workplace. Given this situation, what is the most likely outcome of the claim?​</p>  ",
                instructionText: "Select all the correct options and click <b>Submit.</b>",
                responseType: "genericResponse",
                maxAttempts: "2",
                type: "mcms",
                isNextQuestion: true,
                optionsGroup: [{
                    group: ["<p>If we successfully included the Carve Out from the indemnity, then we will NOT have to defend the client against the claim.​</p>", "<p>If the Carve Out is not included and the indemnity is for the actions or inactions of Talent, we will have to defend the client even though the client's employee is the cause of the claim.</p>", "<p>If we successfully included the Carve Out from the indemnity, then we will have to defend the client against the claim.​</p>", "<p>If the Carve Out is not included and the indemnity is for the actions or inactions of Talent, we will NOT have to defend the client even though the client's employee is the cause of the claim.</p>"],
                    correctAnswer: [1, 1, 0, 0]
                }],
                genericResponse: {
                    correct: {
                        description: "<p class='feedback_head'>Feedback</p> <p class='bold'>Yes, you are right!</p> <p>If we successfully included the Carve Out from the indemnity, then we will NOT have to defend the client against the claim. If the Carve Out is not included and the indemnity is for the actions or inactions of Talent, we will have to defend the client even though the client's employee is the cause of the claim.​</p>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './views/m01_l02_p03_04/m01_l02_p03_04_1.mp3',
                            timeline: [],
                            transcript: '<p>Yes, you are right!</p> <p>If we successfully included the Carve Out from the indemnity, then we will NOT have to defend the client against the claim. If the Carve Out is not included and the indemnity is for the actions or inactions of Talent, we will have to defend the client even though the client\'s employee is the cause of the claim.​</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>If we successfully included the Carve Out from the indemnity, then we will NOT have to defend the client against the claim. If the Carve Out is not included and the indemnity is for the actions or inactions of Talent, we will have to defend the client even though the client's employee is the cause of the claim.​</p>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './views/m01_l02_p03_04/m01_l02_p03_04_2.mp3',
                            timeline: [],
                            transcript: '<p>That’s not right!</p> <p>If we successfully included the Carve Out from the indemnity, then we will NOT have to defend the client against the claim. If the Carve Out is not included and the indemnity is for the actions or inactions of Talent, we will have to defend the client even though the client\'s employee is the cause of the claim.​</p>'
                        }
                    },
                },
                "specificResponse": {
                    "response": [
                        {
                            "id": "1",
                            "isCorrect": "n",
                            "description": "<p class='bold'>That’s not right!</p><p> 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.</p>"
                        },
                        {
                            "id": "2",
                            "isCorrect": "y",
                            "description": "<p class='bold'>Yes, you are right!</p><p> Your company is obligated to pay only $100,000, as this is the maximum liability stipulated in the contract. Without the Limitation of Liability clause, the company would have been responsible for covering the full cost of the damaged televisions, potentially incurring additional expenses as well.​</p>"
                        },
                        {
                            "id": "3",
                            "isCorrect": "n",
                            "description": "<p class='bold'>That’s not right!</p><p> 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. ​</p>"
                        },
                        {
                            "id": "4",
                            "isCorrect": "n",
                            "description": "<p class='bold'>That’s not right!</p><p> 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. ​</p>"
                        }
                    ]
                }
            },
            section: `
                 <h2 class="pagetitle"> </h2>
                 <div id = 'activityHolder' class = 'activityHolder h-100 page-section' > </div>
                `,
            audio: [{
                id: 1,
                autoplay: true,
                url: './views/m01_l02_p03_04/m01_l02_p03_04.mp3',
                timeline: [],
                transcript: '<p>Let us look at another scenario now. ​</p> '
            }]
        }];
        return _data;
    };

    var _shell_wrapper = $('#shell_wrapper');

    _shell_wrapper.trigger("initPage");


})();