(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 () {


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

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

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

        } else {

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

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



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

        // let btn = $(".closebgtn");
        // btn.off("click").on("click", function () {
        //     btn.off("click");
        //     _shell_wrapper.trigger("gotoModule", _data)
        // }).addClass("active");
        _shell_wrapper.trigger("gotoModule", _data)
        _shell_wrapper.off("activityResult_3");
        _shell_wrapper.off("activityResult");
        _shell_wrapper.off("closeActivity");


        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_l01_p07_04",
            userDataRef: {
                currentPageNo: 5
            },
            question_1: {
                id: 1,
                scenarioText: "",
                questionText: "<p>An employee of your company accidentally backs a forklift into a pallet of flat-screen televisions at a client's warehouse. The total value of the damaged televisions is $200,000. Your company has a Limitation of Liability clause in its contract with the client, capping liability at $100,000. What is the correct amount your company owes the client based on the Limitation of Liability clause?</p>  ",
                instructionText: "Select the correct option and click <b>Submit.</b>",
                responseType: "specificResponse",
                maxAttempts: "2",
                type: "mcss",
                isNextQuestion: true,
                optionsGroup: [{
                    group: ["<p>$200,000 — The full cost of the damaged televisions as liable.</p>", "<p>$100,000 — The maximum amount liable.</p>", "<p>$0 — Since the damage was accidental, the company is not liable for anything.</p>", "<p>$50,000 — A partial payment as liable.</p>"],
                    correctAnswer: [0, 1, 0, 0]
                }],
                genericResponse: {
                    correct: {
                        description: "<p class='feedback_head'>Feedback</p> <p>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>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './shell/audio/blank.mp3',
                            timeline: [],
                            transcript: '<p>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>'
                        }
                    },
                    partialCorrect: {
                        description: "<p class='feedback_head'>Feedback</p> <p>This is partially correct.</p> <p></p>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './shell/audio/blank.mp3',
                            timeline: [],
                            transcript: '<p>This is partially correct.</p> <p> </p>'
                        }
                    },
                    incorrect: {
                        description: "<p class='feedback_head'>Feedback</p> <p>Not quite!</p> <p>Candidates for ITAR covered roles do not have to be US Citizens to be eligible.  They must be US Persons which includes protected persons like refugees or asylees</p>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './shell/audio/blank.mp3',
                            timeline: [],
                            transcript: '<p>Not quite!</p> <p>Candidates for ITAR covered roles do not have to be US Citizens to be eligible.  They must be US Persons which includes protected persons like refugees or asylees</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>",
                            "audio": [
                                {
                                    id: 1,
                                    autoplay: true,
                                    url: './views/m01_l01_p07_04/m01_l01_p07_04_01.mp3',
                                    timeline: [],
                                    transcript: `<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>",
                            "audio": [
                                {
                                    id: 1,
                                    autoplay: true,
                                    url: './views/m01_l01_p07_04/m01_l01_p07_04_02.mp3',
                                    timeline: [],
                                    transcript: `<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>",
                            "audio": [
                                {
                                    id: 1,
                                    autoplay: true,
                                    url: './views/m01_l01_p07_04/m01_l01_p07_04_03.mp3',
                                    timeline: [],
                                    transcript: `<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 contractually obligated to pay $100,000, as this is the maximum liability stipulated in the contract. ​ ​</p>",
                            "audio": [
                                {
                                    id: 1,
                                    autoplay: true,
                                    url: './views/m01_l01_p07_04/m01_l01_p07_04_04.mp3',
                                    timeline: [],
                                    transcript: `<p class='bold'>That’s not right!</p><p>A partial payment can be a goodwill gesture, however, your company is contractually obligated to pay $100,000, as this is the maximum liability stipulated in the contract. ​ ​</p>`
                                }

                            ]
                        },
                        {
                            "id": "5",
                            "isCorrect": "n",
                            "description": "<p class='bold'>Try again!</p><p>​</p>",
                            "audio": [
                                {
                                    id: 1,
                                    autoplay: true,
                                    url: './shell/audio/tryagain.mp3',
                                    timeline: [],
                                    transcript: `<p class='bold'>Try again!</p><p>​</p>`
                                }

                            ]
                        }
                    ]
                }
            },
            section: `
                 <h2 class="pagetitle"> </h2>
                 <div id = 'activityHolder' class = 'activityHolder h-100 page-section' > </div>
                `,
            audio: [{
                id: 1,
                autoplay: true,
                url: './views/m01_l01_p07_04/m01_l01_p07_04.mp3',
                timeline: [],
                transcript: '<p>Let us look at a scenario now.</p> '
            }]
        }];
        return _data;
    };

    var _shell_wrapper = $('#shell_wrapper');

    _shell_wrapper.trigger("initPage");


})();