var TopicList = function () {
    let _self = this;
    let _topic_list_wrapper = null;
    let _shell_wrapper = $("#shell_wrapper");
    let _totalbatch = -2;

    this.init = function () {
        // console.log("TopicList:   init...");
        _topic_list_wrapper = $("#topic_list_wrapper");
        setTimeout(function () {
            updateModuleStatus();
            addEvents();
        }, 300)
    }

    var updateModuleStatus = function () {
        let _menus = $(".topic_list .mTile")
        _menus.removeClass("active").addClass("not_attempted").removeClass("inprogress").removeClass("completed").removeClass("cMNo")

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

        setTimeout(function () {
            $.each(_moduleDetails, function (i, module) {
                let mItem = $(".topic_list #topic_" + (i + 1));
                mItem.removeClass("active");
                console.log("module.status---"+module.status)
                if (module.status >= 1) {
                    mItem.addClass("active");
                    
                }
                mItem.removeClass("not_attempted").addClass(module.sClass);
                console.log(module.mNo +"=="+ module.cMNo)
                if (module.mNo == module.cMNo) {
                    mItem.addClass(module.cMNo);
                    _totalbatch++;
                 // $(".batchCount").html(_totalbatch);
                }

            })
            checkAllCompleted();
        }, 30)


    }

    
    var checkAllCompleted  = function () {

        console.log("kkkk")
        let cDetails = window['course']['data']['shellController'].getCurrentMnoNdPno();
            
        var allCompleted = true;
        var completedCount = 0;

        for (var i = 2; i <= 8; i++) {
            if (!$("#topic_" + i).hasClass("completed")) {
                allCompleted = false;
            }

            if ($("#topic_" + i).hasClass("completed")) {
                completedCount++; 
            }
            console.log("iii--"+i)
        }

        let _batchCount = window['course']['data']['shellController'].updatebadgearryval();
       
        let sumOfValues = _batchCount.reduce((acc, curr) => acc + curr, 0);

        console.log("sum value:::"+sumOfValues);

        $(".batchCount").html(sumOfValues);

        //$(".batchCount").html(completedCount);

        if (allCompleted && cDetails.mNo == 9) {
            setTimeout(function(){
                console.log("enabledDisableenabledDisable----")
                enabledDisable($(".btn_prev"), false);
                enabledDisable($(".btn_next"), true);
            }, 400);
        }else{
            enabledDisable($(".btn_next"), false);
            enabledDisable($(".btn_prev"), false);
        }

        console.log(",,,,,,,,"+cDetails.mNo)
        
    }

    var addEvents = function () {

        setTimeout(function () {
            $(".topic_list .mTile").off("click").on("click", function () {
                let _tNo = parseInt($(this).attr("id").split("_")[1]) || 1;
                let _data = {
                    mNo: _tNo,
                    pNo: 2
                }
                // $("#tree_pTil_" + mNo + "_2").trigger("click")
                _shell_wrapper.trigger("gotoModule", _data)
            })
        }, 300)
    }
    return this;
};

