(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', 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 != 2) {
            aNo = 1
        }
        updateAudio(aNo, function () {
            lastStep();
        });
    }

    var lastStep = function () {
        Utils.pageCompleted();
        // _shell_wrapper.trigger('jumpToNext');
    }


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

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

    var getData = function (_userDetails) {
        var _data = [{
            section: `                 
                `,
            id: 'm01_l01_p04',
            userDataRef: {
                currentPageNo: 1
            },
            audio: [{
                id: "1",
                autoplay: true,
                url: './views/m01_l01_p04/m01_l01_p04.mp3',
                timeline: [{
                    'element': ".topic_list .mTile",
                    'sTime': 0.1,
                    'eTime': '',
                    'classIn': 'bgTrans',
                    'classOut': ''
                },{
                    'element': ".topic_list .mTile",
                    'sTime': 11.5,
                    'eTime': '',
                    'classIn': '',
                    'classOut': 'bgTrans'
                },{
                    'element': ".sub-section",
                    'sTime': 0,
                    'eTime': '',
                    'classIn': '',
                    'classOut': 'd-show'
                },
                {
                    'element': "#sub-section-1",
                    'sTime': 0.1,
                    'eTime': '',
                    'classIn': 'd-show',
                    'classOut': ''
                },
                {
                    'element': ".mTile-2 .clickBox",
                    'sTime': 0.2,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-3 .clickBox",
                    'sTime': 0.7,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-4 .clickBox",
                    'sTime': 0.12,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-5 .clickBox",
                    'sTime': 0.17,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-6 .clickBox",
                    'sTime': 0.22,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-7 .clickBox",
                    'sTime': 0.27,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-8 .clickBox",
                    'sTime': 0.32,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                }
                ],
                transcript: '<p>Now that you know the rules of the game, here is the map of the rooms you need to explore. Each room will unlock sequentially after you have earned a badge. However, the first room is unlocked for you.</p> <p>All the best!</p>',
            }, {
                id: "2",
                autoplay: true,
                url: './shell/audio/blank_2sec.mp3',
                timeline: [{
                    'element': ".sub-section",
                    'sTime': 0,
                    'eTime': '',
                    'classIn': '',
                    'classOut': 'd-show'
                },
                {
                    'element': "#sub-section-1",
                    'sTime': 0.1,
                    'eTime': '',
                    'classIn': 'd-show',
                    'classOut': ''
                },
                {
                    'element': ".topic-img",
                    'sTime': 0.1,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },{
                    'element': ".mTile-2 .clickBox",
                    'sTime': 0.2,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-3 .clickBox",
                    'sTime': 0.7,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-4 .clickBox",
                    'sTime': 0.12,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-5 .clickBox",
                    'sTime': 0.17,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-6 .clickBox",
                    'sTime': 0.22,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-7 .clickBox",
                    'sTime': 0.27,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                },
                {
                    'element': ".mTile-8 .clickBox",
                    'sTime': 0.32,
                    'eTime': '',
                    'classIn': 'bounceIn',
                    'classOut': ''
                }
                ],
                transcript: '<p></p>',
            }]
        }];
        return _data;
    };

    var _shell_wrapper = $('#shell_wrapper');

    _shell_wrapper.trigger("initPage");

})();