'use strict';

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

var _configUtil = require('../utils/configUtil');

var _configUtil2 = _interopRequireDefault(_configUtil);

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

var _config2 = _interopRequireDefault(_config);

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

var _constants2 = _interopRequireDefault(_constants);

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

/*
 * *******************************************************************************
 *  * 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
 *  *******************************************************************************
 *
 */

var request = require('request');
var fs = require("fs");


var getFogControllerConfigs = function getFogControllerConfigs() {
  return _configUtil2.default.getAllConfigs().then(function () {
    return {
      ipAddress: _configUtil2.default.getConfigParam(_constants2.default.CONFIG.fog_controller_ip_address) || _config2.default.fogControllerIPAddress,
      port: _configUtil2.default.getConfigParam(_constants2.default.CONFIG.fog_controller_port) || _config2.default.fogControllerPort,
      protocol: _configUtil2.default.getConfigParam(_constants2.default.CONFIG.fog_controller_protocol) || _config2.default.fogControllerProtocol,
      ca: _configUtil2.default.getConfigParam(_constants2.default.CONFIG.intermediate_cert),
      selfSignedCerts: _configUtil2.default.getConfigParam(_constants2.default.CONFIG.self_signed_certs)
    };
  });
};

var authenticateUser = function authenticateUser(req, res, next) {
  getFogControllerConfigs().then(function (configs) {
    var protocol = configs.protocol,
        ipAddress = configs.ipAddress,
        port = configs.port,
        ca = configs.ca,
        selfSignedCerts = configs.selfSignedCerts;

    var options = {
      url: protocol + '://' + ipAddress + ':' + port + '/api/v2/user/authenticate/' + req.cookies.apwAccessToken
    };
    if (ca && selfSignedCerts === 'true') {
      options.ca = fs.readFileSync(ca);
    }
    request(options, function (error, response, body) {
      error ? res.redirect('/login') : next();
    });
  });
};

/*
	calls external service for microservice json
 */
var importMicroserviceEndPoint = function importMicroserviceEndPoint(req, res) {
  request(req.query.url, function (error, response, body) {
    if (error) {
      res.status(400).send(error);
    } else {
      res.status(200).json(JSON.parse(body));
    }
  });
};

var loginPage = function loginPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    var protocol = configs.protocol,
        ipAddress = configs.ipAddress,
        port = configs.port,
        ca = configs.ca,
        selfSignedCerts = configs.selfSignedCerts;

    var options = {
      url: protocol + '://' + ipAddress + ':' + port + '/api/v2/emailActivation'
    };
    if (ca && selfSignedCerts === 'true') {
      options.ca = fs.readFileSync(ca);
    }
    request(options, function (error, response, body) {
      var emailActivationFlag = error ? 'off' : JSON.parse(body).emailActivation;
      res.render("login/base", {
        title: 'Login to iofog',
        metaDescription: "User login page for iofog software",
        metaCacheControl: "",
        metaPragma: "",
        metaKeywords: "",
        metaAuthor: "Edgeworks, Inc.",
        metaCopyright: "2018 Edgeworks, Inc.",
        metaRobotDirective: "",
        linkCanonical: "https://iofog.org/login",
        fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port,
        emailActivation: emailActivationFlag
      });
    });
  });
};

var logoutEndPoint = function logoutEndPoint(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("logout/base", {
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var userSignupPage = function userSignupPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("login/userSignup", {
      title: 'Signup for iofog',
      metaDescription: "User signup page for iofog software",
      metaCacheControl: "",
      metaPragma: "",
      metaKeywords: "",
      metaAuthor: "Edgeworks, Inc.",
      metaCopyright: "2018 Edgeworks, Inc.",
      metaRobotDirective: "",
      linkCanonical: "https://iofog.org/signup",
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var updateProfilePage = function updateProfilePage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("account/updateProfile", {
      title: 'Update iofog profile',
      metaDescription: "Update profile for iofog user",
      metaCacheControl: "",
      metaPragma: "",
      metaKeywords: "",
      metaAuthor: "Edgeworks, Inc.",
      metaCopyright: "2018 Edgeworks, Inc.",
      metaRobotDirective: "",
      linkCanonical: "https://iofog.org/profile/update",
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var resetUserPasswordPage = function resetUserPasswordPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("login/resetPassword", {
      title: 'Forgot password',
      metaDescription: "Password reset page for iofog software",
      metaCacheControl: "",
      metaPragma: "",
      metaKeywords: "",
      metaAuthor: "Edgeworks, Inc.",
      metaCopyright: "2018 Edgeworks, Inc.",
      metaRobotDirective: "",
      linkCanonical: "https://iofog.org/password/reset",
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var updateUserPasswordPage = function updateUserPasswordPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("account/updatePassword", {
      title: 'Change iofog password',
      metaDescription: "Change password for iofog software",
      metaCacheControl: "",
      metaPragma: "",
      metaKeywords: "",
      metaAuthor: "Edgeworks, Inc.",
      metaCopyright: "2018 Edgeworks, Inc.",
      metaRobotDirective: "",
      linkCanonical: "https://iofog.org/password/change",
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var deleteUserAccountPage = function deleteUserAccountPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("account/deleteUserAccount", {
      title: 'Delete iofog account',
      metaDescription: "Delete account for iofog user",
      metaCacheControl: "",
      metaPragma: "",
      metaKeywords: "",
      metaAuthor: "Edgeworks, Inc.",
      metaCopyright: "2018 Edgeworks, Inc.",
      metaRobotDirective: "",
      linkCanonical: "https://iofog.org/account/delete",
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var resendActivationEmailPage = function resendActivationEmailPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("login/resendActivationEmail", {
      title: 'Resend activation',
      metaDescription: "Resend activation email page for iofog software",
      metaCacheControl: "",
      metaPragma: "",
      metaKeywords: "",
      metaAuthor: "Edgeworks, Inc.",
      metaCopyright: "2018 Edgeworks, Inc.",
      metaRobotDirective: "",
      linkCanonical: "https://iofog.org/account/activate/resend",
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var activateAccountPage = function activateAccountPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("account/activateUserAccount", {
      title: 'Activate account',
      metaDescription: "Account activation page for iofog software",
      metaCacheControl: "",
      metaPragma: "",
      metaKeywords: "",
      metaAuthor: "Edgeworks, Inc.",
      metaCopyright: "2018 Edgeworks, Inc.",
      metaRobotDirective: "",
      linkCanonical: "https://iofog.org/account/activate",
      code: req.params.code,
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var buildPage = function buildPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("build/base", {
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var publishingPage = function publishingPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("publish/base", {
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};

var fogPage = function fogPage(req, res) {
  getFogControllerConfigs().then(function (configs) {
    res.render("fog/base", {
      fogControllerUrl: configs.protocol + '://' + configs.ipAddress + ':' + configs.port
    });
  });
};
var accountPage = function accountPage(req, res) {
  res.render("account/base");
};

exports.default = {
  authenticateUser: authenticateUser,
  activateAccountPage: activateAccountPage,
  loginPage: loginPage,
  logoutEndPoint: logoutEndPoint,
  importMicroserviceEndPoint: importMicroserviceEndPoint,
  resetUserPasswordPage: resetUserPasswordPage,
  resendActivationEmailPage: resendActivationEmailPage,
  userSignupPage: userSignupPage,
  buildPage: buildPage,
  publishingPage: publishingPage,
  fogPage: fogPage,
  accountPage: accountPage,
  updateProfilePage: updateProfilePage,
  updateUserPasswordPage: updateUserPasswordPage,
  deleteUserAccountPage: deleteUserAccountPage
};