// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
"use strict";

const child_process = require("child_process");
const fs = require("fs");
const log = require('fancy-log');
const colors = require('ansi-colors');
const path = require("path");
const PluginError = require('plugin-error');
const through = require("through2");

/**
 * Pretty logger using 'log'
 * @param {string} pluginName Name of the pluginName
 * @param {Object} file A gulp file to report on
 * @param {string} message The error message to display
 */
function logError(pluginName, file, message) {
    const sourcePath = path.relative(__dirname, file.path).replace("../", "");
    log(`[${colors.cyan(pluginName)}] ${colors.red("error")} ${sourcePath}: ${message}`);
}

/**
 * Plugin to verify the Microsoft copyright notice is present
 */
function checkCopyright() {
    const pluginName = "check-copyright";
    let hadErrors = false;
    const copyrightNotice = "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for details.";

    return through.obj(function (file, encoding, callback) {
        if (file.isBuffer()) {
            let fileContents = file.contents.toString(encoding);
            fileContents = fileContents.replace("\r\n", "\n");
            fileContents = fileContents.replace("\"use strict\";\n", "");
            fileContents = fileContents.replace("Object.defineProperty(exports, \"__esModule\", { value: true });\n", "");

            if (fileContents.indexOf(copyrightNotice) !== 0) {
                logError(pluginName, file, "missing copyright notice");
                hadErrors = true;
            }
        }

        callback(null, file);
    },
        function (callback) {
            if (hadErrors) {
                return this.emit("error", new PluginError(pluginName, "Failed copyright check"));
            }
            callback();
        });
}

/**
 * Helper function to check if a file exists case sensitive
 * @param {string} filePath The path to check
 * @returns {boolean} If the path exists case sensitive
 */
function existsCaseSensitive(filePath) {
    if (fs.existsSync(filePath)) {
        const fileName = path.basename(filePath);
        return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1;
    }

    return false;
}

/**
 * Plugin to verify if import statements use correct casing
 */
function checkImports() {
    const pluginName = "check-imports";
    let hadErrors = false;
    const re = /(?:\s|^)(?:[^\n:]*).*from ["'](\.[^"']*)["'];/;

    return through.obj(function (file, encoding, callback) {
        if (file.isBuffer()) {
            var fileContents = file.contents.toString(encoding);
            var importStatements = fileContents.match(new RegExp(re.source, "g")) || [];
            var workingDirectory = path.dirname(file.path);

            importStatements.forEach(function (importStatement) {

                var modulePath = re.exec(importStatement);
                if (modulePath && modulePath[1]) {
                    var moduleFilePath = path.resolve(workingDirectory, modulePath[1] + ".ts");
                    var moduleFilePath2 = path.resolve(workingDirectory, modulePath[1] + "/index.ts");

                    if (!existsCaseSensitive(moduleFilePath) && !existsCaseSensitive(moduleFilePath2)) {
                        logError(pluginName, file, `unresolved import: "${modulePath[1]}"`);
                        hadErrors = true;
                    }
                }
            });
        }

        callback(null, file);
    },
        function (callback) {
            if (hadErrors) {
                return this.emit("error", new PluginError(pluginName, "Failed import casing check"));
            }
            callback();
        });
}

function executeCommand(command, args, callback, opts) {
    const proc = child_process.spawn(command + (process.platform === "win32" ? ".cmd" : ""), args, opts);
    let errorSignaled = false;

    proc.stdout.on("data", (data) => {
        log(`${data}`);
    });

    proc.stderr.on("data", (data) => {
        log.error(`${data}`);
    });

    proc.on("error", (error) => {
        if (!errorSignaled) {
            callback(`An error occurred. ${error}`);
            errorSignaled = true;
        }
    });

    proc.on("exit", (code) => {
        if (code === 0) {
            callback();
        } else if (!errorSignaled) {
            callback(`Error code: ${code}`);
            errorSignaled = true;
        }
    });
}

module.exports = {
    checkCopyright,
    checkImports,
    executeCommand
}