var ShellProgress = function () {

    var _self = this;

    var _shell_wrapper = null;
    var _progress_wrapper = null;
    var _parentIndex = -1;
    var _btn_close = null;

    this.init = function () {
      //  console.log("ShellProgress:   init...")
        _shell_wrapper = $("#shell_wrapper");
        _progress_wrapper = $("#progress_wrapper");
        _parentIndex = parseInt(_progress_wrapper.parent().attr('tabindex')) + 1 || -1;
        _btn_close = _progress_wrapper.find(".close_btn")
        addEvents();
    }



    var addEvents = function () {
      //  console.log("ShellProgress:   addEvents...")
        _btn_close.off("click").on("click", function () {
            _shell_wrapper.trigger("closePopup")
            var _data = {
                'popupName': 'Progress'
            }
            _shell_wrapper.trigger("closePopupGlobalPopup", _data);
            // $("#shell_wrapper").trigger("closeGlobalPopup");
        })

        _shell_wrapper.off("openProgress").on("openProgress", function (e) {
            e.stopPropagation();
            //  Utils.removeTabIndex()
            openProgress();
        });

        _shell_wrapper.off("closePopup").on("closePopup", function (e) {
            e.stopPropagation();
            closePopup();
        });

    }


    var addTabIndex = function () {
        var _tabElements = {
            'elements': [{
                    'selector': "#progress_popup_holder .modal-content"
                },
                {
                    'selector': "#progress_popup_holder ul li>a"
                },
                {
                    'selector': _btn_close,
                    'title': 'Close'
                }
            ],
            'sIndex': 1,
            'nIndex': 1
        };
        _shell_wrapper.trigger("addTabIndexing", _tabElements);
    }

    var openProgress = function () {
        // addTabIndex()
        let _progress_popup_holder = $("#progress_popup_holder .modal-content")
        _progress_popup_holder.trigger('focus');
        _btn_close.blur(function () {
            _progress_popup_holder.focus();
        })

        setTimeout(function () {
            updateModuleStatus();
        }, 600)
    }

    var closePopup = function () {
        $("#shell_wrapper").trigger("closeGlobalPopup");
    }

    var updateModuleStatus = function () {
        let _menu = $("#progressBar.mTile-list")
        let _mItems = _menu.find(".mTile")
        _mItems.removeClass("not_attempted").removeClass("inprogress").removeClass("completed").removeClass("cMNo")

        let _moduleDetails = window['course']['data']['shellController'].getModulesDetails();

        $.each(_moduleDetails, function (i, module) {
            if (i != 0) {
                let mItem = _menu.find(".mTile.mTile-" + i);              
              //  console.log(i, module, mItem)
                if (module.status>=1) {
                     mItem.addClass("active");
                }  
                mItem.addClass(module.sClass);
                if (module.mNo == module.cMNo) {
                    mItem.addClass(module.cMNo);
                }
                
            }

        })
    }
    return this;
};

$(document).ready(function () {
    var _ShellProgress = new ShellProgress();
    _ShellProgress.init();
});