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 log = 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(configurationFile);

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

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

			log.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 textualFilePrefixDate) {
		TextualFilePrefixDate = textualFilePrefixDate;
	}

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

	public Path getRealTextualFilePath() {
		return RealTextualFilePath;
	}

}
