'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.sendPackageMessage = (options) => {
  tools.logTitle('Sending package 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.getPackageConfig(options.package);

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

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



module.exports.sendPackageDiffReportMessage = (pkg, diffMetadata) => {
  tools.logTitle('Sending package difference report message');

  const config = innerConfig.getPackageConfig(pkg);

  if (config.CLIENT === 'SLACK') {
    return Promise.resolve()
      .then(() => {
        return slackUtils.sendPackageDiffReportMessage(
          diffMetadata,
          config.SLACK_HOOK,
          config.SLACK_CHANNEL
        );
      })

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

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