(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
    };


    _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();
            }
        }
        _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_11", function () {
            _mcms_mcss = new initMCMS_MCSS_11();
            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_11');
       // _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 () {
        _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 = [{
            id: "m01_l05_p02",
            userDataRef: {
                currentPageNo: 5
            },
            question_1: {
                id: 1,
                scenarioText: "",
                questionText: "<p>Liability insurance comprises a variety of different insurance policies, which cover operational risks.​</p>  ",
                instructionText: "Select the correct option and then click on <b>“Submit”</b>.",
                responseType: "genericResponse",
                maxAttempts: "1",
                type: "mcss",
                isNextQuestion: true,
                optionsGroup: [{
                    group: ["<p>True", "<p>False</p>"],
                    correctAnswer: [1, 0]
                }],
                genericResponse: {
                    correct: {
                        description: "<p class='feedback_head'>Feedback</p> <p class='bold'>You are right!</p> <p>Liability insurance comprises a variety of different insurance policies, which cover operational risks.</p>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './views/m01_l05_p02/m01_l05_p02_01.mp3',
                            timeline: [],
                            transcript: '<p>You are right!</p> <p>Liability insurance comprises a variety of different insurance policies, which cover operational risks.</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  class='bold'>Not quite!</p> <p>Liability insurance comprises a variety of different insurance policies, which cover operational risks.</p>",
                        audio: {
                            id: 1,
                            autoplay: true,
                            url: './views/m01_l05_p02/m01_l05_p02_02.mp3',
                            timeline: [],
                            transcript: '<p>Not quite!</p> <p>Liability insurance comprises a variety of different insurance policies, which cover operational risks.</p>'
                        }
                    },
                }
            },
            section: `
                 <h2 class="pagetitle"> </h2>
                 <div id = 'activityHolder' class = 'activityHolder h-100 page-section' > </div>
                `,
            audio: [{
                id: 1,
                autoplay: true,
                url: './views/m01_l05_p02/m01_l05_p02.mp3',
                timeline: [],
                transcript: '<p>To move forward in this unit, you need to answer a quick question.</p><p>Liability insurance comprises a variety of different insurance policies, which cover operational risks. Is this true or false?</p><p><b><i>Select the correct option and then click “Submit.”</i></b></p> '
            }]
        }];
        return _data;
    };

    var _shell_wrapper = $('#shell_wrapper');

    _shell_wrapper.trigger("initPage");


})();