package org.josast.app.amicalsat.cli;

import java.nio.file.Path;
import java.util.logging.Logger;

import org.josast.property.CONFIG;
import org.josast.property.ConfigObject;


public class AmicalsatCliConf extends ConfigObject {

	private Logger logger = Logger.getLogger(getClass().getName());

	private String configurationFile = "defaultConfigFile.ini";
	private String smBinarieOutputFile = null;
	private String smBinarieOutputDirectory = "SoundModemOutpt";
	private boolean smGenerateBinarieFile = false;
	private String textualFile = null;
	private boolean generateTextualOutputFile = false;
	private String outputTextualFileRepository = "OuputTextual";
	private String configFileDefined = "true";
	private boolean textualFilePrefixDate = true;
	private String smIPadress = "localhost";
	private int smPort = 8100;
	private boolean isInitialised = false;

	protected CONFIG configFile = CONFIG.getInstance();

	private Path RealTextualFilePath;
	
	private AmicalsatCliConf()
	{
		
	}
    /** Holder */
    private static class SingletonHolder
    {       
        /** Instance unique non pr�initialis�e */
        private final static AmicalsatCliConf instance = new AmicalsatCliConf();
    }
 
    /** Point d'acc�s pour l'instance unique du singleton */
    public static AmicalsatCliConf getInstance()
    {
    	SingletonHolder.instance.setConfigurationFile(SingletonHolder.instance.configurationFile);
     	SingletonHolder.instance.initialise(SingletonHolder.instance.configurationFile);
        return SingletonHolder.instance;

    }
    
    public static AmicalsatCliConf getInstance(String configFilename)
    {
    	SingletonHolder.instance.configurationFile  = configFilename;
    	SingletonHolder.instance.initialise(configFilename);
        return SingletonHolder.instance;
    }
    
	private void initialise(String configFilename) {
		
		
		if (!isInitialised)
		{
		// vérifie si la propriété est dans le fichier
		configFile.setFileName(configFilename);

		String temp = configFile.GetProperty("configFileDefined");

		if (temp == null) {
			// config file not initialised

			logger.severe("properties file not initialised - a file is created with properties with defaults value");
			System.out.println("config file does not content required properties \n the file " + configFilename
					+ " has been set up with default properties to configure");
			save();

		} else {

			initialiseFromProperties();
		}
		
		isInitialised=true;
		}
	}

	private void initialiseFromProperties() {
		configFileDefined = configFile.GetProperty("configFileDefined");
		smIPadress = configFile.GetProperty("smIPadress");
		String portString = configFile.GetProperty("smPort");
		smPort = Integer.parseInt(portString);
		smGenerateBinarieFile = Boolean.getBoolean(configFile.GetProperty("smGenerateBinarieFile"));
		smBinarieOutputFile = configFile.GetProperty("smBinarieOutputFile");
		textualFile = configFile.GetProperty("textualFile");
		configurationFile = configFile.GetProperty("configurationFile");
		generateTextualOutputFile = Boolean.getBoolean(configFile.GetProperty("generateTextualOutputFile"));
		outputTextualFileRepository = configFile.GetProperty("outputTextualFileRepository");
		smBinarieOutputDirectory = configFile.GetProperty("smBinarieOutputDirectory");
	}

	public void save() {
		configFile.SetProperty("configFileDefined", configFileDefined);
		configFile.SetProperty("smPort", "" + smPort);
		configFile.SetProperty("smIPadress", "" + smIPadress);
		configFile.SetProperty("smBinarieOutputFile", "" + smBinarieOutputFile);
		configFile.SetProperty("smGenerateBinarieFile", "" + smGenerateBinarieFile);
		configFile.SetProperty("textualFile", "" + textualFile);
		configFile.SetProperty("generateTextualOutputFile", "" + generateTextualOutputFile);
		configFile.SetProperty("outputTextualFileRepository", "" + outputTextualFileRepository);		
		configFile.SetProperty("configurationFile", "" + configurationFile);
		configFile.SetProperty("smBinarieOutputDirectory", "" + smBinarieOutputDirectory);
		

	}

	public String getConfigurationFile() {
		return configurationFile;
	}

	public void setConfigurationFile(String configurationFile) {
		this.configurationFile = configurationFile;
	}

	public String getSmBinarieOutputFile() {
		return smBinarieOutputFile;
	}

	public void setSmBinarieOutputFile(String binariefile) {
		this.smBinarieOutputFile = binariefile;
		smGenerateBinarieFile = true;
	}

	public String getTextualFile() {
		return textualFile;
	}

	public void setTextualFile(String textualFile) {
		this.textualFile = textualFile;
		generateTextualOutputFile = true;
	}

	public boolean isSmGenerateBinarieFile() {
		return smGenerateBinarieFile;
	}

	public void setSmGenerateBinarieFile(boolean smGenerateBinarieFile) {
		this.smGenerateBinarieFile = smGenerateBinarieFile;
	}

	public boolean isGenerateTextualOutputFile() {
		return generateTextualOutputFile;
	}

	public void setGenerateTextualOutputFile(boolean smTextualOutputFile) {
		this.generateTextualOutputFile = smTextualOutputFile;
	}

	public String getOutputTextualFileRepository() {
		return outputTextualFileRepository;
	}

	public void setOutputTextualFileRepository(String outputTextualFileRepository) {
		this.outputTextualFileRepository = outputTextualFileRepository;
	}

	public boolean isTextualFilePrefixDate() {
		return textualFilePrefixDate;
	}

	public void setTextualFilePrefixDate(boolean textualFilePrefixDateInput) {
		textualFilePrefixDate = textualFilePrefixDateInput;
	}

	public void setRealTextualFilePath(Path filepath) {
		RealTextualFilePath = filepath;
	}

	public Path getRealTextualFilePath() {
		return RealTextualFilePath;
	}

}
