package meerkat.voting.gui.configuration;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import meerkat.protobuf.BallotQuestionUiElement.*;
import org.json.JSONException;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Vladimir Eliezer Tokarev on 11/5/2016.
 * This class uses the configuration creator which converts json configuration into normal binary proto file
 */
public class Converter {
    private final static String DEFAULT_OUTPUT_PATH = "configuration- old but gold.bin";
    private final static String DEFAULT_CONFIGURATION_PATH = "VotingBoothConfiguration.json";
    private final static String CONVERTER_DESCRIPTION = "Welcome to simple voting booth " +
            "configuration converter (which converts json voting booth configuration into " +
            "binary configuration).\n-p    (path) stands for the path to the json file configuration " +
            "which we want to convert into binary file.\n-o    (output) stands for the path to which we " +
            "want to write the output binary file.\n-?,/?,help    (help) stands for this description.\n";

    /**
     * Reads the json configuration from given path
     * @return VotingBoothConfiguration object
     * @throws IOException
     * @throws JSONException
     */
    private static VotingBoothConfiguration GetVotingBoothJsonConfiguration(String path) throws IOException, JSONException {
         VotingBoothConfigurationCreator creator = new VotingBoothConfigurationCreator();
         return creator.CreateConfiguration(path);
    }

    /**
     * Converts the configuration file into single binary proto file and writes it to the disc
     * @param configuration the configuration object
     * @param outputPath the path to the new configuraiton file
     */
    private static void ConvertJsonIntoBinaryFile(VotingBoothConfiguration configuration, String outputPath) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(outputPath);
        BallotQuestionsUIElements read_questions =
                BallotQuestionsUIElements.newBuilder()
                        .addQuestions(configuration.NameSelectionByPictureQuestion)
                        .addQuestions(configuration.NameSelectionQuestion)
                        .build();
        read_questions.writeTo(fileOutputStream);
        fileOutputStream.close();
    }

    public static void main(String[] args) throws InterruptedException {
        try {
            OptionParser parser = new OptionParser( "p:o:?*.::" );
            String configurationPath = "";
            String outputPath = "";
            OptionSet options = parser.parse(args);

            if (options.has("?") || options.has("help")){
                System.out.println(CONVERTER_DESCRIPTION);
            }
            if (!options.has("p")){
                configurationPath = DEFAULT_CONFIGURATION_PATH;
            }
            if (options.has("p")){
                configurationPath = (String) options.valueOf("p");
                System.out.println("Configuraiton read from " + configurationPath + ".\n");
            }
            if (!options.has("o")){
                outputPath = DEFAULT_OUTPUT_PATH;
            }
            if (options.has("o")){
                outputPath = (String) options.valueOf("o");
            }

            VotingBoothConfiguration config = GetVotingBoothJsonConfiguration(configurationPath);
            ConvertJsonIntoBinaryFile(config, outputPath);
        }
        catch (Exception e) {
            System.out.println("Exception thrown: " + e + ", " + e.getMessage());
            e.printStackTrace();
        }
    }

}
