'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      * *******************************************************************************
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * Copyright (c) 2018 Edgeworx, Inc.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * This program and the accompanying materials are made available under the
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * terms of the Eclipse Public License v. 2.0 which is available at
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * http://www.eclipse.org/legal/epl-2.0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * SPDX-License-Identifier: EPL-2.0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  *******************************************************************************
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      */

/**
 * @file sslFileController.js
 * @author Zishan Iqbal
 * @description This file includes the CRUD operarions on ssl files.
 */

var _underscore = require('underscore');

var _underscore2 = _interopRequireDefault(_underscore);

var _appUtils = require('./appUtils');

var _appUtils2 = _interopRequireDefault(_appUtils);

var _ioauthoringConfigManager = require('./../managers/ioauthoringConfigManager');

var _ioauthoringConfigManager2 = _interopRequireDefault(_ioauthoringConfigManager);

var _constants = require('./../constants');

var _constants2 = _interopRequireDefault(_constants);

var _config = require('./../../config.json');

var _config2 = _interopRequireDefault(_config);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var ioauthoringConfigUtil = function () {
    function ioauthoringConfigUtil() {
        _classCallCheck(this, ioauthoringConfigUtil);
    }

    _createClass(ioauthoringConfigUtil, [{
        key: 'getConfigParam',
        value: function getConfigParam(key) {
            var configValue = void 0;

            if (!this.ioauthoringConfigs) {
                return undefined;
            } else {

                _underscore2.default.each(this.ioauthoringConfigs, function (config) {
                    if (config.key.toLowerCase() === key.toLowerCase()) {
                        configValue = config.value;
                        return;
                    }
                });
                return configValue;
            }
        }
    }, {
        key: 'setConfigParam',
        value: function setConfigParam(key, value) {
            if (this.isKeyExist(key)) {
                if (this.validateValue(key, value)) {
                    if (key == _constants2.default.CONFIG.port) {
                        _appUtils2.default.checkPortAvailability(value).then(function (availability) {
                            if (availability == 'closed') {
                                return _ioauthoringConfigManager2.default.setByKey(key, value).then(function (result) {
                                    console.log('"' + key + '" has been updated successfully.');
                                });
                            } else {
                                console.log('Port "' + value + '" is not available.');
                            }
                        });
                    } else {
                        return _ioauthoringConfigManager2.default.setByKey(key, value).then(function (result) {
                            console.log('"' + key + '" has been updated successfully.');
                        });
                    }
                } else {
                    throw 'Invalid value provided for key "' + key + '"';
                }
            } else {
                throw '"' + key + '" is not a valid property. You can set properties like port, fog_controller_ip_address, fog_controller_port, fog_controller_protocol etc';
            }
        }
    }, {
        key: 'isKeyExist',
        value: function isKeyExist(configKey) {
            return _underscore2.default.find(_constants2.default.CONFIG, function (value, key) {
                if (configKey.toLowerCase() == key.toLowerCase()) {
                    return true;
                }
            });
        }
    }, {
        key: 'validateValue',
        value: function validateValue(key, value) {
            if (key.toLowerCase() == _constants2.default.CONFIG.port || key.toLowerCase() == _constants2.default.CONFIG.fog_controller_port) {
                return _appUtils2.default.isValidPort(value);
                // let validPort = AppUtils.isValidPort(value);
                // return validPort && AppUtils.checkPortAvailability('127.0.0.1', value);
            } else if (key.toLowerCase() == _constants2.default.CONFIG.fog_controller_ip_address) {
                return _appUtils2.default.isValidPublicIP(value);
            } else if (key.toLowerCase() == _constants2.default.CONFIG.fog_controller_protocol) {
                return value == 'http' || value == 'https';
            } else if (key.toLowerCase() == _constants2.default.CONFIG.ssl_key || key.toLowerCase() == _constants2.default.CONFIG.ssl_cert || key.toLowerCase() == _constants2.default.CONFIG.intermediate_cert) {
                return _appUtils2.default.isFileExists(value);
            } else if (key.toLowerCase() == _constants2.default.CONFIG.self_signed_certs) {
                return value == 'true' || value == 'false';
            }
        }
    }, {
        key: 'getAllConfigs',
        value: function getAllConfigs() {
            return _ioauthoringConfigManager2.default.find().then(function (configs) {
                this.ioauthoringConfigs = configs;
                return configs;
            }.bind(this));
        }
    }]);

    return ioauthoringConfigUtil;
}();

var instance = new ioauthoringConfigUtil();
exports.default = instance;