'use strict';

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

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

// INNER MODULES
const innerGlobal = require('./global');


// Register newly created package

module.exports.registerCsdrPackage = (pkg, rootPkgScope, rootPkgName, euiVersionNumber, isRemote, remoteConfig, isBackend) => {

  const csdrConfigPackage = {
    "name": "@module.full.name@",
    "npmPkg": "@module.npmPkg.name@",
    "repository": "@module.repo.name@"
  };

  const csdrConfigPackageBackend = {
      "name": "@module.full.name@",
      "repository": "@module.repo.name@",
      "backend": true,
      "teams": [
          "cc-backend",
          "backend"
      ],
      "build": {
          "publish": true
      },
      "slack": {
          "channel": "csdr-timeline-backend"
      }
  };

  const csdrConfigRemote = {
    "npmPkg": "@module.npmPkg.name@",
    "euiVersion": "@module.eui.version@.x",
    "build": {
      "compiledTranslations": true,
      "translationScopes": "eui,csdr,cc,mywp",
      "envTargetActive": true,
      "sonarScanner": false
    },
    "release": {
       "team": "@module.rmTeam.name@"
    },
    "slack": {
      "channel": "csdr-timeline-mwp-remotes-cc"
    },
    "fullSkeletonSources": true,
    "skeletonConfig": {
        "rootNpmPkg": "@module.rootPkg.scope@/@module.rootPkg.name@",
        "rootCommonModule": "@module.class.name@Module",
        "baseUrl": "@module.rootPkg.name@",
        "elementName": "@module.scope.name@",
        "storeName": "@module.scope.name@"
    }
  }



  // inserting package in root config
  let packageDef;

  if (isRemote) {
    packageDef = JSON.stringify(csdrConfigRemote);

  } else {
    if (isBackend) {
      packageDef = JSON.stringify(csdrConfigPackageBackend);
    } else {
      packageDef = JSON.stringify(csdrConfigPackage);
    }
  }

  // replacing tokens
  let replaceModule = tools.replaceAll(packageDef, '@module.full.name@', pkg.name);
  replaceModule = tools.replaceAll(replaceModule, '@module.eui.version@', euiVersionNumber);
  replaceModule = tools.replaceAll(replaceModule, '@module.repo.name@', pkg.repoName);
  replaceModule = tools.replaceAll(replaceModule, '@module.npmPkg.name@', pkg.npmPkgName);
  replaceModule = tools.replaceAll(replaceModule, '@module.rootPkg.scope@', rootPkgScope);
  replaceModule = tools.replaceAll(replaceModule, '@module.rootPkg.name@', rootPkgName);
  replaceModule = tools.replaceAll(replaceModule, '@module.class.name@', tools.camelCaseString(`${rootPkgScope.substr(1)}-${rootPkgName}`, '-', true));
  replaceModule = tools.replaceAll(replaceModule, '@module.scope.name@', `${rootPkgScope.substr(1)}-${rootPkgName}`);
  if (isRemote && remoteConfig && remoteConfig.rmTeamName) {
    replaceModule = tools.replaceAll(replaceModule, '@module.rmTeam.name@', remoteConfig.rmTeamName);
  }

  let file;
  if (isRemote) {
    if (tools.isDirExists(path.join(process.cwd(), '.config'))) {
      file = path.join(process.cwd(), '.config', 'remotes', '.euirc-remotes.json');
    } else {
      file = path.join(process.cwd(), '.csdr', '.euirc-csdr-remotes.json');
    }
  } else {
    if (tools.isDirExists(path.join(process.cwd(), '.config'))) {
      file = path.join(process.cwd(), '.config', '.euirc-packages.json');
    } else {
      file = path.join(process.cwd(), '.csdr', '.euirc-csdr-packages.json');
    }
  }

  const moduleJson = JSON.parse(replaceModule);

  if (isRemote && remoteConfig) {
    if (Object.keys(remoteConfig.options).length !== 0) {
      moduleJson.skeletonConfig.options = {};

      if (remoteConfig.options.userReducers) {
        moduleJson.skeletonConfig.options.userReducers = remoteConfig.options.userReducers;
      }
      if (remoteConfig.options.zipkin) {
        moduleJson.skeletonConfig.options.zipkin = remoteConfig.options.zipkin;
      }
      if (remoteConfig.options.dynamicForms) {
        moduleJson.skeletonConfig.options.dynamicForms = remoteConfig.options.dynamicForms;
      }
      if (remoteConfig.options.dynatrace) {
        moduleJson.skeletonConfig.options.dynatrace = remoteConfig.options.dynatrace;
      }

      if (remoteConfig.options.participant) {
        moduleJson.skeletonConfig.options.participant = remoteConfig.options.participant;
        moduleJson.skeletonConfig.blockContainerName = `${rootPkgScope.substr(1)}-participant-cp`;
      }
    }
  }

  let jsonFile = require(file);
  jsonFile[pkg.name] = moduleJson;

  tools.writeJsonFileSync(file, jsonFile);

  if (isBackend) {
    return;
  }

  // inserting project entry into local config
  const packages = [];
  packages.push(pkg.name);

  // updating euirc.json local config
  if (isRemote) {
    innerGlobal.updateConfig({ remotes: packages });

  } else {
    innerGlobal.updateConfig({ packages: packages });
  }
}
