package leigh.chain.pki.pkitool;

import leigh.chain.pki.PKI;
import leigh.mecha.json.JSONObject;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.util.UniversalJob;
import org.apache.commons.lang.StringUtils;
import org.web3j.crypto.CipherException;

import java.io.IOException;

/**
 * The PKI tool is a low-level command-line tool for originating PKI transactions. Through
 * the use of this tool it is possible to claim names, create secondaries, and issue
 * burn notices.
 * <p>
 * The following environment variables must be set to use this tool:
 *
 * <code>LC_GETH_URL</code> - The GETH RPC URL. e.g. http://localhost:8545
 * <code>LC_PKI_PRIMARY_WALLET</code> - The path to the primary wallet file.
 * <code>LC_PKI_PRIMARY_PW</code> - The password for the primary wallet file.
 * <p>
 * Upon normal operation the tool will output a JSON object to STDOUT containing relevant information. Additional
 * information & logging may be provided to STDERR.
 *
 * @author C. Alexander Leigh
 */
public class PKITool {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(PKITool.class);
    public static String ENV_PRIMARY_WALLET = "LC_PKI_PRIMARY_WALLET";
    public static String ENV_PRIMARY_PW = "LC_PKI_PRIMARY_PW";
    public static String LC_RPC_URL = "LC_RPC_URL";

    public static void main(String[] args) throws CipherException, IOException {
        UniversalJob.banner(logger, "PKITool");

        String wallet = System.getenv(ENV_PRIMARY_WALLET);
        String walletPw = System.getenv(ENV_PRIMARY_PW);
        String gethUrl = System.getenv(LC_RPC_URL);

        if (StringUtils.isEmpty(wallet)) {
            logger.error(ENV_PRIMARY_WALLET + " environment variable must be set.");
            System.exit(UniversalJob.RET_BADENV);
        }

        if (StringUtils.isEmpty(walletPw)) {
            logger.error(ENV_PRIMARY_PW + " environment variable must be set.");
            System.exit(UniversalJob.RET_BADENV);
        }

        if (StringUtils.isEmpty(gethUrl)) {
            logger.error(LC_RPC_URL + " environment variable must be set.");
            System.exit(UniversalJob.RET_BADENV);
        }

        if (args.length != 3) {
            logger.info("Usage: PKITool name validityDays serviceUrl");
            System.exit(UniversalJob.RET_BADARGS);
        }

        // PkiTool name validityPeriod serviceUrl
        String name = args[0];
        int validityDays = Integer.parseInt(args[1]);
        String serviceUrl = args[2];

        String txnHash = PKI.submitNameClaim(gethUrl, wallet, walletPw, name, serviceUrl, validityDays);

        if (txnHash == null) {
            logger.error("Submission failed.");
            JSONObject jo = new JSONObject();
            jo.put("status", "error");
            System.out.println(jo);
            System.exit(UniversalJob.RET_ERROR);
        }

        logger.info("Name claim submitted. [txnHash: {}] [name: {}] [validity: {}] [serviceUrl: {}]",
                txnHash, name, validityDays, serviceUrl);

        JSONObject jo = new JSONObject();
        jo.put("txnHash", txnHash);
        jo.put("status", "ok");
        System.out.println(jo);
    }
}
