'use strict';

// GLOBAL

// LOCAL
const slackUtils = require('./slack-utils');
const tools = require('../tools');

// INNER
const innerCommon = require('./common');
const innerConfig = require('./config');
const innerMessageStack = require('./message-stack');

// ARGS
const { debug, debugNotification } = tools.getArgs();


module.exports.sendProjectMessage = (options) => {

  tools.logTitle('Sending project message');

  const message = innerCommon.getMessage(options);

  if (debug && debugNotification) {
    tools.logInfo('Message content :');
    console.log(message);
  }

  // buffer message
  innerMessageStack.pushMessage(message);

  // getting package notification config
  const config = innerConfig.getProjectConfig(options.project);

  // sending slack notification
  if (config.CLIENT === 'SLACK') {
    return Promise.resolve()
    .then(() => {
      return slackUtils.sendProjectMessage(config.SLACK_HOOK, config.SLACK_CHANNEL, message);
    })
    .catch((e) => {
      throw e;
    })

  } else {
    return Promise.resolve();
  }
}



module.exports.sendProjectDiffReportMessage = (project, diffMetadata) => {
  tools.logTitle('Sending project difference report message');

  return Promise.resolve()
    .then(() => {
      const config = innerConfig.getProjectConfig(project);

      return slackUtils.sendProjectDiffReportMessage(
        diffMetadata,
        config.SLACK_HOOK,
        config.SLACK_CHANNEL
      );
    })

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