/**
  * ******************************************************
  *                                                       *
  *   Copyright (C) Microsoft. All rights reserved.       *
  *                                                       *
  *******************************************************
  */
/// <reference path="../customTypings/vcremote-utils.d.ts" />
"use strict";
const utils = require("vcremote-utils");
const build = require("./build");
const project = require("./project");
const path = require("path");
const fs = require("fs");
const nconf = require("nconf");
var resources = utils.ResourcesManager;
var VCRemoteLib;
(function (VCRemoteLib) {
    // Will use /localization directory with translated strings if exists in npm installation, otherwise use default english strings.
    var pathToLocalizedStrings = path.join(__dirname + "../../../../", "localization/app/localization/i18n");
    if (fs.existsSync(pathToLocalizedStrings)) {
        resources.init(nconf.get("lang"), pathToLocalizedStrings);
    }
    else {
        resources.init(nconf.get("lang"), path.join(__dirname, "./resources"));
    }
    let ProjectCollectionError;
    (function (ProjectCollectionError) {
        ProjectCollectionError[ProjectCollectionError["OK"] = 0] = "OK";
        ProjectCollectionError[ProjectCollectionError["INCOMPATIBLE"] = 1] = "INCOMPATIBLE";
    })(ProjectCollectionError = VCRemoteLib.ProjectCollectionError || (VCRemoteLib.ProjectCollectionError = {}));
    let ProjectCollectionStatus;
    (function (ProjectCollectionStatus) {
        ProjectCollectionStatus[ProjectCollectionStatus["SUCCESS"] = 0] = "SUCCESS";
        ProjectCollectionStatus[ProjectCollectionStatus["FAILURE"] = 1] = "FAILURE";
    })(ProjectCollectionStatus = VCRemoteLib.ProjectCollectionStatus || (VCRemoteLib.ProjectCollectionStatus = {}));
    class ProjectCollection {
        get storeDirectory() {
            return this._storeDirectory;
        }
        get userDirectory() {
            return this._userDirectory;
        }
        get version() {
            return this._version;
        }
        get minimumSupportedClientVersion() {
            return this._minimumSupportedClientVersion;
        }
        get maximumSupportedClientVersion() {
            return this._maximumSupportedClientVersion;
        }
        get provisioningProfiles() {
            return this._provisioningProfiles;
        }
        constructor(storeDirectory) {
            this._projects = [];
            this._version = require("../package").version;
            this._minimumSupportedClientVersion = require("../package")._minimumSupportedClientVersion;
            this._maximumSupportedClientVersion = require("../package")._maximumSupportedClientVersion;
            this._storeDirectory = path.join(storeDirectory, "vc");
            this._storePersistenceFileName = path.join(this._storeDirectory, "store.json");
            utils.UtilHelper.createDirectoryIfNecessary(this._storeDirectory);
            this._userDirectory = process.env.HOME;
            this._provisioningProfiles = new build.ProvisioningProfiles();
            this.load();
        }
        contains(id) {
            return this.getProjectById(id) != undefined;
        }
        add(remoteProject) {
            console.info(resources.getStringForLanguage(nconf.get("lang"), "AddingProjectToCollection"));
            remoteProject.collection = this;
            var self = this;
            return remoteProject.retrieve().then(function (localProject) {
                console.info(resources.getStringForLanguage(nconf.get("lang"), "AddingProjectToCollectionWithDetails", localProject.id, localProject.directory));
                self._projects.push(localProject);
                return new Promise(function (resolve, reject) { resolve(localProject); });
            });
        }
        getProjectById(id) {
            for (var i = 0; i < this._projects.length; i++) {
                if (this._projects[i].id === id) {
                    return this._projects[i];
                }
            }
            return undefined;
        }
        close() {
        }
        cleanup() {
            for (var i = 0; i < this._projects.length; i++) {
                this._projects[i].deleteIfExpired();
            }
        }
        save() {
            var entries = [];
            for (var i = 0; i < this._projects.length; i++) {
                if (!this._projects[i].expired) {
                    entries.push(this._projects[i].serialize());
                }
            }
            fs.writeFileSync(this._storePersistenceFileName, JSON.stringify(entries, null, 4));
        }
        load() {
            if (fs.existsSync(this._storePersistenceFileName)) {
                var objects = JSON.parse(fs.readFileSync(this._storePersistenceFileName, "utf8"));
                for (var i = 0; i < objects.length; i++) {
                    var prj = new project.LocalSrcProject(this);
                    prj.deserialize(objects[i]);
                    if (!prj.expired) {
                        this._projects.push(prj);
                    }
                }
            }
        }
    }
    VCRemoteLib.ProjectCollection = ProjectCollection;
})(VCRemoteLib || (VCRemoteLib = {}));
module.exports = VCRemoteLib;
