'use strict';

const router = require('express').Router();

const configUtils = require('../../csdr/config/config-utils');
const metadataUtils = require('../../csdr/metadata/metadata-utils');
const cliUtils = require('../../csdr/cli/cli-utils');
const tools = require('../../utils/tools');


const handlerError = (res, e) => {
  console.log(e);
  res.status(500);
  res.send({ error: e });
}

router.get('/remotes', function (req, res) {
  return Promise.resolve()
    .then(() => {
      return [
      ...configUtils.remotes.getCsdrRemotesArray('my-workplace', '15.x'),
      ...configUtils.remotes.getCsdrRemotesArray('my-workplace', '16.x'),
      ];
    })
    .then((fetchedRemotes) => {
      let remotes = tools.sortArray(fetchedRemotes, 'name');
      remotes = remotes.filter((r) => {
        let participant = false;
        if (r.fullSkeletonSources) {
          if (r.skeletonConfig.options && r.skeletonConfig.options.participant) {
            participant = true;
          }
        }

        if (r.fullSkeletonSources && !participant) {
          return true;
        }
      })
      res.send(remotes);
    })
    .catch((e) => {
      handlerError(res, e);
    })
});

router.get('/remotesForConfig', function (req, res) {
  return Promise.resolve()
    .then(() => {
      return [
      ...configUtils.remotes.getCsdrRemotesArray('my-workplace', '16.x'),
      ];
    })
    .then((fetchedRemotes) => {
      let remotes = tools.sortArray(fetchedRemotes, 'name');
      remotes = remotes.filter((r) => {
        let participant = false;
        if (r.fullSkeletonSources) {
          if (r.skeletonConfig.options && r.skeletonConfig.options.participant) {
            participant = true;
          }
        }

        if (r.fullSkeletonSources && !participant) {
          return true;
        }
      })
      res.send(remotes);
    })
    .catch((e) => {
      handlerError(res, e);
    })
});

router.post('/remotes/packages', function (req, res) {
  return Promise.resolve()
    .then(() => {
      const remotes = req.body;
      const depsPackages = configUtils.remotes.getRemotesPackages(remotes);
      res.send(depsPackages);
    })
    .catch((e) => {
      handlerError(res, e);
    })
});

router.get('/config', function (req, res) {
  return Promise.resolve()
    .then(() => {
      return configUtils.global.getCustomConfig();
    })
    .then((customConfig) => {
      res.send(customConfig);
    })
    .catch((e) => {
      handlerError(res, e);
    })
});

router.post('/config', function (req, res) {
  return Promise.resolve()
    .then(() => {
      return configUtils.global.generateCustomConfig(req.body);
    })
    .then(() => {
      res.status(200);
      res.send({ status: 'OK'})
    })
    .catch((e) => {
      handlerError(res, e);
    })
});

router.get('/remotes/stats', function (req, res) {
  return Promise.resolve()
    .then(() => {
      return metadataUtils.stats.getRemoteAllStats();
    })
    .then((stats) => {
      res.send(stats);
    })
    .catch((e) => {
      handlerError(res, e);
    })
});

router.post('/remotes/stats', function (req, res) {
  return Promise.resolve()
    .then(() => {
      return metadataUtils.stats.getRemoteStats(req.body.remote);
    })
    .then((remoteStats) => {
      res.send(remoteStats);
    })
    .catch((e) => {
      handlerError(res, e);
    })
});

router.post('/package-init', function (req, res) {
  return Promise.resolve()
    .then(() => {
      const params = req.body.params;
      return cliUtils.package.generateExternal(params);
    })
    .then(() => {
      res.status(200);
      res.send({ status: 'OK'})
    })
    .catch((e) => {
      handlerError(res, e);
    })
});

// Export API routes
module.exports = router;
