/**
* ******************************************************
*                                                       *
*   Copyright (C) Microsoft. All rights reserved.       *
*                                                       *
*******************************************************
*/
/// <reference path="../../typings/node.d.ts" />
/// <reference path="../../typings/Q.d.ts" />
/// <reference path="../../typings/vcremote-utils.d.ts" />
/// <reference path="../../typings/vcremote-lib.d.ts" />
/// <reference path="../../typings/express.d.ts" />
/// <reference path="../../typings/semver.d.ts" />
"use strict";
var utils = require("vcremote-utils");

var q = require("q");
var build = require("./build");
var project = require("./project");
var path = require("path");
var fs = require("fs");

var VCRemoteLib;
(function (VCRemoteLib) {
    (function (ProjectCollectionError) {
        ProjectCollectionError[ProjectCollectionError["OK"] = 0] = "OK";
        ProjectCollectionError[ProjectCollectionError["INCOMPATIBLE"] = 1] = "INCOMPATIBLE";
    })(VCRemoteLib.ProjectCollectionError || (VCRemoteLib.ProjectCollectionError = {}));
    var ProjectCollectionError = VCRemoteLib.ProjectCollectionError;

    (function (ProjectCollectionStatus) {
        ProjectCollectionStatus[ProjectCollectionStatus["SUCCESS"] = 0] = "SUCCESS";
        ProjectCollectionStatus[ProjectCollectionStatus["FAILURE"] = 1] = "FAILURE";
    })(VCRemoteLib.ProjectCollectionStatus || (VCRemoteLib.ProjectCollectionStatus = {}));
    var ProjectCollectionStatus = VCRemoteLib.ProjectCollectionStatus;

    var ProjectCollection = (function () {
        function ProjectCollection(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();
        }
        Object.defineProperty(ProjectCollection.prototype, "storeDirectory", {
            get: function () {
                return this._storeDirectory;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ProjectCollection.prototype, "userDirectory", {
            get: function () {
                return this._userDirectory;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ProjectCollection.prototype, "version", {
            get: function () {
                return this._version;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ProjectCollection.prototype, "minimumSupportedClientVersion", {
            get: function () {
                return this._minimumSupportedClientVersion;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ProjectCollection.prototype, "maximumSupportedClientVersion", {
            get: function () {
                return this._maximumSupportedClientVersion;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ProjectCollection.prototype, "provisioningProfiles", {
            get: function () {
                return this._provisioningProfiles;
            },
            enumerable: true,
            configurable: true
        });

        ProjectCollection.prototype.contains = function (id) {
            return this.getProjectById(id) != undefined;
        };

        ProjectCollection.prototype.add = function (remoteProject) {
            console.info("Adding new project in the collection.");
            remoteProject.collection = this;
            var self = this;
            return remoteProject.retrieve().then(function (localProject) {
                console.info("Added new project in the collection, project id: %s, project directory: %s", localProject.id, localProject.directory);
                self._projects.push(localProject);
                return q(localProject);
            });
        };

        ProjectCollection.prototype.getProjectById = function (id) {
            for (var i = 0; i < this._projects.length; i++) {
                if (this._projects[i].id === id) {
                    return this._projects[i];
                }
            }
            return undefined;
        };

        ProjectCollection.prototype.close = function () {
        };

        ProjectCollection.prototype.cleanup = function () {
            for (var i = 0; i < this._projects.length; i++) {
                this._projects[i].deleteIfExpired();
            }
        };

        ProjectCollection.prototype.save = function () {
            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));
        };

        ProjectCollection.prototype.load = function () {
            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);
                    }
                }
            }
        };
        return ProjectCollection;
    })();
    VCRemoteLib.ProjectCollection = ProjectCollection;
})(VCRemoteLib || (VCRemoteLib = {}));

module.exports = VCRemoteLib;
