package configgen;

import configgen.data.Datas;
import configgen.define.ConfigCollection;
import configgen.gen.Generator;
import configgen.type.Cfgs;
import configgen.value.CfgVs;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public final class Main {
    private static void usage(String reason) {
        System.err.println(reason);

        System.out.println("Usage: java -jar configgen.jar [options]");
        System.out.println("	-configdir  config data directory.");
        System.out.println("	-xml        default config.xml in datadir.");
        System.out.println("	-encoding   csv and xml encoding. default GBK");
        System.out.println("	-v          verbose, default no");
        Generator.providers.forEach((k, v) -> System.out.println("	-gen        " + k + "," + v.usage()));
        Runtime.getRuntime().exit(1);
    }

    public static void main(String[] args) throws Exception {
        String configdir = null;
        String xml = null;
        String encoding = "GBK";
        List<Generator> generators = new ArrayList<>();

        for (int i = 0; i < args.length; ++i) {
            switch (args[i]) {
                case "-configdir":
                    configdir = args[++i];
                    break;
                case "-xml":
                    xml = args[++i];
                    break;
                case "-encoding":
                    encoding = args[++i];
                    break;
                case "-gen":
                    Generator generator = Generator.create(args[++i]);
                    if (generator == null)
                        usage("");
                    generators.add(generator);
                    break;
                case "-v":
                    Logger.enableVerbose();
                    break;
                default:
                    usage("unknown args " + args[i]);
                    break;
            }
        }

        if (configdir == null) {
            usage("-configdir miss");
            return;
        }

        Path dir = Paths.get(configdir);
        File xmlFile = xml != null ? new File(xml) : dir.resolve("config.xml").toFile();
        Logger.verbose("parse xml " + xmlFile);
        ConfigCollection define = new ConfigCollection(xmlFile);
        Cfgs type = new Cfgs(define);
        type.resolve();

        Logger.verbose("read data " + dir + " then auto complete xml");
        Datas data = new Datas(dir, encoding);
        data.autoCompleteDefine(type);
        define.save(xmlFile, encoding);
        Cfgs newType = new Cfgs(define);
        newType.resolve();

        Logger.verbose("verify constraint");
        CfgVs value = new CfgVs(newType, data);
        value.verifyConstraint();

        for (Generator generator : generators) {
            Logger.verbose("generate " + generator.context.arg);
            generator.generate(dir, value);
        }

        Logger.verbose("end");
    }
}

@@ -88,16 +88,62 @@
 ction;%0D%0A
+import configgen.gen.CachedFileOutputStream;%0D%0A
 import c
@@ -167,16 +167,16 @@
 rator;%0D%0A
-
 import c
@@ -251,16 +251,45 @@
 .File;%0D%0A
+import java.nio.file.Files;%0D%0A
 import j
@@ -389,16 +389,159 @@
 l.List;%0D
+%0Aimport java.util.zip.CRC32;%0D%0Aimport java.util.zip.CheckedOutputStream;%0D%0Aimport java.util.zip.ZipEntry;%0D%0Aimport java.util.zip.ZipOutputStream;%0D
 %0A%0D%0Apubli
@@ -1113,24 +1113,105 @@
 usage()));%0D%0A
+        System.out.println(%22%09-packtext   for i18n, pack text.csv to text.zip%22);%0D%0A
         Runt
@@ -2182,24 +2182,24 @@
 Verbose();%0D%0A
-
             
@@ -2205,32 +2205,543 @@
          break;%0D
+%0A%0D%0A                case %22-packtext%22:%0D%0A                    File file = new File(args%5B++i%5D);%0D%0A                    try (ZipOutputStream zos = new ZipOutputStream(new CheckedOutputStream(new CachedFileOutputStream(new File(%22text.zip%22)), new CRC32()))) %7B%0D%0A                        ZipEntry ze = new ZipEntry(%22text.csv%22);%0D%0A                        ze.setTime(0);%0D%0A                        zos.putNextEntry(ze);%0D%0A                        Files.copy(file.toPath(), zos);%0D%0A                    %7D%0D%0A                    return;%0D
 %0A               

add -packtext for i18n