package be.msyben.vinylManager.properties;

import be.msyben.vinylManager.tools.Tools;
import be.msyben.vinylManager.tools.log.VinylManagerLogger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

/**
 * Gestion des langues.
 *
 * @author Mathieu Syben
 */
public class Language {

    /**
     * instance
     */
    private static Language _instance = null;
    /**
     * table des propriétés
     */
    Properties language = new Properties();

    /**
     * Récupère l'instance
     *
     * @return l'instance
     */
    public static synchronized Language getInstance() {
        if (_instance == null) {
            _instance = new Language();
        }
        return _instance;
    }

    /**
     * Constructeur
     */
    private Language() {
        load();
    }

    /**
     * Charge le fichier de langue.
     *
     */
    private void load() {
        String filename = Preferences.getInstance().get(Preferences.LANGUAGE_FILE);
        InputStream languageStream = null;
        Reader r = null;
        if (filename == null || filename.equals("")) {
            languageStream = this.getClass().getResourceAsStream("/be/msyben/vinylManager/properties/french.lng");
        } else {
            File f = new File("languages" + System.getProperty("file.separator") + filename + ".lng");
            if (f.exists() && f.isFile()) {
                try {
                    languageStream = new FileInputStream(f);
                } catch (FileNotFoundException ex) {
                    languageStream = this.getClass().getResourceAsStream("/be/msyben/vinylManager/properties/french.lng");
                }
            } else {
                languageStream = this.getClass().getResourceAsStream("/be/msyben/vinylManager/properties/french.lng");
            }

        }

        try {
            r = new InputStreamReader(languageStream, Tools.defaultCharset);
            language.load(r);
        } catch (IOException ex) {
            VinylManagerLogger.log(ex);
        } finally {
            Tools.closeQuietly(languageStream);
            Tools.closeQuietly(r);
        }

        if (language.isEmpty()) {
            languageStream = this.getClass().getResourceAsStream("/be/msyben/vinylManager/properties/french.lng");
            try {
                r = new InputStreamReader(languageStream, Tools.defaultCharset);
                language.load(r);
            } catch (IOException ex1) {
                VinylManagerLogger.log(ex1);
            } finally {
                Tools.closeQuietly(languageStream);
                Tools.closeQuietly(r);
            }
        }
    }

    /**
     * Récupère une propriété
     *
     * @param name le nom
     * @return la propriété associée
     */
    public String get(String name) {
        return language.getProperty(name, "");
    }

    /**
     * Recharge les langues.
     */
    public void reload() {
        language.clear();
        load();
    }
}
