'use strict';

// GLOBAL

// LOCAL
const pipelineUtils = require('../pipeline-utils');
const configUtils = require('../../csdr/config/config-utils');



module.exports.getPackageConfig = (pkg) => {
  // Fetching global config - default if not overriden in package own config
  let globalConfig;

  if (pkg.standalone) {
    globalConfig = configUtils.global.getConfigOptionsStandalone()
  } else {
    globalConfig = configUtils.global.getConfigOptions();
  }

  let slackChannel, msTeamsChannel;


  if (pkg.backend) {
    slackChannel = globalConfig.SLACK_CHANNEL_BACKEND;
    msTeamsChannel = globalConfig.MSTEAMS_BACKEND_CHANNEL;
  } else {
    slackChannel = globalConfig.SLACK_CHANNEL_FRONTEND;
    msTeamsChannel = globalConfig.MSTEAMS_FRONTEND_CHANNEL;
  }

  // checking package notification strategy override
  if (!pkg.notification && !pkg.slack) {
    if (globalConfig.NOTIFICATION_CLIENT === 'SLACK') {
      return {
        CLIENT: globalConfig.NOTIFICATION_CLIENT,
        SLACK_HOOK: globalConfig.SLACK_HOOK,
        SLACK_CHANNEL: slackChannel
      }
    } else if (globalConfig.NOTIFICATION_CLIENT === 'MSTEAMS') {
      return {
        CLIENT: globalConfig.NOTIFICATION_CLIENT,
        MSTEAMS_CHANNEL: msTeamsChannel,
        MSTEAMS_CHANNEL_EMAIL: globalConfig.MSTEAMS_CHANNELS_EMAILS[msTeamsChannel]
      }
    }

  // backward compatibility if slack config is declared, in that case we force it to slack client
  } else if (pkg.slack && !pkg.notification) {
    return {
      CLIENT: 'SLACK',
      SLACK_HOOK: globalConfig.SLACK_HOOK,
      SLACK_CHANNEL: pkg.slack.channel
    }

  // new method, notification config can be either slack or mail
  } else {
    if (pkg.notification) {
      if (pkg.notification.client === 'SLACK') {
        return {
          CLIENT: pkg.notification.client,
          SLACK_HOOK: globalConfig.SLACK_HOOK,
          SLACK_CHANNEL: pkg.notification.channel
        }
      } else if (pkg.notification.client === 'MSTEAMS') {
        return {
          CLIENT: pkg.notification.client,
          MSTEAMS_CHANNEL: pkg.notification.channel,
          MSTEAMS_CHANNEL_EMAIL: globalConfig.MSTEAMS_CHANNELS_EMAILS[pkg.notification.channel]
        }
      }
    }
  }

  return {};
}

module.exports.getProjectConfig = (project) => {
  // Fetching global config - default if not overriden in project own config
  const globalConfig = configUtils.global.getConfigOptions();

  // checking project notification strategy override
  if (!project.notification && !project.slack) {
    if (globalConfig.NOTIFICATION_CLIENT === 'SLACK') {
      return {
        CLIENT: globalConfig.NOTIFICATION_CLIENT,
        SLACK_HOOK: globalConfig.SLACK_HOOK,
        SLACK_CHANNEL: globalConfig.SLACK_CHANNEL_PROJECT
      }
    } else if (globalConfig.NOTIFICATION_CLIENT === 'MSTEAMS') {
      return {
        CLIENT: globalConfig.NOTIFICATION_CLIENT,
        MSTEAMS_CHANNEL: globalConfig.MSTEAMS_PROJECT_CHANNEL,
        MSTEAMS_CHANNEL_EMAIL: globalConfig.MSTEAMS_CHANNELS_EMAILS[globalConfig.MSTEAMS_PROJECT_CHANNEL]
      }
    }

  // backward compatibility if slack config is declared, in that case we force it to slack client
  } else if (project.slack && !project.notification) {
    return {
      CLIENT: 'SLACK',
      SLACK_HOOK: globalConfig.SLACK_HOOK,
      SLACK_CHANNEL: project.slack.channel
    }

  // new method, notification config can be either slack or mail
  } else {
    if (project.notification) {
      if (project.notification.client === 'SLACK') {
        return {
          CLIENT: project.notification.client,
          SLACK_HOOK: globalConfig.SLACK_HOOK,
          SLACK_CHANNEL: project.notification.channel
        }
      } else if (project.notification.client === 'MSTEAMS') {
        return {
          CLIENT: globalConfig.NOTIFICATION_CLIENT,
          MSTEAMS_CHANNEL: project.notification.channel,
          MSTEAMS_CHANNEL_EMAIL: globalConfig.MSTEAMS_CHANNELS_EMAILS[project.notification.channel]
        }
      }
    }
  }
}


module.exports.storeConfig = (rootPath, slackHook, slackChannel, msTeamsChannel, msTeamsChannelEmail) => {
  return Promise.resolve()
    .then(() => {
      var content = '';
      content += `export SLACK_HOOK=${slackHook}\n`;
      content += `export SLACK_CHANNEL=${slackChannel}\n`;
      content += `export MSTEAMS_CHANNEL=${msTeamsChannel}\n`;
      content += `export MSTEAMS_CHANNEL_EMAIL=${msTeamsChannelEmail}\n`;
      return pipelineUtils.setVariables(rootPath, content);
    })

    .catch((e) => {
      throw e;
    })
}

module.exports.storePackageConfig = (pkg) => {
  return Promise.resolve()
    .then(() => {
      const config = this.getPackageConfig(pkg);

      return this.storeConfig(
        process.cwd(),
        config.SLACK_HOOK, config.SLACK_CHANNEL,
        config.MSTEAMS_CHANNEL, config.MSTEAMS_CHANNEL_EMAIL
      );
    })

    .catch((e) => {
      throw e;
    })
}

module.exports.storeProjectConfig = (project) => {
  return Promise.resolve()
    .then(() => {
      const config = this.getProjectConfig(project);

      return this.storeConfig(
        project.paths.rootPath,
        config.SLACK_HOOK, config.SLACK_CHANNEL,
        config.MSTEAMS_CHANNEL, config.MSTEAMS_CHANNEL_EMAIL
      );
    })

    .catch((e) => {
      throw e;
    })
}
