package ruc.irm.wikit.common.conf;

import org.apache.commons.cli.*;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Configuration factory, use this method to createWeiboConf application configuration.
 * <p/>
 * User: xiatian
 * Date: 4/16/14
 * Time: 12:25 PM
 */
public class ConfFactory {
    private static Map<String, Conf> confs = new HashMap<>();

    private static Conf defaultConf = null;

    public static Conf defaultConf() {
        if (defaultConf == null) {
            defaultConf = new Conf();
        }
        return defaultConf;
    }

    public static Conf createCommonConf() {
        return createConf("conf-common.xml", false);
    }

    public static Conf createZhConf() {
        return createConf("conf-chinese.xml", false);
    }

    public static Conf createEnConf() {
        return createConf("conf-english.xml", false);
    }

    public static Conf createConf(String resource, boolean isFileResource) {
        Conf conf = confs.get(resource);
        if (conf == null) {
            conf = new Conf();
            if (isFileResource) {
                File f = new File(resource);
                if(!f.exists()) {
                    System.err.println("File does not exist when loading " +
                            "resource from " + f.getAbsolutePath());
                    System.exit(1);
                } else {
                    conf.addResource(f);
                }
            } else {
                conf.addResource(resource);
            }

            if (conf.getBoolean("common.debug", false)) {
                System.out.println(StringUtils.center("Configuration", 30));

                try {
                    conf.writeXml(System.out);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            confs.put(resource, conf);
        }

        return conf;
    }


    /**
     * Create Commandline options for configuration, this method can be used
     *
     * @return
     */
    public static Options createConfCmdOptions() {
        Options options = new Options();
        options.addOption("h", false, "Print help");

        options.addOption("c", true, "specify config file, or en for default English wiki config, " +
                "or zh for default Chinese wiki config");//参数可用

        return options;
    }

    public static Conf createConf(String[] args) throws ParseException {
        return createConf(args, createConfCmdOptions());
    }

    public static Conf createConf(String[] args, Options options) throws ParseException {
        HelpFormatter hf = new HelpFormatter();

        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("h")) {
            hf.printHelp("Options", options);
        } else if (cmd.hasOption("c")) {
            String value = cmd.getOptionValue("c");
            if (value.equals("en")) {
                return createEnConf();
            } else if (value.equals("zh")) {
                return createZhConf();
            } else if (!new File(value).exists()) {
                System.out.println("the config file does not exist ==> " + value);
            } else {
                return ConfFactory.createConf(value, true);
            }
        } else {
            hf.printHelp("Options", options);
        }
        return null;
    }

}

@@ -829,16 +829,21 @@
 nf(%22conf
+/conf
 -chinese
@@ -935,16 +935,21 @@
 nf(%22conf
+/conf
 -english
@@ -3515,10 +3515,236 @@
 %0A    %7D%0A%0A
+    public static void main(String%5B%5D args) %7B%0A        Conf conf = ConfFactory.createZhConf();%0A        System.out.println(%22lang:%22 + conf.getEsaLanguage());%0A        System.out.println(%22prefix:%22 + conf.get(%22redis.prefix%22));%0A    %7D%0A
 %7D%0A

update the place of default config file