/*
 * Copyright (C) 2013 Snowsun <http://wyrez.org> and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.wyrez.shootingstars.states.util;

import com.jme3.system.AppSettings;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import org.wyrez.shootingstars.helper.PathHelper;

/**
 *
 * @author Snowsun
 */
public class OptionSettings {

    private AppSettings settings;

    public void init() {
        if (saveExists()) {
            init(false);
            load();
        } else {
            init(true);
            save();
        }
    }

    public void init(boolean loadDefault) {
        settings = new AppSettings(loadDefault);
        create(loadDefault);
    }

    public void init(AppSettings settings) {
        settings = new AppSettings(false);
        settings.putAll(settings);
    }

    private void create(boolean loadDefault) {
        if (loadDefault) {
            settings.setResolution(1024, 600);
            settings.setTitle("Shooting Stars");
            settings.setSettingsDialogImage("");
            setMasterVolume(100);
            setFXVolume(100);
            setSoundVolume(100);
            setUsername("Player");
            setPostProcessing(true);
            setParticleDensity(3);
        }
    }

    public void setUsername(String username) {
        settings.putString("Username", username);
    }

    public void setMenuStyle(String style) {
        settings.putString("MenuStyle", style);
    }

    public void setTransitionSpeed(int speed) {
        settings.putInteger("TransitionSpeed", speed);
    }

    public void setMasterVolume(int volume) {
        settings.putInteger("MasterVolume", volume);
    }

    public void setMusicVolume(int volume) {
        settings.putInteger("MusicVolume", volume);
    }

    public void setFXVolume(int volume) {
        settings.putInteger("FXVolume", volume);
    }

    public void setSoundVolume(int volume) {
        settings.putInteger("SoundVolume", volume);
    }

    public void setPostProcessing(boolean enabled) {
        settings.putBoolean("PostProcessing", enabled);
    }

    public void setFullscreen(boolean enabled) {
        settings.setFullscreen(enabled);
    }

    public void setVSync(boolean enabled) {
        settings.setVSync(enabled);
    }

    public void setHeight(int height) {
        settings.setHeight(height);
    }

    public void setWidth(int width) {
        settings.setWidth(width);
    }

    public void setFrequency(int frequency) {
        settings.setFrequency(frequency);
    }

    public void setDepthBits(int depthBits) {
        settings.setDepthBits(depthBits);
    }

    public void setParticleDensity(int particleDensity) {
        settings.putInteger("ParticleDensity", particleDensity);
    }

    public int getKey(String keyId) {
        return settings.getInteger(keyId);
    }

    public String getUsername() {
        return settings.getString("Username");
    }

    public int getMasterVolume() {
        return settings.getInteger("MasterVolume");
    }

    public int getMusicVolume() {
        return settings.getInteger("MusicVolume");
    }

    public int getFXVolume() {
        return settings.getInteger("FXVolume");
    }

    public int getSoundVolume() {
        return settings.getInteger("SoundVolume");
    }

    public boolean isPostprocessingEnabled() {
        return settings.getBoolean("PostProcessing");
    }

    public boolean isFullscreenEnabled() {
        return settings.isFullscreen();
    }

    public boolean isVSyncEnabled() {
        return settings.isVSync();
    }

    public int getHeight() {
        return settings.getHeight();
    }

    public int getWidth() {
        return settings.getWidth();
    }

    public int getFrequency() {
        return settings.getFrequency();
    }

    public int getDepthBits() {
        return settings.getDepthBits();
    }

    public int getParticleDensity() {
        return settings.getInteger("ParticleDensity");
    }

    public void save() {
        try {
            OutputStream os = new FileOutputStream(PathHelper.CONFIG);
            settings.save(os);
        } catch (Exception ex) {
            System.out.println("Can't save settings");
        }
    }

    public void load() {
        try {
            InputStream is = new FileInputStream(PathHelper.CONFIG);
            settings.load(is);
        } catch (Exception ex) {
            System.out.println("Can't load settings");
        }
    }

    public boolean saveExists() {
        return new File(PathHelper.CONFIG).exists();
    }

    public void put(String key, Object value) {
        settings.put(key, value);
    }

    public Set<String> keySet() {
        return settings.keySet();
    }

    public Object get(String key) {
        return settings.get(key);
    }

    public AppSettings getAppSettings() {
        return settings;
    }
}
