package org.getopentest;

import java.io.File;
import java.util.Arrays;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.getopentest.base.TestActor;
import org.getopentest.contracts.ITestActor;
import org.getopentest.logging.Logger;
import org.getopentest.util.Encryptor;

public class Main {

    public static void main(String[] args) {
        processCommandLineArgs(args);
        ITestActor actor = new TestActor();

        while (true) {
            try {
                actor.runOneSession();
            } catch (Exception ex) {
                Logger.error("An exception was thrown from the runOneSession method, which shouldn't "
                        + "normally happen and might indicate a bug. Please provide all potentially "
                        + "relevant data to the dev team for a fix (logs, context, etc).", ex);
            }
        }
    }

    private static void processCommandLineArgs(String[] args) {
        OptionParser parser = new OptionParser();

        parser.acceptsAll(Arrays.asList(new String[]{"encrypt"})).withRequiredArg();
        parser.acceptsAll(Arrays.asList(new String[]{"decrypt"})).withRequiredArg();
        parser.acceptsAll(Arrays.asList(new String[]{"p", "password"})).withRequiredArg();
        parser.acceptsAll(Arrays.asList(new String[]{"w", "workdir"})).withRequiredArg();

        OptionSet options = parser.parse(args);

        if (options.has("workdir")) {
            String workDirPath = options.valueOf("workdir").toString();
            File file = new File(workDirPath);

            if (file.exists() && file.isDirectory()) {
                System.setProperty("user.dir", file.getAbsolutePath());
            } else {
                throw new RuntimeException(String.format(
                        "Path \"%s\", specified as the value of the --workdir command "
                        + "line argument, does not exist or is not a directory.",
                        workDirPath));
            }
        }

        if (options.has("encrypt")) {
            if (options.has("password")) {
                String secretData = options.valueOf("encrypt").toString();
                Encryptor encryptor = new Encryptor(options.valueOf("password").toString());
                System.out.println(encryptor.encrypt(secretData));
            } else {
                System.out.println(
                        "To perform the encryption, we'll need a password. You can "
                        + "provide it using the --password argument.");
            }

            System.exit(0);
        }
        
        if (options.has("decrypt")) {
            if (options.has("password")) {
                String encryptedData = options.valueOf("decrypt").toString();
                Encryptor encryptor = new Encryptor(options.valueOf("password").toString());
                System.out.println(encryptor.decrypt(encryptedData));
            } else {
                System.out.println(
                        "To perform the decryption, we'll need a password. You can "
                        + "provide it using the --password argument.");
            }

            System.exit(0);
        }
    }
}
