(function () {
    'use strict';
    angular.module('uniflash').service('ufQuickSettingsSrv', ufQuickSettingsSrv);
    ufQuickSettingsSrv.$inject = ['$log', 'ufSession', 'ufPropertiesValue', 'ufContext', 'ticAnalyticsSrv'];
    function ufQuickSettingsSrv($log, ufSession, ufPropertiesValue, ufContext, ticAnalyticsSrv) {
        var pinnedList = [];
        var coreList = [];
        var showPinSetting = false;
        function checkListForCurrentCore() {
            if (!pinnedList[ufContext.getCurrentCore().name]) {
                pinnedList[ufContext.getCurrentCore().name] = [];
                coreList.push(ufContext.getCurrentCore().name);
            }
        }
        function getPinnedList() {
            checkListForCurrentCore();
            return pinnedList[ufContext.getCurrentCore().name];
        }
        function added(id, type) {
            checkListForCurrentCore();
            return _.some(pinnedList[ufContext.getCurrentCore().name], function (entry) {
                return (entry.id === id) && (entry.type === type);
            });
        }
        function addEntry(id, type) {
            checkListForCurrentCore();
            ticAnalyticsSrv.record("AddQuickSetting", {
                "id": id,
                "type": type,
                "core": ufContext.getCurrentCore().name
            });
            $log.debug('AddQuickSetting. id: ' + id + ', type: ' + type + ', core: ' + ufContext.getCurrentCore().name);
            if (!added(id, type)) {
                if (type === 'property') {
                    pinnedList[ufContext.getCurrentCore().name].push({ 'id': id, 'type': 'property' });
                }
                else if (type === 'category') {
                    pinnedList[ufContext.getCurrentCore().name].push({ 'id': id, 'type': 'category' });
                }
            }
        }
        function remove(index) {
            checkListForCurrentCore();
            pinnedList[ufContext.getCurrentCore().name].splice(index, 1);
        }
        function removeAll() {
            checkListForCurrentCore();
            pinnedList[ufContext.getCurrentCore().name] = [];
        }
        function moveEntryUp(index) {
            var curPinList = pinnedList[ufContext.getCurrentCore().name];
            if ((index - 1) >= 0) {
                var temp = curPinList[index];
                curPinList[index] = curPinList[index - 1];
                curPinList[index - 1] = temp;
            }
        }
        function moveEntryDown(index) {
            var curPinList = pinnedList[ufContext.getCurrentCore().name];
            if ((index + 1) <= (curPinList.length - 1)) {
                var temp = curPinList[index];
                curPinList[index] = curPinList[index + 1];
                curPinList[index + 1] = temp;
            }
        }
        function getIndex(id, type) {
            var curPinList = pinnedList[ufContext.getCurrentCore().name];
            if (curPinList) {
                for (var index = 0; index < curPinList.length; index++) {
                    var curPinEntry = curPinList[index];
                    if ((curPinEntry.id === id) && (curPinEntry.type === type)) {
                        return index;
                    }
                }
            }
        }
        /* Property Values load/save session */
        ufSession.registerSessionDataProvider('propertyValues', {
            onSessionLoad: function (newData) {
                if (newData) {
                    ufPropertiesValue.setPropertiesValueList(newData);
                }
            },
            getSessionData: function () {
                return ufPropertiesValue.getPropertiesValueList();
            }
        });
        /* Quick Settings load/save session */
        ufSession.registerSessionDataProvider('quickSettings', {
            onSessionLoad: function (newData) {
                pinnedList = [];
                coreList = [];
                if (newData) {
                    _.each(newData, function (coreData) {
                        coreList.push(coreData.core);
                        pinnedList[coreData.core] = coreData.pinnedList;
                    });
                }
            },
            getSessionData: function () {
                var toJSON = [];
                _.each(ufSession.queryConfig().cores, function (core) {
                    toJSON.push({
                        core: core.name,
                        pinnedList: pinnedList[core.name]
                    });
                });
                return toJSON;
            }
        });
        return {
            getPinnedList: getPinnedList,
            showPinSetting: showPinSetting,
            addEntry: addEntry,
            added: added,
            remove: remove,
            removeAll: removeAll,
            moveEntryUp: moveEntryUp,
            moveEntryDown: moveEntryDown,
            getIndex: getIndex
        };
    }
})();
