import inquirer from "inquirer";
import { ArgumentsCamelCase } from "yargs";

import fs from "fs";

import { XwebConfig } from "./config.js";
import { info, large_info, status } from "../log.js";
import path from "path";

export async function run(argv: ArgumentsCamelCase<{
    y: boolean;
}>): Promise<XwebConfig | null> {
    const CONFIG: XwebConfig = {
        folder: {
            src: "./src",
            out: "./out",
            tags: "./xweb_tags"
        },
        extension: {
            src: ".xweb",
            out: ".html"
        },
        use: {
            inline: true,
            tags: true
        },
        inline: {
            from: {
                start: "{{",
                end: "}}",
            },
            to: {
                start: "<?=",
                end: "?>"
            }
        },
        tags: []
    };

    if (!argv.yes) {
        const ANSWERS = await inquirer.prompt([
            {
                name: "source_folder",
                message: "Source folder:",
                type: "input",
                default: "./src"
            },
            {
                name: "output_folder",
                message: "Output folder:",
                type: "input",
                default: "./out"
            },
            {
                name: "tags_folder",
                message: "Tags folder:",
                type: "input",
                default: "./xweb_tags"
            },
            {
                name: "src_extension",
                message: "Source file extension:",
                type: "input",
                default: ".xweb"
            },
            {
                name: "out_extension",
                message: "Output file extension:",
                type: "input",
                default: ".html"
            },
            {
                name: "use_inline",
                message: "Use inline xweb:",
                type: "list",
                choices: ["yes", "no"],
                default: "yes"
            },
            {
                name: "use_tags",
                message: "Use xweb tags:",
                type: "list",
                choices: ["yes", "no"],
                default: "yes"
            }
        ]);
        CONFIG.folder.src = ANSWERS.source_folder;
        CONFIG.folder.out = ANSWERS.output_folder;
        CONFIG.folder.tags = ANSWERS.tags_folder;
        CONFIG.extension.src = ANSWERS.src_extension;
        CONFIG.extension.out = ANSWERS.out_extension;
        CONFIG.use.inline = ANSWERS.use_inline == "yes";
        CONFIG.use.tags = ANSWERS.use_tags == "yes";

        if (CONFIG.use.tags) {
            const TAGSETS = await inquirer.prompt([{
                name: "import_tags",
                message: "Tagset names seperated by comma:",
                type: "input",
            }]);
            CONFIG.tags = TAGSETS.import_tags.split(",").map((tagset: string) => tagset.trim()).filter((tagset: string) => tagset != "");
        }

        if (CONFIG.use.inline) {
            const INLINE_USE = await inquirer.prompt([
                {
                    name: "from_start",
                    message: "Inline xweb start (src):",
                    type: "input",
                    default: "{{"
                },
                {
                    name: "from_end",
                    message: "Inline xweb end (src):",
                    type: "input",
                    default: "}}"
                },
                {
                    name: "to_start",
                    message: "Inline xweb start (out):",
                    type: "input",
                    default: "<?="
                },
                {
                    name: "to_end",
                    message: "Inline xweb end (out):",
                    type: "input",
                    default: "?>"
                }
            ]);
            if (CONFIG.inline == undefined) CONFIG.inline = {
                from: {
                    start: "{{",
                    end: "}}"
                },
                to: {
                    start: "<?=",
                    end: "?>"
                }
            };
            CONFIG.inline.from.start = INLINE_USE.from_start;
            CONFIG.inline.from.end = INLINE_USE.from_end;
            CONFIG.inline.to.start = INLINE_USE.to_start;
            CONFIG.inline.to.end = INLINE_USE.to_end;
        } else {
            delete CONFIG.inline;
        }
    }
    large_info("init/config", "Generated output", JSON.stringify(CONFIG, null, 4));
    if (!argv.yes) {
        const IS_OK = await inquirer.prompt([{
            name: "config_ok",
            message: "Is this ok?",
            type: "list",
            choices: ["yes", "no"],
            default: "yes"
        }]);
        if (IS_OK.config_ok != "yes") {
            info("init/config", "Rerun init command to generate config file");
            return null;
        }
    }

    const CONFIG_FILE_PATH = path.join(process.cwd(), "xwebconfig.json");
    fs.writeFileSync(CONFIG_FILE_PATH, JSON.stringify(CONFIG, null, 4));
    status("init/config", "Created file 'xwebconfig.json'");

    return CONFIG;
}