'use strict';

const metadataUtils = require('../../csdr/metadata/metadata-utils');


module.exports.getMessage = (options) => {
  const message = {
    scope: null,
    version: options.version || null,
    exception: options.exception || null,
    mainTitle: null,
    mainText: null,
    detailTitle: null,
    detailText: null,
    detailTextArray: [],
    text: options.text || null,
    status: null,
    statusColor: null
  };

  if (options.package) {
    message.scope = options.package.name;
  } else if (options.project) {
    message.scope = options.project.name;
  } else {
    return {};
  }


  // exception message
  if (options.exception) {
    if (options.title) {
      message.mainTitle = options.title;
      message.mainText = options.subtitle;
    } else {
      message.mainTitle = '[BUILD FAILED]';
      message.mainText = 'Awwww! The last build failed... :(';
    }
    message.status = 'ERROR';
    message.statusColor = '#D91E18';

    switch (options.exception) {
      case 'NO_VALID_COMMIT_FOUND':
        message.detailTitle = 'NO VALID COMMIT FOUND!';
        message.detailTextArray.push('No valid commit found since last release tag');
        break;

      case 'NO_ISSUE_FOUND':
        message.detailTitle = 'NO ISSUE FOUND!';
        message.detailTextArray.push('No issue found on commits since last release tag');
        message.detailTextArray.push('Commit details:');
        if (options.metadata.commits && options.metadata.commits.length !== 0) {
          const commits = options.metadata.commits.map((commit) => {
            return metadataUtils.commit.formatCommit(commit);
          });
          message.detailTextArray = [...message.detailTextArray, ...commits];
        }
        break;

      case 'GLOBAL_PIPELINE_DISABLED':
        message.detailTitle = 'PIPELINE GLOBALLY DISABLED!';
        message.detailTextArray.push('Due to maintenance, all pipeline jobs have been temporary disabled - retry later');
        break;

      case 'PACKAGE_LOCKED':
        message.detailTitle = 'CONCURRENT BUILD DETECTED FOR PACKAGE - ABORTING!';
        message.detailTextArray.push('Another build has been detected for the current package being build\n Retry again this build after 15m, lock will be auto-cleared');
        break;

      case 'MULTIPLE_LOCAL_EUI_VERSIONS_FOUND':
        message.detailTitle = 'MULTIPLE eUI VERSIONS FOUND IN LOCAL PROJECT - ABORTING!';
        message.detailTextArray.push('Uncompatible version of eUI found installed in the local CSDR project, please adjust and keep same compatible versions of eUI packages and projects');
        break;

      case 'BRANCH_NOT_PROVIDED':
        message.detailTitle = 'RELEASE BRANCH missing - ABORTING!';
        message.detailTextArray.push('Package release needs to have a branch to build from as parameter from Bamboo');
        break;

      case 'REMOTE_ENVTARGET_NOT_MASTER_BRANCH':
        message.detailTitle = 'REMOTE envTarget type branchis different from master - ABORTING!';
        message.detailTextArray.push('For envTarget remote delivery or "à la carte", only the master branch is allowed for build, contact support if any question');
        break;

      case 'AUDIT_DEPENDENCIES_GATES_FAILED':
        message.detailTitle = 'AUDIT DEPENDENCIES GATES FAILED';
        message.detailTextArray.push('Audit dependencies gates for current eUI version contains blocking vulnerabilities - ABORTING!');
        break;

      default:
        message.detailTitle = 'GENERAL ERROR!';
        if (options.exception.stderr) {
          try {
            message.detailTextArray = options.exception.stderr.split('\n');
            if (options.exception.stdout) {
              message.detailTextArray = [...message.detailTextArray, ...options.exception.stdout.split('\n')];
            }
          } catch (e) {}
        } else {
          try {
            message.detailTextArray = options.exception.stack.split('\n');
          } catch (e) {};
        }
    };

    message.detailText = message.detailTextArray.join('\n');

  } else {

    if (!options.text) {
      if (options.title) {
        message.mainTitle = options.title;
        message.mainText = options.subtitle;
      } else {
        message.mainTitle = `[BUILD SUCCESS]`;
        message.mainText = 'Great! Last build succeeded... The boat is floating :)'
      }
      message.status = 'SUCCESS';
      message.statusColor = '#26A65B';

      if (options.metadata && options.metadata.commits && options.metadata.commits.length !== 0) {
        message.detailTitle = 'Commit history since last release tag';
        const formattedCommits = options.metadata.commits.map((commit) => {
          return metadataUtils.commit.formatCommit(commit);
        });
        message.detailText = formattedCommits.join('\n ');
        message.detailTextArray = formattedCommits;

        if (options.metadata.issues && options.metadata.issues.length !== 0) {
          const extractedIssuesText = 'Extracted issues : ' + options.metadata.issues;
          message.detailText += '\n' + extractedIssuesText;
          message.detailTextArray.push(extractedIssuesText);
        }
      }
    }
  }

  return message;
}

