var LeftSideNav = (function() {

    var _self = this,
        _data = null,
        _uData = null,
        _mNo = 1,
        _pNo = 0,
        _btn_ref = {
            'btn_home': '',
            'btn_mail': '',
            'btn_connect': '',
            'btn_chat': '',
            'btn_activity': '',
            'btn_board': '',
            'btn_calendar': '',
            'btn_message': ''
        },
        _btns = null,
        _shell_wrapper = null,
        _left_sideNav_holder = null;

    this.init = function(data, callback) {
        console.log('LeftSideNav.. Init');
        _data = data;
        _uData = data['uData'];
        _shell_wrapper = $("#shell_wrapper");
        _left_sideNav_holder = $("#left_sideNav_holder");
        _btns = $(".navbar_icons_spri .btn");

        _self.updateLeftNavRef(data)

        if (callback) {
            callback()
        }
    };

    var removeEventFromLeftNavRef = function() {
        try {
            $.each(_btn_ref, function(btnName, pageName) {
                let _btn = $("#left_sideNav_holder #" + btnName)
                _btn.removeAttr('data-page')
                _btn.removeClass('selected').removeClass('blink').removeClass('notifing')
                Utils.enabledDisable(_btn, false);
                _btn.removeAttr('tabindex');
            })
        } catch (err) {

        }
    }

    this.updateLeftNavRef = function(data) {
        removeEventFromLeftNavRef()
        try {
            let _btns = data['btn_ref'] || _btn_ref;
            _mNo = data['mNo'] || 1
            _pNo = data['pNo'] || 0;
            addEvents(_btns);
        } catch (err) {

        }
    }

    this.updateData = function(data) {
        try {
            _uData = data['uData'];
            _data = data;
            updateDom()
        } catch (err) {

        }

    }

    var updateDom = function() {

        _self.removeNotification(_btns)
        try {
            let _btnName = _data['lsn_btn']
            let _c_btn = $("#" + _btnName);
            Utils.enabledDisable(_c_btn, false);
            _c_btn.addClass('selected');
        } catch (err) {

        }

        let _isShow = _data['isShow'] || false;
        _left_sideNav_holder.removeClass("show");
        _left_sideNav_holder.hide();

        if (_isShow) {
            _left_sideNav_holder.addClass("show");
            let _btn = $("#left_sideNav_holder #btn_files")
            _self.addEventsOnFiles(_btn)
        }
    }

    var addEvents = function(btns) {
        let _pNo = 0;
        $.each(btns, function(btnName, pageName) {
            try {
                let _btn = $("#left_sideNav_holder #" + btnName)
                if (pageName != "") {
                    _btn.attr('data-page', pageName)
                    _btn.off("click").on("click", function() {
                        let __btn = $(this)
                        if (__btn.hasClass('active') && !(__btn.hasClass('selected'))) {
                            let _pageName = __btn.attr('data-page');
                            if (_pageName != "") {
                                _shell_wrapper.trigger('jumpToPage', _pageName);
                                __btn.addClass('selected');
                                __btn.addClass('disabled').removeClass('active')
                                __btn.removeAttr('tabindex');
                                Utils.enabledDisable(__btn, false);
                            }
                        }
                    })
                    Utils.enabledDisable(_btn, false);
                } else {
                    _btn.addClass('disabled');
                    _btn.removeAttr('tabindex');
                }

            } catch (err) {

            }
        })

        updateDom();
    }

    this.addEventsOnFiles = function(_btn) {
        _btn.off("click").on("click", function() {
            $("#btn_resources").trigger("click")
        })
        Utils.enabledDisable(_btn, true);
        _btn.attr('tabindex','545');
    }

    this.removeNotification = function(_btn, callback) {
        let __btn = $(_btn)
        __btn.removeClass('selected');
        __btn.removeClass('blink').removeClass("notifing");
        __btn.find('.notification').removeClass("notification_01")
            //__btn.addClass("disabled").removeClass("active")
        __btn.removeAttr('tabindex');

        if (callback) {
            callback()
        }
        
    }

    this.addNotification = function(btn, callback, target) {

        let _btn = $(btn);

        _btn.addClass('blink').addClass('notifing')
        _btn.find('.notification').addClass('notification_01');

        let _audioType = "notification"
            //   console.log("addNotification:   ",_btn)   

        try {
            let _btn_name = _btn.attr('id').split('_')[1] || '';
            if (_btn_name !== '') {
                _audioType = _btn_name + '_notification'
            }
        } catch (err) {
            console.log(err)
        }

        if (target != "") {
            _btn.attr('data-page', target)
        }

        //   console.log("addNotification:  audioType ",_audioType) 
        Utils.enabledDisable(_btn, true);
        _btn.focus();
        _btn.attr('tabindex','545');

        _shell_wrapper.trigger('sendAudioDetails', {
            audioType: _audioType,
            callback: function() {
                Utils.enabledDisable(_btn, true);
                _btn.focus();
                callback();
            }
        });
    }

    return this;
});