'use strict';

// GLOBAL
const path = require('path');

// UTILS
const utils = require('../../../utils');

// CSDR RELATED
const configUtils = require('../../config/config-utils');
const metadataUtils = require('../../metadata/metadata-utils');

// INNER MODULES
const innerCommon = require('./common');


module.exports.run = () => {

    // fetching package from config based on default arg
    const pkg = configUtils.packages.getPackage();

    // checking if the package can be retrieved from args
    if (!pkg.name) {
      utils.tools.logError('ERROR : "package" NOT PROVIDED as argument !!!');
      process.exit(1);
    }

    // Get branches flags
    const branches = innerCommon.getBranches();

    // Get envTarget (used by remotes)
    const envTarget = innerCommon.getEnvTarget();

    // local saved vars
    let newVersion, pkgMetadata;


    return Promise.resolve()
      // LOGGING CURRENT eUI TOOLS VERSION
      .then(() => {
        utils.tools.logVersion();
      })

      // RELEASE PACKAGE START
      .then(() => {
        return innerCommon.init(pkg);
      })

      // FETCHING COMMITS METADATA
      .then(() => {
        return metadataUtils.commit.getMetadata(pkg);
      })
      .then((metadata) => {
        pkgMetadata = metadata;
      })

      // COMMIT METADATA CHECKS
      .then(() => {
        return innerCommon.commitMetadataChecks(pkg);
      })
      .then((metadata) => {
        pkgMetadata = metadata;
      })


      // EXECUTING PRE BUILD SCRIPT IF AVAILABLE
      .then(() => {
        return innerCommon.preBuild(pkg);
      })


      // BUILD PACKAGE
      .then(() => {
        return utils.buildPackage.build(pkg, branches.isMaster);
      })


      // EXECUTING SONAR ANALYSIS
      .then(() => {
        return utils.sonar.run(pkg, branches.isMaster);
      })


      // GENERATE and UPDATE new version
      .then(() => {
        return innerCommon.updateVersion(pkg, pkgMetadata, envTarget);
      })
      .then((version) => {
        // storing version for later use
        newVersion = version;
      })


      // GENERATE CHANGELOG
      .then(() => {
        return innerCommon.generateChangelog(pkg, newVersion, pkgMetadata);
      })


      // EXECUTING GLOBAL POST BUILD
      .then(() => {
        return innerCommon.postBuild(pkg, newVersion);
      })


      // PUBLISH PACKAGE
      .then(() => {
        return utils.publish.publish(pkg);
      })


      // EXECUTING POST PUBLISH SCRIPT IF AVAILABLE
      .then(() => {
        return utils.publish.postPublish(pkg);
      })


      // STORING CENTRALIZED METADATA
      .then(() => {
        // get run duration
        const duration = utils.pipeline.getTimerDuration();
        return metadataUtils.backend.storeMetadataBackend(
          pkg, newVersion, pkgMetadata,
          branches.isMaster,
          duration,
          envTarget
        );
      })


      // GIT OPERATIONS
      .then(() => {
        return innerCommon.runGitOperations(pkg, newVersion);
      })

      // SEND SUCCESS NOTIFICATION
      .then(() => {
        return innerCommon.sendSuccessNotification(pkg, newVersion, pkgMetadata)
          .then(() => {
            return innerCommon.close(pkg);
          });
      })

      .catch((e) => {
        return innerCommon.sendErrorNotification(pkg, e, pkgMetadata)
          .then(() => {
            return innerCommon.close(pkg);
          })
          .then(() => {
            process.exit(1);
          })
      })
}
