package meerkat.voting.storage;

import meerkat.protobuf.Voting.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * A mockup for the StorageManager interface
 * Currently keeps the ElectionParams in a file in the user's home-directory
 */
public class StorageManagerMockup implements StorageManager {

    private static final Logger logger = LoggerFactory.getLogger(StorageManagerMockup.class);

//    public static final String electionParamFullFilename = "/home/hai/meerkat-java/meerkat_election_params_tempfile.dat";
//    public static final String systemMessagesFilename = "/home/hai/meerkat-java/meerkat_booth_system_messages.dat";
    public static final String electionParamFullFilename = "./meerkat_election_params_tempfile.dat";
    public static final String systemMessagesFilename = "./meerkat_booth_system_messages.dat";

    private boolean adminHardwareKeyInserted;


    public StorageManagerMockup () {
        logger.info("A StorageManagerMockup is constructed");
        this.adminHardwareKeyInserted = false;
    }

    @Override
    public boolean isAdminHardwareKeyInserted() {
        logger.info("Entered method isAdminHardwareKeyInserted");
        logger.warn("isAdminHardwareKe01d is not yet fully implemented. It does not check the file system. "
                + "Rather it just returns a private boolean member");
        return adminHardwareKeyInserted;
    }

    @Override
    public ElectionParams readElectionParams() throws IOException {
        logger.info("Entered method readElectionParams");
        ElectionParams params;
        try {
            FileInputStream inputStream = new FileInputStream(electionParamFullFilename);
            params = ElectionParams.parseFrom(inputStream);
            inputStream.close();
            logger.info ("Successfully read election parameter protobuf from a file");
        }
        catch (IOException e) {
            logger.error("Could not read from the election parameter file: '" + electionParamFullFilename + "'.");
            throw e;
        }
        return params;
    }

    @Override
    public void writeElectionParams(ElectionParams params) throws IOException {
        logger.info("Entered method writeElectionParams");
        try {
            FileOutputStream output = new FileOutputStream(electionParamFullFilename);
            params.writeTo(output);
            output.close();
            logger.info ("Successfully wrote election parameter protobuf to a file");
        }
        catch (IOException e) {
            logger.error("Could not write to the election parameter file: '" + electionParamFullFilename + "'.");
            throw e;
        }
    }


    @Override
    public Map<String, UIElement> readSystemMessages() throws IOException {

        logger.info("Entered method readSystemMessages");
        BoothSystemMessages systemMessages;
        try {
            FileInputStream inputStream = new FileInputStream(systemMessagesFilename);
            systemMessages = BoothSystemMessages.parseFrom(inputStream);
            inputStream.close();
            logger.info ("Successfully read systemMessages protobuf from a file");
        }
        catch (IOException e) {
            logger.error("Could not read from the systemMessages file: '" + systemMessagesFilename + "'.");
            throw e;
        }
        return systemMessages.getSystemMessageMap();
    }

}
