import { ApiService } from "./apiService.js";

export class ClientStorage {
    constructor() {
        this.time = 3000;
        this.changed = false;
        this.locked = false;
        this.removeItem = null;
        this.init();
    }
    init() {
        this.interval = setInterval(function () {
            if (this.changed && !this.locked) {
                this.locked = true;
                this.sendToServer();
            }
        }.bind(this), this.time);
    }
    setSetting(items) {
        var settings = this.getAllSetting();
        if (!settings || settings === null)
            settings = [];
        for (var i = 0; i < items.length; i++) {
            if (_h.any(settings)) {
                var item = settings.filter(a => a.key === items[i].key);
                if (item.length > 0) {
                    item[0].value = items[i].value
                    item[0].changed = true;
                }
                else {
                    items[i].changed = true;
                    settings.push(items[i]);
                }
            }
            else {
                items[i].changed = true;
                settings.push(items[i]);
            }
        }
        this.set("sts", JSON.stringify(settings));
        this.changed = true;
    }
    getSetting(key) {
        try {
            var item
            var settings = this.getAllSetting();
            item = settings.filter(a => a.key === key)[0];
            return item.value;
        } catch (e) {
            return undefined;
        }
    }
    get(key) {
        try {
            return localStorage.getItem(key);
        } catch (e) {
            return undefined;
        }
    }
    set(key, value) {
        try {
            localStorage.setItem(key, value);
            return true;
        } catch (e) {
            return false;
        }
    }
    sendToServer() {
        if (!this.removeItem) {
            var settings = this.getAllSetting();
            if (settings && settings.length > 0) {
                settings = settings.filter(a => a.changed);
            }

            if (_h.isEmpty(settings) || settings.length === 0)
                return;
            var model = {
                settings: settings
            };
            ApiService.request(
                {
                    url: "/api/common/postsetting",
                    data: model,
                    notShowAjaxLoader: true,
                    success: function (data) {
                        this.changed = false;
                        this.locked = false;
                        this.resetChangedItems();

                    }.bind(this),
                    error: function (data) {
                        this.locked = false;
                    }.bind(this)
                });
        }
        else {
            var model = {
                list: [this.removeItem.key]
            };
            ApiService.request(
                {
                    url: "/api/common/deletesetting",
                    data: model,
                    success: function (data) {
                        this.changed = false;
                        this.locked = false;
                        this.removeItem = null;

                    }.bind(this),
                    error: function (data) {
                        this.locked = false;
                    }
                });
        }

    }
    removeSetting(key) {
        var settings = this.getAllSetting();
        var newList = [];
        var item = null;
        if (settings && settings.length > 0) {
            for (var i = 0; i < settings.length; i++) {
                if (key !== settings[i].key) {
                    newList.push(settings[i]);
                } else {
                    item = settings[i];
                }
            }
            if (item) {
                this.set("sts", JSON.stringify(newList));
                this.removeItem = item;
                this.changed = true;
            }
        }
    }
    getAllSetting() {
        var text = this.get("sts");
        if (!_h.isEmpty(text)) {
            return JSON.parse(text);
        }
    }
    resetChangedItems() {
        var settings = this.getAllSetting();
        if (settings && settings.length > 0) {
            for (var i = 0; i < settings.length; i++) {
                delete settings[i]["changed"];
            }
            this.set("sts", JSON.stringify(settings));
        }
    }
}