(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 addEvents = function () {
        _shell_wrapper.off("activityResult_3").on("activityResult_3", function (e, data) {
            //  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: 1,
            pNo: 7
        }

        enabledDisable(_btn_next, false);
        _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_l02_p03_03",
            userDataRef: {
                currentPageNo: 7
            },
            question_1: {
                id: 1,
                scenarioText: "",
                questionText: "<p>Imagine an employee of your company accidentally backed a forklift into a pallet of flat-screen televisions at a client's warehouse. The total value of the damaged televisions is $200,000. The company is liable for a capped amount of $100,000. If there is also an indemnity in the contract, how could that impact the company’s responsibility?</p>",
                instructionText: "Select the correct option and click <b>Submit.</b>",
                responseType: "specificResponse",
                maxAttempts: "2",
                type: "mcss",
                isNextQuestion: true,
                optionsGroup: [{
                    group: ["<p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.​</p>", "<p>We agreed to indemnification, so we pay 100%.</p>", "<p>If the indemnification is for the actions or inactions of Talent, we don’t pay anything.</p>", "<p>If the indemnification is for negligence of Talent, it limits the liability to 50%.</p>", "<p>All of the above</p>"],
                    correctAnswer: [1, 0, 0, 0, 0]
                }],
                genericResponse: {
                    correct: {
                        description: "<p class='feedback_head'>Feedback</p> <p>You are right!</p> <p>There are multiple ways to answer this question based on the indemnification lesson:</p> <ol> <li>If we have an indemnity for the actions or inactions of Talent, we would pay the full cost.</li> <li>If we have an indemnity for gross negligence or willful misconduct, we would only pay if the actions were intentional or reckless.</li> <li>If there is a Carve Out, we would not pay if the client was negligent in their training of the employee on how to use the client's forklift.</li> </ol>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './shell/audio/blank.mp3',
                            timeline: [],
                            transcript: '<p>You are right!</p> <p>There are multiple ways to answer this question based on the indemnification lesson:</p> <ol> <li>If we have an indemnity for the actions or inactions of Talent, we would pay the full cost.</li> <li>If we have an indemnity for gross negligence or willful misconduct, we would only pay if the actions were intentional or reckless.</li> <li>If there is a Carve Out, we would not pay if the client was negligent in their training of the employee on how to use the client\'s forklift.</li> </ol>'
                        }
                    },
                    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>There are multiple ways to answer this question based on the indemnification lesson:</p> <ol> <li>If we have an indemnity for the actions or inactions of Talent, we would pay the full cost.</li> <li>If we have an indemnity for gross negligence or willful misconduct, we would only pay if the actions were intentional or reckless.</li> <li>If there is a Carve Out, we would not pay if the client was negligent in their training of the employee on how to use the client's forklift.</li> </ol>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './shell/audio/blank.mp3',
                            timeline: [],
                            transcript: '<p>Not quite!</p> <p>There are multiple ways to answer this question based on the indemnification lesson:</p> <ol> <li>If we have an indemnity for the actions or inactions of Talent, we would pay the full cost.</li> <li>If we have an indemnity for gross negligence or willful misconduct, we would only pay if the actions were intentional or reckless.</li> <li>If there is a Carve Out, we would not pay if the client was negligent in their training of the employee on how to use the client\'s forklift.</li> </ol>'
                        }
                    },
                },
                "specificResponse": {
                    "response": [
                        {
                            "id": "1",
                            "isCorrect": "y",
                            "description": "<p class='bold'>You got that right!</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.</p>",
                            audio: {
                                id: 1,
                                autoplay: true,
                                url: './views/m01_l02_p03_03/m01_l02_p03_03_01.mp3',
                                timeline: [],
                                transcript: '<p>You got that right!</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.</p>'
                            }
                        },
                        {
                            "id": "2",
                            "isCorrect": "n",
                            "description": "<p class='bold'>That’s not correct.</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.​</p>",
                            audio: {
                                id: 1,
                                autoplay: true,
                                url: './views/m01_l02_p03_03/m01_l02_p03_03_02.mp3',
                                timeline: [],
                                transcript: '<p>That’s not correct.</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.​</p>'
                            }
                        },
                        {
                            "id": "3",
                            "isCorrect": "n",
                            "description": "<p class='bold'>That’s not correct.</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.​</p>",
                            audio: {
                                id: 1,
                                autoplay: true,
                                url: './views/m01_l02_p03_03/m01_l02_p03_03_03.mp3',
                                timeline: [],
                                transcript: '<p>That’s not correct.</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.​</p>'
                            }
                        },
                        {
                            "id": "4",
                            "isCorrect": "n",
                            "description": "<p class='bold'>That’s not correct.</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.​</p>",
                            audio: {
                                id: 1,
                                autoplay: true,
                                url: './views/m01_l02_p03_03/m01_l02_p03_03_04.mp3',
                                timeline: [],
                                transcript: '<p>That’s not correct.</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.​</p>'
                            }
                        },
                        {
                            "id": "5",
                            "isCorrect": "n",
                            "description": "<p class='bold'>That’s not correct.</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.​</p>",
                            audio: {
                                id: 1,
                                autoplay: true,
                                url: './shell/audio/blank.mp3',
                                timeline: [],
                                transcript: '<p>That’s not correct.</p><p>If the indemnification is for the gross negligence or willful misconduct of the Talent, we don’t pay anything.​</p>'
                            }
                        },
                        {
                            "id": "6",
                            "isCorrect": "n",
                            "description": "<p class='bold'>Try again!</p><p>​</p>",
                            audio: {
                                id: 1,
                                autoplay: true,
                                 url: './shell/audio/tryagain.mp3',
                                timeline: [],
                                transcript: '<p>Try again!​</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_03/m01_l02_p03_03.mp3',
                timeline: [],
                transcript: '<p>Let us look at a scenario now.</p> '
            }]
        }];
        return _data;
    };

    var _shell_wrapper = $('#shell_wrapper');

    _shell_wrapper.trigger("initPage");


})();