#!/usr/bin/env node

const chalk = require('chalk');
const clear = require('clear');
const figlet = require('figlet');

const CLI = require('clui');
const ncp = require('ncp');
const Spinner = CLI.Spinner;

const spawn = require('cross-spawn');

// libraries
const inquirer = require('./lib/inquirer');
const files = require('./lib/files');
const functions = require('./lib/functions');

clear();

console.log(
    chalk.green(
        figlet.textSync('DIME', { horizontalLayout: 'full', font:"ANSI Shadow" })
    )
);
console.log(
    chalk.green("Create DIME angular application.\n")
)


const run = async () => {
    const appConfig = await inquirer.askAppConfig();

    console.log(chalk.green("\nApplication environment variables (local).\n"))
    const envConfigLocal = await inquirer.askEnvConfig();
    console.log(chalk.green("\nApplication environment variables (Development).\n"))
    const envConfigDev = await inquirer.askEnvConfig("Development", envConfigLocal);
    console.log(chalk.green("\nApplication environment variables (Production).\n"))
    const envConfigProd = await inquirer.askEnvConfig("Production", envConfigDev);
    console.log(chalk.green("\nApplication environment variables (Test).\n"))
    const envConfigTest = await inquirer.askEnvConfig("Test", envConfigProd);

    console.log(chalk.green("\nApplication authentication configs (local).\n"))
    const authConfigLocal = await inquirer.askAuthConfig("local", envConfigLocal);
    console.log(chalk.green("\nApplication authentication configs (Development).\n"))
    const authConfigDev = await inquirer.askAuthConfig("Development", envConfigDev);
    console.log(chalk.green("\nApplication authentication configs (Production).\n"))
    const authConfigProd = await inquirer.askAuthConfig("Production", envConfigProd);
    console.log(chalk.green("\nApplication authentication configs (Test).\n"))
    const authConfigTest = await inquirer.askAuthConfig("Test", envConfigTest);

    clear()
    const status = new Spinner('Creating you app...');

    status.start();
    const packageJson = functions.createFile(await files.getFileByPath('boilerplate/package.json' ,'utf-8'), appConfig)
    await files.writeToFile('package.json', packageJson)
    console.log(authConfigLocal)
    await files.copyFolder('boilerplate/src', 'src')
    await files.copyFolder('boilerplate/e2e', 'e2e')
    await files.copyFolder('boilerplate/gitignore', '.gitignore')
    await files.copyFolder('boilerplate/angular.json', 'angular.json')
    await files.copyFolder('boilerplate/README.md', 'README.md')
    await files.copyFolder('boilerplate/tsconfig.json', 'tsconfig.json')
    await files.copyFolder('boilerplate/tslint.json', 'tslint.json')
    const authConfigTemp=await files.getFileByPath('boilerplate/src/assets/auth.config.json' ,'utf-8')
    const authConfigJsonLocal = functions.createFile(authConfigTemp, authConfigLocal)
    await files.writeToFile('src/assets/auth.config.json', authConfigJsonLocal)
    const authConfigJsonDev = functions.createFile(authConfigTemp, authConfigDev)
    await files.writeToFile('src/assets/dev/auth.config.json', authConfigJsonDev)
    const authConfigJsonProd = functions.createFile(authConfigTemp, authConfigProd)
    await files.writeToFile('src/assets/prod/auth.config.json', authConfigJsonProd)
    const authConfigJsonTest = functions.createFile(authConfigTemp, authConfigTest)
    await files.writeToFile('src/assets/test/auth.config.json', authConfigJsonTest)

    await files.writeToFile('src/environments/environment.ts', "export const environment = "+functions.createObjString(envConfigLocal))
    await files.writeToFile('src/environments/environment.dev.ts', "export const environment = "+functions.createObjString(envConfigDev))
    await files.writeToFile('src/environments/environment.prod.ts', "export const environment = "+functions.createObjString(envConfigProd))
    await files.writeToFile('src/environments/environment.test.ts', "export const environment = "+functions.createObjString(envConfigTest))
    const result = await spawn.sync('yarn', ['install'], { stdio: 'inherit' })
    console.log(result)
    status.stop();
    clear()
    console.log(chalk.green("\nApplication has been created.\n"))
};

run()
