/// <reference path="../../typings/index.d.ts" />
import "source-map-support/register"
import * as fs from "fs-extra"
import * as pathModule from "path"
import * as ts from "typescript"
import * as helper from "./helper"
import * as wrench from "wrench"
import * as commander from "commander"
import { Context } from "./context"
import { TSConfigParser } from "./tsConfigParser"
import { ComposerServer } from "../../composer/server/src/composer"
import * as colors from "colors"
let program = commander
    .description("Core util to generate leaf-ts required resources")
    .usage("[path-to-R.d.ts] [options...]")
    .option("-g --global", "created R.ts don't export")
    .option("-l --leaf <leaf>", "relative link to leaf.d.ts")
    .option("-c --create", "create the R.d.ts")
    .option("--root-name <name>", "using custom root namespace instead of \"R\"")
    .option("--composer", "start composer server")
    .option("--composer-port <port>", "port for the composer")
    .option("--composer-style-path <path>", "If your style file isn't at /style/main.css, set it here. The path is relative to R.d.ts or `Root` value of R.d.ts")
    .parse(process.argv)
interface UIInfo {
    tagName: string
    name: string
}
interface VMInfo {
    name: string
    usage: string[]
}
let defaultTemplate = `type Template = "./template"
type Output = "./script/R.ts"
type StyleOutput = "./style/R.less"
type Root = "./"
`
if (program["create"]) {
    fs.writeFileSync("./R.d.ts", defaultTemplate)
    process.exit(0)
}
let configPath = program.args[0] || "R.d.ts"
configPath = pathModule.resolve(process.cwd() + "/", configPath)

if (!fs.existsSync(configPath)) {
    console.error(colors.red(`Can't find config R.d.ts at ${configPath}`))
    program.help()
    process.exit(1)
}
let parser = new TSConfigParser(configPath)
if (program["composer"]) {
    let server = new ComposerServer(parser);
    if (program["composerStylePath"]) {
        server.setStylePath(program["composerStylePath"])
    }
    server.start(program["composerPort"] || 3780)
} else {
    let context = new Context(parser.getTemplatePath())
    if (program["rootName"]) {
        context.rootName = program["rootName"]
    }
    // compile mode
    let files = wrench.readdirSyncRecursive(parser.getTemplatePath()).filter(item => fs.statSync(pathModule.join(parser.getTemplatePath(), item)).isFile())
    let supportedExtnames = [".html", ".widget"]
    for (let file of files) {
        let ext = pathModule.extname(file)
        if (supportedExtnames.indexOf(ext) < 0) continue
        context.includeWidgetByRelativeFilePath(file)
    }
    let output = context.compile()
    if (program["leaf"]) {
        output = `///<reference path="${program["leaf"]}"/>` + "\n" + output
    }
    fs.writeFileSync(parser.getOutputPath(), output)
    let styleOutput = parser.getStyleOutputPath()
    if (styleOutput) {
        let content = context.compileStyle()
        if (content) {
            fs.writeFileSync(styleOutput, content)
        }
    }
}
