var uniflash;
(function (uniflash) {
    var preferences;
    (function (preferences) {
        'use strict';
        //auto save and always read from file upon access. 
        var Preferences = (function () {
            function Preferences($window, $q, $log, ticFileStorage, ticFileFactory) {
                var _this = this;
                this.$window = $window;
                this.$q = $q;
                this.$log = $log;
                this.ticFileStorage = ticFileStorage;
                this.ticFileFactory = ticFileFactory;
                this.preferencesMap = {};
                this.filePath = 'preferences.json';
                this.lastOpPromise = null;
                this.recentSessionsId = 'recent-session-files';
                this.ticFileStorage.getAppDataDirectory()
                    .then(function (parentPath) {
                    _this.filePath = parentPath + '/' + _this.filePath;
                    _this.load();
                });
                this.lastOpPromise = this.load();
                this.$window.onbeforeunload = this.load();
            }
            Preferences.prototype.load = function () {
                var _this = this;
                return this.ticFileStorage.fileExists(this.filePath)
                    .then(function (isExists) {
                    if (isExists) {
                        return _this.ticFileStorage.openFile(_this.filePath) //text
                            .then(function (data) {
                            _this.preferencesMap = JSON.parse(data);
                            return _this.getRecentSessions();
                        })
                            .catch(function (err) {
                            _this.$log.error('Preferences get err: ' + err + ', using default.');
                        });
                    }
                    else {
                        _this.preferencesMap = _this.getDefault();
                    }
                });
            };
            Preferences.prototype.get = function (id) {
                return this.preferencesMap[id];
            };
            Preferences.prototype.set = function (id, val) {
                var _this = this;
                this.preferencesMap[id] = val;
                return this.lastOpPromise
                    .then(function () {
                    _this.lastOpPromise = _this.ticFileStorage.saveFile(_this.filePath, JSON.stringify(_this.preferencesMap), ti_common.file.ContentType.Text);
                    return _this.lastOpPromise;
                });
            };
            Preferences.prototype.getDefault = function () {
                return {
                    'Auto Disconnect': true,
                    'runTargetAfterProgramLoad': true,
                    'runTargetAfterProgramLoadC2000': false,
                    'showDetectWelcomePage': true,
                };
            };
            Preferences.prototype.purgeRecentSessions = function () {
                return this.set(this.recentSessionsId, []);
            };
            Preferences.prototype.pushRecentSessionFile = function (file) {
                var _this = this;
                var isDuplicate = false;
                var listOfSessions;
                return this.getRecentSessions()
                    .then(function (list) {
                    listOfSessions = list;
                    return angular.forEach(listOfSessions, function (sessionFile) {
                        if (sessionFile.path === file.path) {
                            isDuplicate = true;
                        }
                    });
                })
                    .then(function () {
                    if (isDuplicate) {
                        return;
                    }
                    if (listOfSessions.length > 5) {
                        listOfSessions.shift();
                    }
                    listOfSessions.push(file.toJSON());
                    return _this.set(_this.recentSessionsId, listOfSessions);
                });
            };
            Preferences.prototype.getRecentSessions = function () {
                var _this = this;
                var listOfSessions = this.get(this.recentSessionsId);
                var listOfExistingSessions = [];
                var promiseList = [];
                //only keep the files that still exist
                angular.forEach(listOfSessions, function (sessionFileJson) {
                    promiseList.push(_this.ticFileStorage.fileExists(sessionFileJson.path)
                        .then(function (isExists) {
                        if (isExists) {
                            return _this.ticFileFactory.createFromJSON(sessionFileJson);
                        }
                    })
                        .then(function (file) {
                        if (file) {
                            listOfExistingSessions.push(file);
                        }
                    }));
                });
                return this.$q.all(promiseList)
                    .then(function () {
                    return listOfExistingSessions;
                });
            };
            Preferences.$inject = ['$window', '$q', '$log', 'ticFileStorage', 'ticFileFactory'];
            return Preferences;
        }());
        preferences.Preferences = Preferences;
        ;
        angular.module('uniflash').service('ufPreferencesSrv', Preferences);
    })(preferences = uniflash.preferences || (uniflash.preferences = {}));
})(uniflash || (uniflash = {}));
