let chat = {
    chat_id: null,
    settings: {
        ajaxUrl: '',
        chat_listUrl: null,
        chat_add_msgUrl: null,
    },
    chat_ids: [],
    userId: null,
    init: function() {
        global_chat.in_messenger_page = true;
        chat.bindChatListBtn();
        chat.getChatIds();
    },
    bindChatListBtn: function() {
        jQ("#chat-conversation-list li").unbind('click').bind('click',function(e) {
            jQ("#chat-conversation-list li").removeClass('active');
            jQ(this).addClass('active');
            let chat_id = jQ(this).data('id');
            chat.chat_id = chat_id;
            chat.chatList(chat_id);
            jQ(this).children('.badge').addClass('hide');
        })
    },
    chatList: function(chat_id) {

        let requestName = 'chatList',
            chatListContainer = jQ('#chat-conversation-container');

        global.ajaxCancel(requestName);

        let settings = {
            url: chat.settings.chat_listUrl,
            type: 'POST',
            data: {chat_id: chat_id},
            beforeSend: function() {
                chatListContainer.html(`<div style="display: table;width: 100%;height: 100%;text-align: center"><div style="display: table-cell;vertical-align: middle;letter-spacing: 3px;font-size: 1.2em;color: #777;"><i class="fa fa-pulse fa-spinner"></i><br>Loading messages...</div></div>`);
            },
            success: function(json) {
                if(json.success) {
                    chatListContainer.removeClass('has-loading').html(json.html);
                    jQ('#txt-chat-msg').unbind('keypress').bind('keypress', function(event){
                        jQ('#chat-conversation-list li[data-id='+chat.chat_id+']').children('.badge').addClass('hide');
                        if(event.keyCode == 13){
                            chat.addChatProcess();
                        }
                    });
                    jQ('#chat-conversation-container').scrollTop(jQ('#chat-conversation-container').scrollTop() + jQ('#txt-chat-msg').position().top);
                    jQ("#chat-header").width(jQ("#txt-chat-msg").width())
                } else {
                    location.reload();
                }
            }
        };

        global.ajax(settings, requestName);
    },
    getChatIds: function(){
        jQ("#chat-conversation-list li").each(function(i,el){
            chat.chat_ids.push(jQ(el).data('id'));
        })
    },
    bindTxtboxEvent: function(){
        jQ("#txt-chat-msg").on('input',function(e){
            jQ('#chat-conversation-list li[data-id='+chat.chat_id+']').children('.badge').addClass('hide');
        })
    },
    addChatProcess: function(){
        let msg = jQ('#txt-chat-msg').val();

        if('' === msg) {
            return;
        }

        let refChatId = 'cdt-' + (Math.floor((Math.random() * 999) + 1)) + "-" + new Date().getTime(),
            cmc = '<div class="chat-msg-container">' +
                '                <div class="chat-msg pull-left">' +
                '                    <div class="chat-datetime" id="' + refChatId + '"><i class="fa fa-spinner fa-pulse"></i></div>' +
                '                    <div>' + msg + '</div>' +
                '                </div>' +
                '                <div class="chat-avatar pull-right">' +
                '                    <img src="/bundles/rxconnexion/admin/images/main-menu-default-avatar.png" />' +
                '                </div>' +
                '            </div>';

        jQ('#txt-chat-msg').val('');
        jQ('#chat-container').append(cmc);
        jQ('#chat-conversation-container').scrollTop(jQ('#chat-conversation-container').scrollTop() + jQ('#txt-chat-msg').position().top);
        chat.chatSendProcess(refChatId, msg);
    },
    chatSendProcess: function(refId, msg) {
        let settings = {
            type: 'POST',
            url: chat.settings.chat_add_msgUrl,
            data: {
                chat_id: chat.chat_id,
                refId: refId,
                msg: msg
            },
            error: function() {
                //alert('Something went wrong, please try again. If the problem persist, please contact the administrator.');
                jQ('#' + refId).html('<i class="fa fa-refresh"></i>');
                jQ('#' + refId + ' i').bind('click', function() {
                    jQ('#' + refId).html('<i class="fa fa-spinner"></i>');
                    chat.chatSendProcess(refId, msg);
                });
            },
            success: function(json) {
                if(json.success) {
                    //eventTran_detail.chatList(false);
                    if(json.added) {
                        // eventTran_detail.chats.sent.push(json.id);
                        jQ('#' + refId).html(json.dt);
                        // TODO: Remove the refId from queue
                        //eventTran_detail.historyList();
                    } else {
                        jQ('#' + refId).html('<i class="fa fa-refresh"></i>');
                        jQ('#' + refId + ' i').bind('click', function() {
                            jQ('#' + refId).html('<i class="fa fa-spinner"></i>');
                            chat.chatSendProcess(refId, msg);
                        });
                    }
                } else {
                    // location.reload();
                }
            }
        };
        global.ajax(settings, 'chatAdd');
    },

};