var CHK_Dashboard = {};

CHK_Dashboard.pickupItems = [
    {id: 'newReport', href: "javascript:CHK_Dashboard.goPickup('newReport');", count: 0, enabled: true, img: {src: 'img/icon_new.svg'}, msg: 'newRegistrationTitle'},
    {id: 'continousWork', href: "javascript:CHK_Dashboard.goPickup('continousWork');", count: 0, enabled: true, img: {src: 'img/icon_proccess.svg'}, msg: 'continousWorkTitle'},
    {id: 'reportWarning', href: "javascript:CHK_Dashboard.goPickup('reportWarning');", count: 0, enabled: true, img: {src: 'img/icon_alert.svg'}, msg: 'reportWarningTitle'},
];
CHK_Dashboard.communicationItems = [
    {id: 'messageList', href: "javascript:CHK_Dashboard.goMessageList();", count: 0, enabled: true, img: {src: 'img/icon_message_lsit.svg'}, msg: 'messageListTitle'},
    {id: 'sendMessage', href: "javascript:CHK.sendAppCommand('goSendMessage');", count: 0, enabled: true, img: {src: 'img/icon_send_message.svg'}, msg: 'sendMessageTitle'},
    {id: 'distanceSupport', href: "javascript:CHK.sendAppCommand('goDistanceSupport');", count: 0, enabled: true, img: {src: 'img/icon_remote.svg'}, msg: 'distanceSupportTitle'},
    {id: 'chat', href: "javascript:CHK.sendAppCommand('goChat');", count: 0, enabled: false, img: {src: 'img/icon_chat.svg'}, msg: 'chatTitle'},
];

CHK_Dashboard.initPickups = function() {
    $("#pickupItems").empty();
    CHK_Dashboard.pickupItems.forEach(function(item) {
        if (item.enabled == true) {
            var html = CHK_Dashboard.initHtmlItem(item);
            $("#pickupItems").append(html);
        }
    });
    //pickup empty
    if ($("#pickupItems").children().length > 0) {
        $("#pickupHeader").removeClass('d-none');
    } else {
        $("#pickupHeader").addClass('d-none');
    }
}

CHK_Dashboard.initCommunications = function() {
    $("#communicationItems").empty();
    CHK_Dashboard.communicationItems.forEach(function(item) {
        if (item.enabled == true) {
            var html = CHK_Dashboard.initHtmlItem(item);
            $("#communicationItems").append(html);
        }
    });
}

CHK_Dashboard.initHtmlItem = function(item) {
    var countText = '' + item.count;
    if (item.count >= 100) {
        countText = '99+';
    }
    var countDClass = '';
    if (typeof item.count == 'undefined' || item.count < 1) {
        countDClass = ' d-none';
    }
    var countSpan = '<span class="count fs-7' + countDClass + '">' + countText + '</span>';
    var html = $('<div class="item" id="' + item.id + '">'
                + '<a href="' + item.href + '" class="d-block text-dark text-decoration-none mb-1 p-3">'
                + '<img src="' + item.img.src + '">'
                + '<div class="fs-8 multi-lang" data-msg="' + item.msg + '">' + getMsg(item.msg) + '</div>'
                + countSpan
                + '</a>'
                + '</div>');
    return html;
}

CHK_Dashboard.init = function() {
    CHK_Dashboard.updateDataPickups();
    CHK_Dashboard.initPickups();
    CHK_Dashboard.updateDataCommunications();
    CHK_Dashboard.initCommunications();
}

CHK_Dashboard.updateDataPickups = function() {
    CHK_Dashboard.pickupItems.forEach(function(item) {
        let enabled = CHK.dashboardSetting[item.id];
        if (enabled == true || enabled == false) {
            item.enabled = enabled;
        }
        item.count = 0;
        if (CHK.dashboardBatch[item.id]) {
            item.count = CHK.dashboardBatch[item.id];
        }
    });
}

CHK_Dashboard.updateDataCommunications = function() {
    CHK_Dashboard.communicationItems.forEach(function(item) {
        if (item.id == 'messageList') {
            item.count = 0;
            if (typeof CHK.pushMessageList !== 'undefined') {
                CHK.pushMessageList.forEach(function(message) {
                    if (!message.readingFlg) {
                        item.count += 1;
                    }
                });
            }
        } else if (item.id == 'chat') {
            item.enabled = CHK.isChat;
        }
    });
}

CHK_Dashboard.goPickup = function(pickupId) {
    CHK.goUrlWithCurrentParams('pickup.html', {pickupActive: pickupId});
}

CHK_Dashboard.goMessageList = function() {
    CHK.goUrlWithCurrentParams('message-list.html', {});
}