import glob from 'glob';
import * as path from 'path';
import * as compielr from './compiler';


export type SrcOptions = {

    projectRoot: string;
}


export class ResFile {

    constructor(public origin: string) {

    }
}

export function src(matcher: string, options: SrcOptions) {


    const globOption: glob.IOptions = {
        root: options.projectRoot,
        cwd: options.projectRoot
    }
    return asyncGlob(matcher, globOption).then(files => files.map(createFile))
}


function createFile(filename: string) {
    return new ResFile(filename);
}

function asyncGlob(matcher: string, options: glob.IOptions) {
    return new Promise<string[]>((resolve, reject) => {
        glob(matcher, options, (error, files) => {
            resolve(files)
        })
    })
}













export type PluginContext = {
    projectRoot: string,
    outputDir: string,
    buildConfig: { command: "build" | "publish" },

    createFile: (relativePath: string, content: Buffer) => void
}

export type Plugin = {

    onStart?: (param: PluginContext) => void | Promise<void>

    onFile: (file: ResFile) => Promise<ResFile | null>;

    onFileLater?: (file: ResFile) => Promise<ResFile | null>;

    onFinish: (param: PluginContext) => void | Promise<void>
}

let projectRoot: string;
let buildConfig: { command: "build" | "publish" };

export function init(__projectRoot: string, __buildConfig: { command: "build" | "publish" }) {
    projectRoot = __projectRoot;
    buildConfig = __buildConfig;
}

export function compile(projectRoot: string) {
    compielr.run(projectRoot);
}


export function createPlugin(p: Plugin, outputDir: string) {

    outputDir = path.resolve(projectRoot, outputDir).split('\\').join('/');

    let context1: PluginContext = {
        projectRoot, outputDir, buildConfig, createFile: () => { console.error('onStart 中暂不支持 createFile') }
    }

    const onFile = async (file: ResFile, enc: any, cb: Function) => {
        try {
            let r = await p.onFile(file);
            if (r) {
                cb(null, file);
            }
            else {
                cb(null);
            }
        }
        catch (e) {
            console.log(e);
        }
    }

    const onFinish = async function (cb: Function) {


        const createFile = (relativePath: string, buffer: Buffer, options?: any) => {
            // relativePath = relativePath.split('\\').join('/')
            // let newFile = new Vinyl({
            //     cwd: projectRoot,
            //     base: projectRoot,
            //     path: path.join(projectRoot, relativePath),
            //     origin: relativePath,
            //     contents: buffer,
            //     isDirty: true,
            //     options,
            //     outputDir: options && options.outputDir ? options.outputDir : null
            // });
            // this.push(newFile);
        }


        try {
            let context2: PluginContext = {
                projectRoot, outputDir, buildConfig, createFile
            }
            await p.onFinish(context2);
            cb();
        }
        catch (e) {
            console.log(e);
        }
    }
    if (p.onStart) {
        let x = p.onStart(context1);
        if (x && x.then) {
            console.error('onStart 暂时不支持异步')
        }
    }
    // return through.obj(onFile, onFinish)
}