class CmdHttpCaller {

    public static void main(String[] args) {
        CommandLineParser parser = new DefaultParser();

        Boolean isDebug = false;
        try {
            CommandLine commandline = parser.parse(opt, args);
            if (commandline.getOptions().length == 0 && commandline.getArgs().length > 0) {
                //use old style cmd line
                CmdCaller.main(args);
                return;
            }
            if (commandline.hasOption("h")) {
                usage(null);
                return;
            }

            String ak = commandline.getOptionValue("ak");
            String sk = commandline.getOptionValue("sk");
            String api = commandline.getOptionValue("api");
            String version = commandline.getOptionValue("version");
            Boolean sdkv = commandline.hasOption("sdkv");
            String method = commandline.getOptionValue("method");
            String[] headers = commandline.getOptionValues("H");
            String[] params = commandline.getOptionValues("D");
            String url = commandline.getOptionValue("url");
            String proxy = commandline.getOptionValue("proxy");
            String cbJSON = commandline.getOptionValue("cbJSON");
            boolean nonce = commandline.hasOption("nonce");
            String bizIdKey = commandline.getOptionValue("bizIdKey");
            String bizId = commandline.getOptionValue("bizId");
            isDebug = commandline.hasOption("d");

            if (sdkv) {
                Version.version();
                return;
            }

            String signImpl = commandline.getOptionValue("sign");
            String verifySignImpl = commandline.getOptionValue("verify");

            if (isDebug) {
                System.out.println("url=" + url);
                System.out.println("api=" + api);
                System.out.println("version=" + version);
                System.out.println("bizIdKey=" + bizIdKey);
                System.out.println("bizId=" + bizId);
                System.out.println("ak=" + ak);
                System.out.println("sk=" + sk);
                System.out.println("proxy=" + proxy);
                System.out.println("nonce=" + nonce);
                System.out.println("signImpl=" + signImpl);
                System.out.println("verifySignImpl=" + verifySignImpl);
                printKV("HTTP Headers", headers);
                printKV("HTTP Params", params);
            }

            if (isEmpty(api)) {
                usage(MessageHelper.getMessage("cli.defparam", "-api"));
                return;
            }

            if (isEmpty(version)) {
                usage(MessageHelper.getMessage("cli.defparam", "-version"));
                return;
            }

            if (isEmpty(url)) {
                usage(MessageHelper.getMessage("cli.defparam", "-url"));
                return;
            }

            if (method == null) {
                method = "get";
            }
            HttpParameters.Builder builder = HttpParameters.newBuilder();
            if (bizIdKey != null && !bizIdKey.trim().equals("")) {
                HttpCaller.bizIdKey(bizIdKey);
            }
            builder.api(api).version(version).method(method).bizId(bizId).requestURL(url).accessKey(ak).secretKey(sk).signImpl(signImpl).verifySignImpl(verifySignImpl);

            if (headers != null) {
                for (String header : headers) {
                    String[] kv = header.split(":", 2);
                    if (kv == null || kv.length != 2) {
                        System.out.println("" + header);
                        return;
                    }
                    builder.putHeaderParamsMap(kv[0], kv[1]);
                }
            }

            if (params != null) {
                for (String param : params) {
                    String[] kv = param.split("=", 2);
                    if (kv == null || kv.length != 2) {
                        System.out.println(MessageHelper.getMessage("cli.defh", param));
                        return;
                    }
                    builder.putParamsMap(kv[0], kv[1]);
                }
            }

            if (cbJSON != null) {
                if ("cget".equalsIgnoreCase(method) || "get".equalsIgnoreCase(method)) {
                    System.out.println(MessageHelper.getMessage("cli.defpost"));
                    return;
                }
                if (cbJSON.startsWith("'")) {
                    System.out.println(MessageHelper.getMessage("cli.json.prefix"));
                    return;
                }
                builder.contentBody(new ContentBody(cbJSON));
            }

            builder.nonce(nonce);

            if (isDebug) {
                builder.diagnostic(true); //打印诊断信息
            }

            boolean curlOnly = false;
            if (method.toLowerCase().startsWith("c")) {
                curlOnly = true;
                HttpCaller.setCurlResponse(true);
            }

            StringBuffer resHttpHeaders = new StringBuffer();
            //set http proxy
            if (proxy != null) {
                String errMsg = MessageHelper.getMessage("cli.errproxy", proxy);
                String[] pcs = proxy.split(":");
                if (pcs == null || pcs.length != 2) {
                    System.out.println(errMsg);
                    return;
                }
                try {
                    HttpCaller.setProxyHost(pcs[0], Integer.parseInt(pcs[1]), null);
                } catch (Exception e) {
                    System.out.println(errMsg);
                    return;
                }
            }

            HttpReturn ret = HttpCaller.invokeReturn(builder.build());

            if (curlOnly) {
                System.out.println("---- curlString = " + ret.getResponseStr());
            } else {
                if (isDebug) {
                    System.out.println("Diagnostic Info:" + ret.diagnosticInfo);
                }
                System.out.println("---- response http headers = " + ret.responseHeaders);
                System.out.println("---- retStr = " + ret.getResponseStr());

                //call multi-times for stress or flow-ctrl testing
                int times = Integer.getInteger("test.stress.times", 0);
                for (int i = 2; i <= times; i++) {
                    ret = HttpCaller.invokeReturn(builder.build());
                    System.out.println("---- retStr [#" + i + "] = " + ret.getResponseStr());
                }
            }
        } catch (Exception e) {
            System.out.println("-- operation error：" + e.getMessage());
            //if (isDebug)
            e.printStackTrace(System.out);
        }
    }

}