const fse = require('fs-extra');
const path = require('path');

const { getProductName, getVersion } = require('./package-info');

const productName = getProductName();
const version = getVersion();

const projectRoot = path.join(__dirname, '..');

function getDistRoot() {
  return path.join(projectRoot, 'build', 'dist');
}

function getDistPath() {
  return path.join(
    getDistRoot(),
    `${getExecutableName()}-${process.platform}-x64`
  );
}

function getExecutableName() {
  const suffix = process.env.NODE_ENV === 'development' ? '-dev' : '';

  if (process.platform === 'win32') {
    return `${getWindowsIdentifierName()}${suffix}`;
  } else if (process.platform === 'linux') {
    return 'desktop';
  } else {
    return productName;
  }
}

function getOSXZipName() {
  return `${productName}.zip`;
}

function getOSXZipPath() {
  return path.join(getDistPath(), '..', getOSXZipName());
}

function getWindowsInstallerName() {
  const productName = getExecutableName();
  return `${productName}Setup.msi`;
}

function getWindowsInstallerPath() {
  return path.join(getDistPath(), '..', 'installer', getWindowsInstallerName());
}

function getWindowsStandaloneName() {
  const productName = getExecutableName();
  return `${productName}Setup.exe`;
}

function getWindowsStandalonePath() {
  return path.join(getDistPath(), '..', 'installer', getWindowsStandaloneName());
}

function getWindowsFullNugetPackageName() {
  return `${getWindowsIdentifierName()}-${version}-full.nupkg`;
}

function getWindowsFullNugetPackagePath() {
  return path.join(
    getDistPath(),
    '..',
    'installer',
    getWindowsFullNugetPackageName()
  );
}

function getWindowsDeltaNugetPackageName() {
  return `${getWindowsIdentifierName()}-${version}-delta.nupkg`;
}

function getWindowsDeltaNugetPackagePath() {
  return path.join(
    getDistPath(),
    '..',
    'installer',
    getWindowsDeltaNugetPackageName()
  );
}

function getWindowsIdentifierName() {
  return 'GitHubDesktop';
}

function getBundleSizes() {
  // eslint-disable-next-line no-sync
  const rendererStats = fse.statSync(
    path.join(projectRoot, 'out', 'renderer.js')
  );
  // eslint-disable-next-line no-sync
  const mainStats = fse.statSync(path.join(projectRoot, 'out', 'main.js'));
  return { rendererSize: rendererStats.size, mainSize: mainStats.size };
}

function getReleaseBranchName() {
  return (
    process.env.CIRCLE_BRANCH || // macOS
    process.env.APPVEYOR_REPO_BRANCH || // Windows
    ''
  );
}

function getReleaseChannel() {
  // Branch name format: __release-CHANNEL-DEPLOY_ID
  const pieces = getReleaseBranchName().split('-');
  if (pieces.length < 3 || pieces[0] !== '__release') {
    return process.env.NODE_ENV || 'development';
  }

  return pieces[1];
}

function getReleaseSHA() {
  // Branch name format: __release-CHANNEL-DEPLOY_ID
  const pieces = getReleaseBranchName().split('-');
  if (pieces.length < 3 || pieces[0] !== '__release') {
    return null;
  }

  return pieces[2];
}

function getUpdatesURL() {
  return `https://central.github.com/api/deployments/desktop/desktop/latest?version=${version}&env=${getReleaseChannel()}`;
}

function shouldMakeDelta() {
  // Only production and beta channels include deltas. Test releases aren't
  // necessarily sequential so deltas wouldn't make sense.
  const channelsWithDeltas = ['production', 'beta'];
  return channelsWithDeltas.indexOf(getReleaseChannel()) > -1;
}

exports.getDistRoot = getDistRoot;
exports.getDistPath = getDistPath;
exports.getExecutableName = getExecutableName;
exports.getOSXZipName = getOSXZipName;
exports.getOSXZipPath = getOSXZipPath;
exports.getWindowsInstallerName = getWindowsInstallerName;
exports.getWindowsStandaloneName = getWindowsStandaloneName;
exports.getWindowsStandalonePath = getWindowsStandalonePath;
exports.getWindowsFullNugetPackageName = getWindowsFullNugetPackageName;
exports.getWindowsFullNugetPackagePath = getWindowsFullNugetPackagePath;
exports.getWindowsDeltaNugetPackageName = getWindowsDeltaNugetPackageName;
exports.getWindowsDeltaNugetPackagePath = getWindowsDeltaNugetPackagePath;
exports.getWindowsIdentifierName = getWindowsIdentifierName;
exports.getBundleSizes = getBundleSizes;
exports.getReleaseBranchName = getReleaseBranchName;
exports.getReleaseChannel = getReleaseChannel;
exports.getReleaseSHA = getReleaseSHA;
exports.getUpdatesURL = getUpdatesURL;
exports.shouldMakeDelta = shouldMakeDelta;
