'use strict';

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

const MAVEN_SETTINGS_TEMPLATE = 'maven-settings.template';
const SONAR_TEMPLATE = 'sonar-project.template';
const SONAR_BACKEND_TEMPLATE = 'sonar-project-backend.template';


const getTemplatePath = (fileName) => {
  return Promise.resolve()
    .then(() => {
      return path.join(__dirname, fileName);
    })
    .catch((e) => {
      throw e;
    })
}



const generateMavenSettings = (userName, userPassword, outputPath) => {
  tools.logInfo('Generating maven settings file...');

  let outputFilePath = path.join(process.cwd(), MAVEN_SETTINGS_TEMPLATE);
  if (outputPath) {
    outputFilePath = path.join(outputPath, MAVEN_SETTINGS_TEMPLATE);
  }

  return Promise.resolve()
    .then(() => {
      return getTemplatePath(MAVEN_SETTINGS_TEMPLATE);
    })
    .then((tplFile) => {
      return Promise.resolve()
        .then(() => {
          tools.logInfo('Copy file...');
          return tools.copy(tplFile, outputFilePath);
        })
        .then(() => {
          tools.logInfo('Replace user name token...');
          return tools.replaceInFile(outputFilePath, '@user.name@', userName);
        })
        .then(() => {
          tools.logInfo('Replace user password token...');
          return tools.replaceInFile(outputFilePath, '@user.password@', userPassword);
        })
        .then(() => {
          tools.logSuccess();
        })
        .catch((e) => {
          throw e;
        })
    })
    .then(() => {
      return outputFilePath;
    })
    .catch((e) => {
      throw e;
    })
}






const generateSonarPropertiesUI = (pkgName, pkgVersion, outputPath, hasSubEntries = false) => {
  tools.logInfo('Generating sonar property file for UI package...');

  const outputFilePath = path.join(outputPath, 'sonar-project.properties');

  return Promise.resolve()
    .then(() => {
      return getTemplatePath(SONAR_TEMPLATE);
    })
    .then((tplFile) => {
      return Promise.resolve()
        .then(() => {
          tools.logInfo('Copy file...');
          return tools.copy(tplFile, outputFilePath);
        })
        .then(() => {
          tools.logInfo('Replace project name token...');
          return tools.replaceInFile(outputFilePath, '@project.name@', pkgName.toUpperCase());
        })
        .then(() => {
          tools.logInfo('Replace project version token...');
          return tools.replaceInFile(outputFilePath, '@project.version@', pkgVersion);
        })
        .then(() => {
          tools.logInfo('Replace project package path...');
          return tools.replaceInFile(outputFilePath, '@project.path@',  path.join(outputPath, hasSubEntries ? '': '/src/lib/'));
        })
        .then(() => {
          tools.logInfo('Replace project lcov root path...');
          return tools.replaceInFile(outputFilePath, '@project.lcov.path@', outputPath);
        })
        .then(() => {
          tools.logSuccess();
        })
        .catch((e) => {
          throw e;
        })
    })
    .then(() => {
      return outputFilePath;
    })
    .catch((e) => {
      throw e;
    })
}




const generateSonarPropertiesBackend = (groupId, pkgName, version, modulesList, outputPath) => {
  tools.logInfo('Generating sonar property file for UI package...');

  const outputFilePath = path.join(outputPath, 'sonar-project.properties');

  return Promise.resolve()
    .then(() => {
      return getTemplatePath(SONAR_BACKEND_TEMPLATE);
    })
    .then((tplFile) => {
      return Promise.resolve()
        .then(() => {
          tools.logInfo('Copy file...');
          return tools.copy(tplFile, outputFilePath);
        })
        .then(() => {
          tools.logInfo('Replace project groupId token...');
          return tools.replaceInFile(outputFilePath, '@project.groupid@', groupId);
        })
        .then(() => {
          tools.logInfo('Replace project name token...');
          return tools.replaceInFile(outputFilePath, '@project.name@', pkgName.toUpperCase());
        })
        .then(() => {
          tools.logInfo('Replace project version token...');
          return tools.replaceInFile(outputFilePath, '@project.version@', version);
        })
        .then(() => {
          tools.logInfo('Replace project package path...');
          return tools.replaceInFile(outputFilePath, '@project.path@', outputPath);
        })
        .then(() => {
          tools.logInfo('Replace project modules...');
          return tools.replaceInFile(outputFilePath, '@project.modules@', modulesList);
        })
        .then(() => {
          tools.logInfo('Checking custom properties');
          const customPropertiesFile = path.join(outputPath, 'sonar-custom.properties');
          if (tools.isFileExists(customPropertiesFile)) {
            const propertiesContent = tools.getFileContent(outputFilePath);
            const customPropertiesContent = tools.getFileContent(customPropertiesFile);
            return tools.writeFileContent(outputFilePath, propertiesContent + customPropertiesContent);
          }
        })
        .then(() => {
          tools.logSuccess();
        })
        .catch((e) => {
          throw e;
        })
    })
    .then(() => {
      return outputFilePath;
    })
    .catch((e) => {
      throw e;
    })
}




module.exports = {
  generateMavenSettings,
  generateSonarPropertiesUI,
  generateSonarPropertiesBackend,
}
