package org.josast.property;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;




/**
 * 
 * <b>Description : This class allows to save data in a configuration file.</b>
 * <br>
 * for the JOSAST project the org.josast.config.databinding is prefered to this package for storing data.
 * 
 *  
 * <p>Projet : JOSAST <BR>
 * 
 * <br>
 * <b>JOSAST</b> : Java Open Source Amateur Satellite  Toolbox 
 * <br>The aim of the project is to create a set of tools for amateur satellite purpose. All this tools could be used together to create specific software.
 * <b>JOSAST</b> project is managed by AVMDTI (<A HREF="http://www.avmdti.org">http://www.avmdti.org </a> )
 * <br> This software is an open source software. Please read the <b><i>JOSAST licence</b></i><BR>(<A HREF="http://www.avmdti.org">http://www.avmdti.org </a> )
 * <p>for more information contact <a href="mailto:josast@avmdti.org">josast@avmdti.org</a></p>
 * @author  <a href="mailto:mercier.josast@avmdti.org">mercier</a>
 * @version 1.0
 * <p><b><i> Source Update </b></i></p>
 * <br> Version : date 		: name 		: comments 
 * <br> V1      : 3 mars 2004	: C. Mercier 	: create file
 * <br> V2 		: 01 mai 2019   :	C. Mercier 	: Change log system
 * <p>
 */
public class CONFIG {

	 private Logger log = Logger.getLogger(getClass().getName());
	 private Properties P = null;
	 private String fileName = "EntrySat.ini";

		/** Constructeur priv� */  
	    private CONFIG()
	    {}
	 
	 
	private String getName()
	{
       		String path = System.getProperty("user.dir")+"/config";
    		File apath = new File(path);
    		
    		if (!apath.exists())
    		{
    			boolean res =  apath.mkdir();
    			if (!res)
    			{
    				log.severe("fail to create "+ path);
    				
    			}
    		}
			return path+"/"+fileName;
	}
	 
	 

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

	 public String GetProperty(String Item) {
		 
		if (P==null)
		{
		try {
			P = new Properties();
			FileInputStream in = new FileInputStream(getName());
			P.load(in);
			in.close();
		} catch (Exception e) {
			log.severe  ("erreur ouverture fichier de configuration"); //$NON-NLS-1$
			return null;
		}
		}
		String S = P.getProperty(Item);
		if (S == null)
		{
			return null;
		}
		else
		{
			return S.trim(); //$NON-NLS-1$
		}
	}

	/**
	 * Set a property into the file 
	 * 
	 * 
	 * @param Item Item to store
	 * @param Valeur value to store
	 * 
	 */
	 public void SetProperty(String Item, String Valeur) {
		 
		 if (P==null)
		 {
			 
	
		P = new Properties();
		try {
			

			FileInputStream in = new FileInputStream(getName());
			P.load(in);
			in.close();
		}
		catch (Exception e2) {
			log.severe ("Erreur de ouverture du fichier de configuration"); //$NON-NLS-1$
					e2.printStackTrace();
				}
		 }
		 
			P.put(Item, Valeur); //$NON-NLS-1$
			
		FileOutputStream out=null;
			try {
				out = new FileOutputStream(getName());
			} catch (FileNotFoundException e) {
				log.severe  ("erreur cr�ation de fichier");
				e.printStackTrace();
			}
			try {
				P.store(out, null);
			} catch (IOException e1) {
				log.severe ("erreur sauvegarde");
				e1.printStackTrace();
			}
			try {
				out.close();
			} catch (IOException e3) {
				log.severe ("erreur de fermeture");
				e3.printStackTrace();
			}
		 
	}

	/**
	 * Returns the nomFichier.
	 * @return String
	 */
	public  String getFileName() {
		return fileName;
	}

	/**
	 * Sets the nomFichier.
	 * @param fileName The nomFichier to set
	 */
	public  void setFileName(String nomfichier) {
		fileName = nomfichier;
	}

}
