/**
 * Dashboard Setting js in dashboard.html
 *
 * @since 1.0 check web
 */
var DashboardSetting = {};

DashboardSetting.changeSettingCallback = function() {};

/**
 * Html element array, map with json key from setting data API
 */
DashboardSetting.elementItems = {
    newReport: { id: 'settingNewReport', name: 'chk-new', enabled: true },
    continousWork: { id: 'settingContinousWork', name: 'chk-proccess', enabled: true },
    warningReport: { id: 'settingWarningReport', name: 'chk-alert', enabled: true },
    dashboardHome: { id: 'settingDashboardHome', name: 'chk-home', enabled: false },
};

DashboardSetting.baseApiUrl = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.DASHBOARD_SETTING;

/**
 * default setting
 */
DashboardSetting.defaultSetting = {
    newReport: 1,
    continousWork: 1,
    warningReport: 1,
    dashboardHome: 0,
}

/** dummy setting json reponse */
DashboardSetting.dummySettingJson = {
    dashboardSettingList: [
        {
            "id": 1,
            "settingName": "New Report",
            "settingValue": 1
        },
        {
            "id": 2,
            "settingName": "Continous Work",
            "settingValue": 1
        },
        {
            "id": 3,
            "settingName": "Warning Report",
            "settingValue": 1
        },
        {
            "id": 4,
            "settingName": "Dashboard Home",
            "settingValue": 0
        }
    ],
}

/**
 * get setting data from cms
 * @param {function} callback - callback with settings
 */
DashboardSetting.getSettingData = function (callback) {
    let param = {
        sid: COMMON.getSid(),
    };
    const url = DashboardSetting.baseApiUrl;
    COMMON.cmsAjax(url, param, false, function (json) {
        let settings = {};
        //map id with key when save setting
        const mapKeys = {
            1: 'newReport',
            2: 'continousWork',
            3: 'warningReport',
            4: 'dashboardHome'
        };
        if (json && json.dashboardSettingList) {
            for (const item of json.dashboardSettingList) {
                if (mapKeys.hasOwnProperty(item.id)) {
                    const settingKey = mapKeys[item.id];
                    settings[settingKey] = item.settingValue;
                }
            }
        }
        if (callback) {
            callback(settings);
        }
    }, function() {
        console.log('DashboardSetting.getSettingData error');
    });
};

/**
 * apply settings to screen
 * @param {JSON} settings 
 */
DashboardSetting.applySettings = function (settings) {
    for (const key in settings) {
        const enabled = settings[key];
        if (DashboardSetting.elementItems.hasOwnProperty(key)) {
            //apply enabled, disabled setting
            let elementItem = DashboardSetting.elementItems[key];
            elementItem.enabled = enabled;
            let element = $('#' + elementItem.id);
            if (element) {
                if (enabled) {
                    element.addClass('checked');
                } else {
                    element.removeClass('checked');
                }
            }
        }
    }
};

/**
 * post request to save setting to cms
 */
DashboardSetting.saveSetting = function () {
    let param = {};
    param.sid = COMMON.getSid();
    for (const key in DashboardSetting.elementItems) {
        const element = DashboardSetting.elementItems[key];
        param[key] = element.enabled ? 1 : 0;
    }
    const url = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.SAVE_DASHBOARD_SETTING;
    COMMON.cmsAjax(url, param, false, function (json) {
        DashboardSetting.closeModal();
        if (DashboardSetting.changeSettingCallback && typeof DashboardSetting.changeSettingCallback === 'function') {
            DashboardSetting.changeSettingCallback();
        }
    }, function() {
        console.log('DashboardSetting.saveSetting error');
        DashboardSetting.closeModal();
    });
};

/**
 * handle on/off setting
 */
DashboardSetting.bindToggleClick = function () {
    $('.toggle').on('click', function () {
        $(this).toggleClass('checked');
        const key = $(this).attr('data-key');
        let enabled = false;
        if ($(this).hasClass('checked')) {
            enabled = true;
        }
        if (DashboardSetting.elementItems.hasOwnProperty(key)) {
            let elementItem = DashboardSetting.elementItems[key];
            elementItem.enabled = enabled;
        }
    });
};

/**
 * init data, action when screen onload
 * @param {function} changeCallback - callback when settings changed
 */
DashboardSetting.init = function (changeCallback) {
    DashboardSetting.getSettingData(function (settings) {
        DashboardSetting.applySettings(settings);
    });
    DashboardSetting.bindToggleClick();
    DashboardSetting.changeSettingCallback = changeCallback;
};

/**
 * handle click event of save button
 */
DashboardSetting.onClickSave = function () {
    DashboardSetting.saveSetting();
};

/**
 * close setting dialog
 */
DashboardSetting.closeModal = function () {
    $('#dashboard-setting-modal .close').click();
};
