import currencyConstants from '@/consts/currency.js';
import statisticsConstants from '@/consts/statistics.js';

const settingsLocalStorageKey = 'ebk_app_settings';

const defaultSettings = {
    theme: 'auto',
    fontSize: 1,
    timeZone: '',
    debug: false,
    applicationLock: false,
    applicationLockWebAuthn: false,
    autoUpdateExchangeRatesData: true,
    autoGetCurrentGeoLocation: false,
    thousandsSeparator: true,
    currencyDisplayMode: currencyConstants.defaultCurrencyDisplayMode,
    showAmountInHomePage: true,
    itemsCountInTransactionListPage: 15,
    showTotalAmountInTransactionListPage: true,
    showAccountBalance: true,
    statistics: {
        defaultChartType: statisticsConstants.defaultChartType,
        defaultChartDataType: statisticsConstants.defaultChartDataType,
        defaultDataRangeType: statisticsConstants.defaultDataRangeType,
        defaultAccountFilter: {},
        defaultTransactionCategoryFilter: {},
        defaultSortingType: statisticsConstants.defaultSortingType
    },
    animate: true
};

function getOriginalSettings() {
    try {
        const storageData = localStorage.getItem(settingsLocalStorageKey) || '{}';
        return JSON.parse(storageData);
    } catch (ex) {
        console.warn('settings in local storage is invalid', ex);
        return {};
    }
}

function getFinalSettings() {
    const originalSettings = getOriginalSettings();

    for (let key in originalSettings) {
        if (!Object.prototype.hasOwnProperty.call(originalSettings, key)) {
            continue;
        }

        if (typeof(defaultSettings[key]) === 'object') {
            originalSettings[key] = Object.assign({}, defaultSettings[key], originalSettings[key]);
        }
    }

    return Object.assign({}, defaultSettings, originalSettings);
}

function setSettings(settings) {
    const storageData = JSON.stringify(settings);
    return localStorage.setItem(settingsLocalStorageKey, storageData);
}

function getOption(key) {
    return getFinalSettings()[key];
}

function getSubOption(key, subKey) {
    const options = getFinalSettings()[key] || {};
    return options[subKey];
}

function setOption(key, value) {
    if (!Object.prototype.hasOwnProperty.call(defaultSettings, key)) {
        return;
    }

    const settings = getFinalSettings();
    settings[key] = value;

    return setSettings(settings);
}

function setSubOption(key, subKey, value) {
    if (!Object.prototype.hasOwnProperty.call(defaultSettings, key)) {
        return;
    }

    if (!Object.prototype.hasOwnProperty.call(defaultSettings[key], subKey)) {
        return;
    }

    const settings = getFinalSettings();
    let options = settings[key];

    if (!options) {
        options = {};
    }

    options[subKey] = value;
    settings[key] = options;

    return setSettings(settings);
}

export function isEnableDebug() {
    return getOption('debug');
}

export function getTheme() {
    return getOption('theme');
}

export function setTheme(value) {
    return setOption('theme', value);
}

export function getFontSize() {
    return getOption('fontSize');
}

export function setFontSize(value) {
    return setOption('fontSize', value);
}

export function getTimeZone() {
    return getOption('timeZone');
}

export function setTimeZone(value) {
    return setOption('timeZone', value);
}

export function isEnableApplicationLock() {
    return getOption('applicationLock');
}

export function setEnableApplicationLock(value) {
    return setOption('applicationLock', value);
}

export function isEnableApplicationLockWebAuthn() {
    return getOption('applicationLockWebAuthn');
}

export function setEnableApplicationLockWebAuthn(value) {
    return setOption('applicationLockWebAuthn', value);
}

export function isAutoUpdateExchangeRatesData() {
    return getOption('autoUpdateExchangeRatesData');
}

export function setAutoUpdateExchangeRatesData(value) {
    setOption('autoUpdateExchangeRatesData', value);
}

export function isAutoGetCurrentGeoLocation() {
    return getOption('autoGetCurrentGeoLocation');
}

export function setAutoGetCurrentGeoLocation(value) {
    setOption('autoGetCurrentGeoLocation', value);
}

export function isEnableThousandsSeparator() {
    return getOption('thousandsSeparator');
}

export function setEnableThousandsSeparator(value) {
    setOption('thousandsSeparator', value);
}

export function getCurrencyDisplayMode() {
    return getOption('currencyDisplayMode');
}

export function setCurrencyDisplayMode(value) {
    setOption('currencyDisplayMode', value);
}

export function isShowAmountInHomePage() {
    return getOption('showAmountInHomePage');
}

export function setShowAmountInHomePage(value) {
    setOption('showAmountInHomePage', value);
}

export function getItemsCountInTransactionListPage() {
    return getOption('itemsCountInTransactionListPage');
}

export function setItemsCountInTransactionListPage(value) {
    setOption('itemsCountInTransactionListPage', value);
}

export function isShowTotalAmountInTransactionListPage() {
    return getOption('showTotalAmountInTransactionListPage');
}

export function setShowTotalAmountInTransactionListPage(value) {
    setOption('showTotalAmountInTransactionListPage', value);
}

export function isShowAccountBalance() {
    return getOption('showAccountBalance');
}

export function setShowAccountBalance(value) {
    setOption('showAccountBalance', value);
}

export function getStatisticsDefaultChartType() {
    return getSubOption('statistics', 'defaultChartType');
}

export function setStatisticsDefaultChartType(value) {
    setSubOption('statistics', 'defaultChartType', value);
}

export function getStatisticsDefaultChartDataType() {
    return getSubOption('statistics', 'defaultChartDataType');
}

export function setStatisticsDefaultChartDataType(value) {
    setSubOption('statistics', 'defaultChartDataType', value);
}

export function getStatisticsDefaultDateRange() {
    return getSubOption('statistics', 'defaultDataRangeType');
}

export function setStatisticsDefaultDateRange(value) {
    setSubOption('statistics', 'defaultDataRangeType', value);
}

export function getStatisticsDefaultAccountFilter() {
    return getSubOption('statistics', 'defaultAccountFilter');
}

export function setStatisticsDefaultAccountFilter(value) {
    setSubOption('statistics', 'defaultAccountFilter', value);
}

export function getStatisticsDefaultTransactionCategoryFilter() {
    return getSubOption('statistics', 'defaultTransactionCategoryFilter');
}

export function setStatisticsDefaultTransactionCategoryFilter(value) {
    setSubOption('statistics', 'defaultTransactionCategoryFilter', value);
}

export function getStatisticsSortingType() {
    return getSubOption('statistics', 'defaultSortingType');
}

export function setStatisticsSortingType(value) {
    setSubOption('statistics', 'defaultSortingType', value);
}

export function isEnableAnimate() {
    return getOption('animate');
}

export function setEnableAnimate(value) {
    return setOption('animate', value);
}

export function clearSettings() {
    localStorage.removeItem(settingsLocalStorageKey);
}
