import {promises as fsp} from 'fs';
import {join} from 'path';
import cpy from 'cpy';
import {generateLove, LoveVariant} from './love';

export async function entry(): Promise<void> {
  const atomDirectory: string = join(__dirname, '../atom/');

  const packageTemplate: any = JSON.parse(
    await fsp.readFile(join(atomDirectory, 'package.json'), 'utf8')
  );
  const syntaxTemplate: string = await fsp.readFile(
    join(atomDirectory, 'syntax-template.less'),
    'utf8'
  );

  const love: LoveVariant[] = generateLove();
  for (const variant of love) {
    // Add the `@variable` colors to the top of the template.
    const lessVariables = `
@foreground-1: ${variant.colors.foreground1};
@foreground-2: ${variant.colors.foreground2};
@background-1: ${variant.colors.background1};
@background-2: ${variant.colors.background2};
${variant.colors.accents
  .map((value, index) => `@accent-${index + 1}: ${value};`)
  .join('\n')}
${variant.colors.grays
  .map((value, index) => `@gray-${index + 1}: ${value};`)
  .join('\n')}`.trim();

    let syntaxTheme: string = syntaxTemplate;
    syntaxTheme = syntaxTheme.replace(
      /\/\* @variables-insert \*\//,
      lessVariables
    );

    const uiFiles: string[] = await fsp.readdir(
      join(atomDirectory, 'ui-template')
    );
    const uiTheme =
      '@import "styles/ui-variables";\n' +
      uiFiles.map(value => `@import "styles/${value}";`).join('\n');

    const syntaxDirectory: string = join(
      atomDirectory,
      `love-${variant.name}-syntax`
    );
    const uiDirectory: string = join(atomDirectory, `love-${variant.name}-ui`);

    // Create all the necessary directories we'll be writing to.
    await fsp.mkdir(join(syntaxDirectory, 'styles/'), {recursive: true});
    await fsp.mkdir(join(uiDirectory, 'styles/'), {recursive: true});
    await fsp.mkdir(join(syntaxDirectory, 'images/'), {recursive: true});
    await fsp.mkdir(join(uiDirectory, 'images/'), {recursive: true});

    // Write formatted templates to each directory.
    await fsp.writeFile(join(syntaxDirectory, 'index.less'), syntaxTheme);
    await fsp.writeFile(join(uiDirectory, 'index.less'), uiTheme);

    // Write all the UI template files with the Less variables.
    for (const uiFile of uiFiles) {
      const uiCSS = `${lessVariables}\n${await fsp.readFile(
        join(atomDirectory, 'ui-template', uiFile),
        'utf8'
      )}`;
      await fsp.writeFile(join(uiDirectory, 'styles', uiFile), uiCSS);
    }

    // Format the `package.json` and write them to the appropriate directories.
    const syntaxPackage: any = {...packageTemplate};
    syntaxPackage.name = `love-${variant.name}-syntax`;
    syntaxPackage.theme = 'syntax';
    syntaxPackage.keywords = syntaxPackage.keywords.concat(
      'syntax',
      variant.name
    );
    syntaxPackage.repository = `git@github.com:HollloCC/love-${variant.name}-syntax.git`;

    const uiPackage: any = {...packageTemplate};
    uiPackage.name = `love-${variant.name}-ui`;
    uiPackage.theme = 'ui';
    uiPackage.keywords = uiPackage.keywords.concat('ui', variant.name);
    uiPackage.repository = `git@github.com:HollloCC/love-${variant.name}-ui.git`;

    await fsp.writeFile(
      join(syntaxDirectory, 'package.json'),
      JSON.stringify(syntaxPackage, null, 2)
    );
    await fsp.writeFile(
      join(uiDirectory, 'package.json'),
      JSON.stringify(uiPackage, null, 2)
    );

    // Write the syntax and UI variables.
    await fsp.writeFile(
      join(syntaxDirectory, 'styles/syntax-variables.less'),
      generateLessSyntaxVariables(variant)
    );
    await fsp.writeFile(
      join(uiDirectory, 'styles/ui-variables.less'),
      generateLessUIVariables(variant)
    );

    // Copy the License, ReadMe and images.
    await fsp.copyFile(
      join(__dirname, '../../License'),
      join(syntaxDirectory, 'License')
    );
    await fsp.copyFile(
      join(__dirname, '../../License'),
      join(uiDirectory, 'License')
    );

    await fsp.copyFile(
      join(atomDirectory, 'ReadMe.md'),
      join(syntaxDirectory, 'ReadMe.md')
    );
    await fsp.copyFile(
      join(atomDirectory, 'ReadMe.md'),
      join(uiDirectory, 'ReadMe.md')
    );

    await cpy(join(atomDirectory, 'images'), join(syntaxDirectory, 'images'));
    await cpy(join(atomDirectory, 'images'), join(uiDirectory, 'images'));
  }
}

