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.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

/**
 * Cette classe gère les préférences pour l'application.
 *
 * @author Mathieu Syben
 */
public class Preferences {

    public static final String OPEN_LAST_FILE = "open.last.file";
    public static final String RESIZE_PICTURES = "resize.pictures";
    public static final String LAST_VERSION_CHECK = "last.version.check";
    public static final String MINIMIZE_TO_TRAY = "minimize.to.tray";
    public static final String REMOVE_PREFIX = "remove.prefix";
    public static final String SHOW_NBR_ALBUMS = "show.nbr.albums";
    public static final String COMPILATION_MODE = "compilation.mode";
    public static final String LANGUAGE_FILE = "language.file";
    public static final String EXPORT_HTML_FILE = "export.html.file";
    public static final String AFFICHAGE = "affichage";
    public static final String STANDARD_NAME_UPPER_CASE = "standard.name.upper.case";
    public static final String TREE_DISPLAY_SUB = "tree.display.sub";
    public static final String TREE_DISPLAY_ROOT = "tree.display.root";
    public static final String TREE_CRITERE_1 = "tree.critere.1";
    public static final String TREE_CRITERE_2 = "tree.critere.2";
    public static final String TREE_CRITERE_3 = "tree.critere.3";
    public static final String SELECTIONNABLE_CRITERE = "selectionnable.critere";
    public static final String POLICE_SIZE = "police.size";
    public static final String POLICE = "police";
    public static final String LAST_OPENED_FILE = "last.opened.file";
    public static final String FILE_HISTORY = "file.history";
    public static final String LAST_IMAGE_DIR = "last.image.dir";
    public static final String MAIN_WINDOW_WIDTH = "main.window.width";
    public static final String MAIN_WINDOW_HEIGHT = "main.window.height";
    public static final String MAIN_WINDOW_STATE = "main.window.state";
    public static final String SORT_ARTIST_DESC = "sort.artist.desc";
    public static final String SEARCH_PARAMS = "searchParams";
    public static final String DISPLAY_DURATION_ALBUM = "display.duration.album";
    public static final String DISPLAY_DURATION_DISC = "display.duration.disque";
    public static final String DISPLAY_DURATION_SIDE = "display.duration.face";
    public static final String DATE_FORMAT = "date.format";
    
    public static final String EXPORT_TABLE_FIELDS = "export.table.fields";
    
    /*public static final String DISCOGS_ACCESS_TOKEN_KEY = "discogs.oauth.access.token.key";
    public static final String DISCOGS_ACCESS_TOKEN_SECRET = "discogs.oauth.access.token.secret";*/
    public static final String DISCOGS_CONSUMER_TOKEN_KEY = "discogs.oauth.consumer.token.key";
    public static final String DISCOGS_CONSUMER_TOKEN_SECRET = "discogs.oauth.consumer.token.secret";
    public static final String DISCOGS_TOKEN = "discogs.token";
    
    public static final String PATH_LOAD = "load.path";
    public static final String PATH_SAVE = "save.path";
    public static final String PATH_EXPORT="export.path";
    public static final String PATH_IMPORT="import.path";
    public static final String PATH_TRACKFILE="trackfile.path";
    
    public static final String HTTP_PROXY_URL = "http.proxy.url";
    public static final String HTTP_PROXY_PORT = "http.proxy.port";
    public static final String HTTPS_PROXY_URL = "https.proxy.url";
    public static final String HTTPS_PROXY_PORT = "https.proxy.port";
    
    /**
     * Le nom du fichier de config
     */
    private static final String filename = ".vinylManager.ini";
    /**
     * Instance
     */
    private static Preferences _instance = null;
    /**
     * table des préférence
     */
    private Properties preferences = new Properties();

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

    /**
     * Creates a new instance of Preferences
     */
    private Preferences() {
        load();
    }

    /**
     * Charge le fichier de config.
     */
    private void load() {
        File f = new File(System.getProperty("user.home", "/temp") + File.separator + filename);
        if (f.exists()) {
            InputStream is = null;
            try {
                is = new FileInputStream(f);
                preferences.load(is);
            } catch (IOException e) {
                VinylManagerLogger.log(e);
            } finally {
                Tools.closeQuietly(is);
            }
        }
    }

    /**
     * Sauve le fichier de config.
     */
    public void save() {
        File f = new File(System.getProperty("user.home", "/temp") + File.separator + filename);
        OutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            preferences.store(fos, "");
            fos.flush();
        } catch (IOException e) {
            VinylManagerLogger.log(e);
        } finally {
            Tools.closeQuietly(fos);
        }
    }

    /**
     * Renvoie une préférence
     *
     * @param name le nom de la préférence
     * @return la préférence correspondant au nom. null si inconnu
     */
    public String get(String name) {
        return preferences.getProperty(name);
    }

    /**
     * Renvoie une préférence
     *
     * @param name le nom de la préférence
     * @param defaultValue la valeur par défaut
     * @return la préférence correspondant au nom. null si inconnu
     */
    public String get(String name, String defaultValue) {
        return preferences.getProperty(name, defaultValue);
    }

    /**
     * Fixe une propriété
     *
     * @param name le nom
     * @param value la valeur
     */
    public void set(String name, String value) {
        preferences.put(name, value);
    }
    
    public void remove(String name) {
        preferences.remove(name);
    }
}
