import * as fs from 'fs';
import { dest, src } from 'gulp';
import ts from 'gulp-typescript';
import { Utilities } from './utilities';

export module UITestAutomationModule {
    const argv = Utilities.gulpArgv();

    // New UTA format
    export function buildLib() {
        const tsProject = ts.createProject('uta/tsconfig.json');

        return tsProject.src()
            .pipe(tsProject())
            .pipe(dest('lib/ui-test-automation'));
    }

    export function copyLibPackageJson() {
        return src(['uta/package.json'])
            .pipe(dest('lib/ui-test-automation'));
    }

    export function saveUiTestAutomation() {
        const packageJsonString = fs.readFileSync('lib/ui-test-automation/package.json').toString();
        const packageJson = JSON.parse(packageJsonString);
        const packageName = packageJson.name;

        return src(
            [
                'lib/ui-test-automation/**/*.ts',
                'lib/ui-test-automation/**/*.js'
            ]
        )
            .pipe(dest(`../msft-sme-ui-test-automation/node_modules/${packageName}`, { overwrite: true }));
    }

    export function saveUiTestAutomationToTarget(cb) {
        const targetName = argv['target'];
        if (!targetName) {
            cb();
            return;
        }

        const packageJsonString = fs.readFileSync('lib/ui-test-automation/package.json').toString();
        const packageJson = JSON.parse(packageJsonString);
        const packageName = packageJson.name;

        return src(
            [
                'lib/ui-test-automation/**/*.ts',
                'lib/ui-test-automation/**/*.js'
            ]
        )
            .pipe(dest(`../${targetName}/node_modules/${packageName}`, { overwrite: true }));
    }

    // Legacy UTA format
    // TODO: cleanup below when fully migrated to new format
    export function buildUiTestAutomation() {
        const tsProject = ts.createProject('./tsconfig.json');
        return src(['ui-test-automation/**/*.ts', '!ui-test-automation/node_modules/**/*.*'])
            .pipe(tsProject());
    }

    export function saveUiTestAutomation_legacy() {
        const urlSegments = '/SME/_git/';
        const lines = fs.readFileSync('.git/config', 'utf8').toString().split('\n');
        let remoteSection = false;
        let moduleName;
        for (const line of lines) {
            if (remoteSection && line.indexOf(urlSegments) >= 0) {
                const matchIndex = line.indexOf(urlSegments);
                moduleName = line.substring(matchIndex + urlSegments.length);
                break;
            }

            // look for [remote "origin"] section.
            if (line.indexOf('[remote \"origin\"]') >= 0) {
                remoteSection = true;
            }
        }

        return src(
            [
                'ui-test-automation/**/*.*'
            ])
            .pipe(dest('../msft-sme-ui-test-automation/src/tools/' + moduleName + '/ui-test-automation', { overwrite: true }));
    }
}