export function generateLessSyntaxVariables(love: LoveVariant): string {
  return `
@syntax-text-color: ${love.colors.foreground1};
@syntax-cursor-color: ${love.colors.foreground1};
@syntax-selection-color: fade(${love.colors.foreground1}, 20%);
@syntax-selection-flash-color: ${love.colors.accents[6]};
@syntax-background-color: ${love.colors.background1};
@syntax-wrap-guide-color: ${love.colors.background2};
@syntax-indent-guide-color: ${love.colors.grays[1]};
@syntax-invisible-character-color: ${love.colors.grays[1]};
@syntax-result-marker-color: ${love.colors.foreground2};
@syntax-result-marker-color-selected: ${love.colors.foreground1};
@syntax-gutter-text-color: ${love.colors.foreground1};
@syntax-gutter-text-color-selected: ${love.colors.foreground1};
@syntax-gutter-background-color: ${love.colors.background2};
@syntax-gutter-background-color-selected: ${love.colors.grays[1]};
@syntax-color-added: ${love.colors.accents[4]};
@syntax-color-modified: ${love.colors.accents[1]};
@syntax-color-removed: ${love.colors.accents[0]};
@syntax-color-renamed: ${love.colors.accents[6]};
@syntax-color-variable: ${love.colors.accents[0]};
@syntax-color-constant: ${love.colors.accents[0]};
@syntax-color-property: ${love.colors.accents[0]};
@syntax-color-value: ${love.colors.accents[1]};
@syntax-color-function: ${love.colors.accents[6]};
@syntax-color-method: ${love.colors.accents[6]};
@syntax-color-class: ${love.colors.accents[2]};
@syntax-color-keyword: ${love.colors.accents[9]};
@syntax-color-tag: ${love.colors.accents[0]};
@syntax-color-attribute: ${love.colors.accents[1]};
@syntax-color-import: ${love.colors.accents[4]};
@syntax-color-snippet: ${love.colors.accents[4]};
`.trim();
}

export function generateLessUIVariables(love: LoveVariant): string {
  return `
@text-color: ${love.colors.foreground1};
@text-color-subtle: ${love.colors.foreground2};
@text-color-highlight: ${love.colors.foreground1};
@text-color-selected: @text-color-highlight;
@text-color-info: ${love.colors.accents[6]};
@text-color-success: ${love.colors.accents[4]};
@text-color-warning: ${love.colors.accents[1]};
@text-color-error: ${love.colors.accents[0]};
@background-color-info: ${love.colors.accents[6]};
@background-color-success: ${love.colors.accents[4]};
@background-color-warning: ${love.colors.accents[1]};
@background-color-error: ${love.colors.accents[0]};
@background-color-highlight: fade(${love.colors.foreground1}, 20%);
@background-color-selected: @background-color-highlight;
@app-background-color: ${love.colors.background1};
@base-background-color: ${love.colors.background1};
@base-border-color: ${love.colors.foreground1};
@pane-item-background-color: ${love.colors.background1};
@pane-item-border-color: @base-border-color;
@input-background-color: ${love.colors.background2};
@input-border-color: @base-border-color;
@tool-panel-background-color: ${love.colors.background1};
@tool-panel-border-color: @base-border-color;
@inset-panel-background-color: ${love.colors.background2};
@inset-panel-border-color: @base-border-color;
@panel-heading-background-color: ${love.colors.background2};
@panel-heading-border-color: transparent;
@overlay-background-color: ${love.colors.background2};
@overlay-border-color: @base-border-color;
@button-background-color: ${love.colors.accents[6]};
@button-background-color-hover: ${love.colors.accents[4]};
@button-background-color-selected: ${love.colors.accents[9]};
@button-border-color: ${love.colors.accents[6]};
@tab-bar-background-color: ${love.colors.background2};
@tab-bar-border-color: ${love.colors.background2};
@tab-background-color: @tool-panel-background-color;
@tab-background-color-active: ${love.colors.background1};
@tab-border-color: @base-border-color;
@tree-view-background-color: @tool-panel-background-color;
@tree-view-border-color: @tool-panel-border-color;
@ui-site-color-1: ${love.colors.accents[4]};
@ui-site-color-2: ${love.colors.accents[6]};
@ui-site-color-3: ${love.colors.accents[1]};
@ui-site-color-4: ${love.colors.accents[9]};
@ui-site-color-5: ${love.colors.accents[2]};
@font-size: 13px;
@input-font-size: 14px;
@disclosure-arrow-size: 12px;
@component-padding: 10px;
@component-icon-padding: 5px;
@component-icon-size: 16px;
@component-line-height: 25px;
@component-border-radius: 0;
@tab-height: 40px;
@font-family: system-ui;
@use-custom-controls: true; // false uses native controls
`.trim();
}

if (require.main === module) {
  entry();
}
