crmApp.service('MenuBarService', function ($window, $timeout, localStorageService) {
    var _mb = {
       // ActChannelOptions: {},
        //ChannelTitle: '',
        //SubMenuIcon: '',
        //MenuIcon: '',
        //ChannelDetails: '',
        //ChannelIndicator:'',
         //ChannelDetails: '',
        ActChannelID: 0, 
        Aliran: '',
        InteractionID: '',
        UserName: 'Agent',
        UserTitle: 'Agent',
        UserInitials: 'AA',
        Cawangan: 'Cawangan',
        TelNoInteraksi:'',
        Area: 'HCL',
        CaseID: 0,
        ChannelTitle: '',
        ChannelDetails: '',
        ChannelIndicator: '',
        SubMenuIcon: '',
        MenuIcon: ''
    };

    var interval = 2000;
    var counter = 0;
    var tmr = null;
    var curWinTitle = $window.document.title;
    var vm = this;

    this.MB = {
        get ActChannelID() {
            return _mb.ActChannelID;
        },
        set ActChannelID(value) {
            _mb.ActChannelID = value;
        },
        get Aliran() {
            return _mb.Aliran;
        },
        set Aliran(value) {
            _mb.Aliran = value;
        },
        get InteractionID() {
            return _mb.InteractionID;
        },
        set InteractionID(value) {
            _mb.InteractionID = value;
        },
        get UserName() {
            return _mb.UserName;
        },
        set UserName(value) {
            _mb.UserName = value;
        },
        get UserTitle() {
            return _mb.UserTitle;
        },
        set UserTitle(value) {
            _mb.UserTitle = value;
        },
        get UserInitials() {
            return _mb.UserInitials;
        },
        set UserInitials(value) {
            _mb.UserInitials = value;
        },
        get Area() {
            return _mb.Area;
        },
        set Area(value) {
            _mb.Area = value;
        },
        get Cawangan() {
            return _mb.Cawangan;
        },
        set Cawangan(value) {
            _mb.Cawangan = value;
        },
        get CaseID() {
            return _mb.CaseID;
        },
        set CaseID(value) {
            _mb.CaseID = value;
        },
        get TPID() {
            return _mb.TPID;
        },
        set TPID(value) {
            _mb.TPID = value;
        },
        get TPName() {
            return _mb.TPName;
        },
        set TPName(value) {
            _mb.TPName = value;
        },
        get TPTelNo() {
            return _mb.TPTelNo;
        },
        set TPTelNo(value) {
            _mb.TPTelNo = value;
        },
        
        get ActChannelOptions() {
            return _mb.ActChannelOptions;
        },
        set ActChannelOptions(value) {
            _mb.ActChannelOptions = value;
        }
    }

    vm.setInteractionChannel = function (intID, tpName) {
        
        var intSession = localStorageService.get('Int_ID' + intID);

        if (!intSession) return;
        if (intSession.WindowNames && intSession['WindowNames'].indexOf($window.name) == -1) return;

        var chnId = intSession.ChannelID;
        if (chnId == 1 || chnId == 5 || chnId == 7 || chnId == 8) {
            $timeout(toggleTitle, interval);
        }
        
        switch (chnId) {
            case 1:  //Chat
                _mb.ChannelTitle = 'Chat';
                _mb.SubMenuIcon = 'fa fa-weixin text-blue';
                _mb.MenuIcon = 'fa fa-commenting';
                _mb.ChannelDetails = tpName;
                _mb.ChannelIndicator = 'ql-item noti-link';
                changeFavicon('/Content/images/comment.png');
                break;
            case 5: //Phone 
                if (intSession.Aliran && intSession.Aliran.toUpperCase() == 'OUT') {
                    vm.MB.ChannelTitle = 'Outbound Call to';
                    vm.MB.SubMenuIcon = 'fa fa-phone text-orange';
                    vm.MB.ChannelIndicator = 'ql-item noti-link out-call';
                    changeFavicon('/Content/images/phone-outbound.png');
                }
                else {
                    vm.MB.ChannelTitle = 'Inbound Call from';
                    vm.MB.SubMenuIcon = 'fa fa-phone text-green';
                    vm.MB.ChannelIndicator = 'ql-item noti-link inb-call';
                    changeFavicon('/Content/images/phone-inbound.png');
                }
                vm.MB.MenuIcon = 'fa fa-volume-control-phone';
                vm.MB.ChannelDetails = vm.MB.TelNoInteraksi = intSession.PhoneNo;
                break;
            case 7:
                _mb.ChannelTitle = 'Social Media';
                _mb.SubMenuIcon = 'fa fa-envelope';
                _mb.MenuIcon = 'fa fa-share-alt';
                _mb.ChannelDetails = tpName;
                _mb.ChannelIndicator = 'ql-item noti-link';
                changeFavicon('/Content/images/email.png');
                break;
            case 8:
                _mb.ChannelTitle = 'CFF';
                _mb.SubMenuIcon = 'fa fa-envelope text-yellow';
                _mb.MenuIcon = 'fa fa-envelope';
                _mb.ChannelDetails = tpName;
                _mb.ChannelIndicator = 'ql-item noti-link';
                changeFavicon('/Content/images/email.png');
                break;
            //default:
            //    _mb.ChannelTitle = '';
            //    _mb.SubMenuIcon = '';
            //    _mb.MenuIcon = '';
            //    _mb.ChannelDetails = '';
            //    _mb.ChannelIndicator = '';
            //    changeFavicon("/Content/images/logolhdn.png");
            //    break;
        }
        
    }

    function toggleTitle() {
        $timeout.cancel(tmr);
        
        if (counter++ % 2 == 0) {
            $window.document.title = vm.MB.ChannelDetails ? "..." + vm.MB.ChannelDetails : curWinTitle;
        } else {
            $window.document.title = vm.MB.ChannelTitle + "...";
        }

        tmr = $timeout(toggleTitle, interval);
    }

    function changeFavicon(src) {
        var link = document.createElement('link'),
            oldLink = document.getElementById('dynamic-favicon');
        link.id = 'dynamic-favicon';
        link.rel = 'shortcut icon';
        link.href = src;
        if (oldLink) {
            document.head.removeChild(oldLink);
        }
        document.head.appendChild(link);
    }
});