(function () {

    var _shell_wrapper = null,
        _data = null,
        _userDetails = null,
        _shell_wrapper = $('#shell_wrapper');


    let _moduleDetails = null
    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) {
            _pageData = pageData || _pageData
            e.stopPropagation();
            updateDom();
        });

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


        _shell_wrapper.off("takeModuleDetails").on("takeModuleDetails", function (e, mData) {
            e.stopPropagation();
            _moduleDetails = mData;

            _scorePoints = parseInt($("#score-points").text()) || 0;
            _shell_wrapper.trigger('getUserDetails');
        });

        _shell_wrapper.trigger("getModuleDetails")
    });

    var updateDom = function () {

        Utils.loadTemplate('#templateHolder', 'topic_list_1', function () {
            var _topicList = new TopicList();
            _topicList.init();
            pageInit();
        });
    }


    var pageInit = function () {

        //    console.log("pageInit")

        let _initData = {
            'callback': function () {
                clearTimeout(_timerInstance);
                _shell_wrapper.off("clearAlertTimerInstance").on("clearAlertTimerInstance", function (e) {
                    clearTimeout(_timerInstance);
                    _shell_wrapper.off("clearAlertTimerInstance");
                });

                stepStart();
            }
        }

        _shell_wrapper.trigger('pageLoaded', _initData);

    }


    var stepStart = function () {
        let aNo = 0;
        if (_moduleDetails.mNo != 1) {
            aNo = 1
        }

        if ($("#tree_pTile_1_4").hasClass("completed") || $("#tree_pTile_1_5").hasClass("completed") || $("#tree_pTile_1_6").hasClass("completed")) {
            aNo = 1;
        }

        updateAudio(aNo, function () {
            lastStep();
        });
    }

    var lastStep = function () {

        // _shell_wrapper.trigger('jumpToNext');
    }


    var updateAudio = function (aNo, callback) {
        var _audioData = _data.audio[aNo];

        console.log("_moduleDetails.mNo ")
        console.log(_moduleDetails.mNo)
        console.log("_audioData ")
        console.log(_audioData)
        

        _audioData['callback'] = _audioData['callback'] || callback || function () { }
        if (_simple_audio) {
            _simple_audio.changeAudio(_audioData);
        }
    }

    var getData = function (_userDetails) {
        var _data = [{
            section: `                 
                `,
            id: 'm01_l01_p07',
            userDataRef: {
                currentPageNo: 1
            },
            audio: [
                {
                id: "1",
                autoplay: true,
                url: './views/m01_l01_p07/m01_l01_p07.mp3',
                timeline: [{
                    'element': ".pagetitle",
                    'sTime': 0,
                    'eTime': '',
                    'classIn': '',
                    'classOut': 'd-show'
                },
                {
                    'element': ".topic_list_body .instTxt",
                    'sTime': 2.5,
                    'eTime': '',
                    'classIn': 'd-show',
                    'classOut': ''
                },
                {
                    'element': ".topic_list  #topic_2",
                    'sTime': 0.5,
                    'eTime': '',
                    'classIn': 'iconFadeInLeft',
                    'classOut': ''
                },
                {
                    'element': ".topic_list  #topic_3",
                    'sTime': 1,
                    'eTime': '',
                    'classIn': 'iconFadeInLeft',
                    'classOut': ''
                },
                {
                    'element': ".topic_list  #topic_4",
                    'sTime': 1.5,
                    'eTime': '',
                    'classIn': 'iconFadeInLeft',
                    'classOut': ''
                },
                {
                    'element': ".topic_list  #topic_5",
                    'sTime': 2,
                    'eTime': '',
                    'classIn': 'iconFadeInLeft',
                    'classOut': ''
                }
                ],
                transcript: '<p>Now that you have answered the question, click each icon to learn more.</p>',
            }, {
                id: "2",
                autoplay: true,
                url: './shell/audio/blank_4sec.mp3',
                timeline: [{
                    'element': ".pagetitle",
                    'sTime': 0,
                    'eTime': '',
                    'classIn': '',
                    'classOut': 'd-show'
                },
                {
                    'element': ".topic_list_body .instTxt",
                    'sTime': 2.5,
                    'eTime': '',
                    'classIn': 'd-show',
                    'classOut': ''
                },
                {
                    'element': ".topic_list  #topic_2",
                    'sTime': 0.5,
                    'eTime': '',
                    'classIn': 'iconFadeInLeft',
                    'classOut': ''
                },
                {
                    'element': ".topic_list  #topic_3",
                    'sTime': 1,
                    'eTime': '',
                    'classIn': 'iconFadeInLeft',
                    'classOut': ''
                },
                {
                    'element': ".topic_list  #topic_4",
                    'sTime': 1.5,
                    'eTime': '',
                    'classIn': 'iconFadeInLeft',
                    'classOut': ''
                },
                {
                    'element': ".topic_list  #topic_5",
                    'sTime': 2,
                    'eTime': '',
                    'classIn': 'iconFadeInLeft',
                    'classOut': ''
                }
                ],
                transcript: '<p></p>',
            }]
        }];
        return _data;
    };

    var _shell_wrapper = $('#shell_wrapper');

    _shell_wrapper.trigger("initPage");

})();