
(function () {
    var CApp, CMainController, CCallQueue, CAgentQueue, CTemplate,
        CMemberView, CQueueView;

    BX24.callMethod('user.current', {}, function (res) {
        if (res.error()) {
            console.error(res.error());
            return;
        }
        var data = res.data();
        if (data.ID === '150') {
            main();
        }
    });

    function main () {
        CApp = getApp();
        CMainController = getMainController();
        CCallQueue = getQueue();
        CAgentQueue = getAgentQueue();
        CTemplate = getTemplate();
        CQueueView = getQueueView();
        CMemberView = getMemberView();

        window.App = new CApp();
        window.MainController = new CMainController({
            targets: {
                agents: "#agents",
                members: "#queues",
                // queuesTab: '#queues_tab',
            }
        });

        $().ready(onReady);
    }

    function onReady() {
        feather.replace();
    }

    function getParams () {
        return {
            statuscode: {
                'off' : 'Отключён',
                'free' : 'Свободен',
                'dnd' : 'DND вкл.',
                'incring' : 'Вх.звонок',
                'inccall' : 'Вх.разговор',
                'outring' : 'Исх.Звонок',
                'outcall' : 'Исх.Разговор',
            },

            statuscolor: {
                'off' : "#F26B47",
                'free' : "#FFF893",
                'dnd' : "#F5B041",
                'incring' : '#99ffb2',
                'inccall' : '#7BC56F',
                'outring' : '#73dcff',
                'outcall' : '#0070BF',
            }
        }
    }


    
    function getMainController () {
        function MainController (params) {
            this.datas = {};

            this.targets = params.targets;


            this.Member;
            this.Chanel;
            this.Queue;
        }

        MainController.prototype = {
            init(datas) {
                var prevDatas = this.datas;
                this.setNewDatas(datas);

                // this.initChanel();
            },

            getQueue() {

            },

            update(params) {

            },

            setNewDatas(newDatas) {
                this.datas = newDatas;
            },

            /**
             * 
             * @param {String|Number} number - member inner number 
             * @param {String|Number} queue - queue number
             */
            addMemberToQueue(number, queue) {
                doAloVoiceCmd('addMemeberQueue', {
                    queue:  queue,
                    member: number
                }, function() {
                    alert(number + ' добавлен в очередь ' + queue);
                });
            },

            removeMemberFromQueue(number, queue) {

            },

            addQueue() {
                
            },

            removeQueue() {

            },

            toggltAgentDnd() {

            },

            _toCurrent(newData, prevData) {
                if (!prevData) {
                    prevData = {};
                }

                return {
                    updata: getUpdates(newData, prevData),
                    new: getNews(newData, prevData),
                    remove: getRemoves(newData, prevData)
                }

                function getUpdates (newData, curretData) {
                    var result = [];
    
                    $.each(newData, function (key, data) {
                        if (curretData[key]) {
                            result.push(data);
                        }
                    });
    
                    return result;
                }
                function getNews (newData, curretData) {
                    var result = [];
    
                    $.each(newData, function (key, data) {
                        
                        if (!curretData[key]) {
                            result.push(data);
                        }
                    });
    
                    return result;
                }
                function getRemoves (newData, curretData) {
                    var result = [];
    
                    $.each(curretData, function (key, data) {
                        if (!newData[key]) {
                            result.push(data);
                        }
                    });
    
                    return result;
                }
            },
        }

        return MainController;
    }

    function getQueue () {
        function Queue(queue, Template) {
            this.queue = queue;

            this._template = Template;
            this._node;

            this.init();
        }

        Queue.prototype = {
            init() {
                this.createNode();
            },

            delete() {
                // MainController.deleteQueue();
            },

            removeNode() {
                this._template.removeNode();
            },
            createNode() {
                this._template.createNode();
            },
            getTemplate() {
                return this._template.getTemplate();
            },
            getNode() {
                return this._template.getNode();
            }
        }

        return Queue;
    }

    function getAgentQueue() {
        function AgentQueue() {
            this.target;
        }

        AgentQueue.prototype = {
            init(params) {

            },
            update(params) {

            },
            render() {

            }
        }

        return AgentQueue;
    }

    function getTemplate() {
        function Template(temp) {
            this.temp = temp;
            this.node;
        }

        Template.prototype = {
            /**
             * @returns {void}
             */
            createNode() {
                this.node = $(this.temp).get(0);
            },

            /**
             * @returns {DOM|String}
             */
            getNode() {
                return this.node || '';
            },

            /**
             * @returns {void}
             */
            removeNode() {
                $(node).remove();
            },

            /**
             * @returns {String}
             */
            getTemplate() {
                return this.temp;
            }
        }

        return Template;
    }

    function getApp () {
        function App () {

        }

        App.prototype = {
            showMessage(msg) {
                alert(msg);
            }
        }

        return App;
    }

    function getMemberView () {
        function MemberView() {
            this.member;
            this.queue;
            this._template = '';
        }

        MemberView.prototype = {
            init() {

                return this.getTemplate();
            },

            update() {

            },

            remove() {

            },

            getTemplate() {
                return '';
            }
        }

        return MemberView;
    }

    function getQueueView () {
        function QueueView (params) {
            this.queue = params.queue;
            this._template = this._createEl(this._setTemplate());
            this.isActive = params.isActive;

            this.btnClass = 'nav-link';
            this.activeClass= 'active';
        }

        QueueView.prototype = {
            remove() {
                $(this._template).remove();
            },

            getTemplate() {
                return '';
            },

            _createEl(tmp) {
                return $(tmp).get(0);
            },

            _setTemplate() {
                return '<li class="nav-item">' +
                    '<a class="'+this._getBtnClass()+'" href="#'+this.queue+'" ' +
                        'data-toggle="pill" aria-expanded="'+this.isActive+'" queue-nav="'+this.queue+'">'+
                        this.queue +
                    '</a>'+
                '</li>';
            },

            _getBtnClass() {
                var result = this.btnClass;

                if (this.isActive) {
                    result += ' ' + this.activeClass;
                }

                return result;
            }
        }

        return QueueView;
    }

    function getCallChanalView () {
        function CallsChanalView () {

        }

        CallsChanalView.prototype = {

        }

        return CallsChanalView;
    }

})();