import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { Options } from "./types";
import path = require('path');
import { tmpdir } from "os";
import { readFileSync, writeFileSync } from "fs";

export = class Anime4KCPP {
    public readonly inputPath: string;
    public readonly outputPath: string;
    public readonly process: ChildProcessWithoutNullStreams;
    public endBuffer: Buffer;

    constructor (inp: string | Buffer, out: string, options: Options = {}) {
        if (typeof inp == "string") this.inputPath = path.resolve(inp);
        else {
            this.inputPath = path.resolve(tmpdir(), generate(50) + ".png");
            writeFileSync(this.inputPath, inp as Buffer);
        }
        if (options.outputAsBuffer) {
            this.outputPath = path.resolve(tmpdir(), generate(50) + ".png");
        } else {
            this.outputPath = path.resolve(out);
        }

        this.process = spawn(__dirname + "/../anime4k/Anime4KCPP_CLI/Anime4KCPP_CLI", [
            "-i", this.inputPath,
            "-o", this.outputPath,
            "-v",
            ...(options.gpu ? ["-q"] : []),
            ...(options.scale ? ['-z', ''+options.scale] : [])
        ]);

        this.process.on('close', () => {
            try {
                if (options.outputAsBuffer) this.endBuffer = readFileSync(this.outputPath);
            } catch (e) {}
        })
    }

    public static upscale(inp: string | Buffer, out: string, options?: Options) {
        return new Anime4KCPP(inp, out, options);
    }

    public static async listGPUs() {
        const process = spawn(__dirname + "/../anime4k/Anime4KCPP_CLI/Anime4KCPP_CLI", [
            "-l"
        ]);
        let out: Array<string> = [];
        process.on('error', console.log)
        process.stdout.on('data', d => out.push(...d.toString().split("\r\n")));
        process.stderr.on('data', d => out.push(...d.toString().split("\r\n")));
        await new Promise<void>(res => (process.on('close', res)));
        const GPUs = [];
        out = out.filter(e => e != "");
        for (let std of out) {
            const str = std.split(" ").filter(e => e != "");
            if (str.includes("Device") && +(str[1].replace(":", "")) >= 0) {
                
                GPUs[+(str[1].replace(":", ""))] = std.split(": ")[1];
            }
        }
        return GPUs
    }

    public finishedPromise() {
        return new Promise<void>(res => {
            process.on('exit', res)
        });
    }
}

const generate=(length)=>{const alphabet="AZERTYUIOPQSDFGHJKLMWXCVBNazertyuiopqsdfghjklmwxcvbn123456789";let end="";for(let i=0;i<length;i++)end+=alphabet[Math.floor(Math.random()*alphabet.length)];return end}