/*
 * 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.gui;

import com.jme3.asset.AssetManager;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.math.Vector2f;
import org.wyrez.shootingstars.gui.controls.ButtonBase;
import org.wyrez.shootingstars.gui.listener.OptionsListener;
import org.wyrez.shootingstars.helper.DisplayHelper;
import org.wyrez.shootingstars.helper.ScreenHelper;
import org.wyrez.shootingstars.states.util.OptionSettings;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.buttons.CheckBox;
import tonegod.gui.controls.lists.ComboBox;
import tonegod.gui.controls.lists.Slider;
import tonegod.gui.controls.text.Label;
import tonegod.gui.controls.text.TextField;
import tonegod.gui.core.Screen;

/**
 *
 * @author Snowsun
 */
public class OptionsGUI extends BackgroundedGui implements Gui {

    private OptionsListener listener;
    private OptionSettings settings;
    private ScreenHelper screenHelper;
    //General Controls
    private TextField txtPlayerName;
    private CheckBox chkEnableAudioVisualization;
    //Video Controls
    private ComboBox cboResolution;
    private ComboBox cboFrequency;
    private CheckBox chkFullscreen;
    private CheckBox chkVSync;
    private CheckBox chkShowWeapon;
    private Slider sldParticleDensity;
    //Audio Controls
    private Slider sldMasterVolume;
    private Slider sldMenuVolume;
    private Slider sldMusicVolume;

    public OptionsGUI(Screen screen, OptionsListener listener, OptionSettings settings,
            ScreenHelper screenHelper, AssetManager assetManager) {
        super(screen, new Vector2f(0f, 0f), new Vector2f(1280f, 720f), assetManager); //create for full hd
        this.listener = listener;
        this.settings = settings;
        this.screenHelper = screenHelper;
        this.setIgnoreMouse(true);
        this.setIsVisible(false);
        create();
        readControlValues();
    }

    public void attach() {
        screen.addElement(this);
    }

    public void detach() {
        screen.removeElement(this);
    }

    private void create() {
        float tabControlStartPointx = screenHelper.calcX(32f);
        float tabControlStartPointy = screenHelper.calcY(18f);

        float buttonWidth = screenHelper.calcX(153.6f);
        float buttonHeight = screenHelper.calcY(40f);

        float labelFontSize = screenHelper.calcX(20f);

        createGeneral(labelFontSize);
        createVideo(labelFontSize);
        createAudio(labelFontSize);

        float differnceTabControlButtons = screenHelper.calcY(622.8f);

        Button btnSave = new ButtonBase(screen, new Vector2f(tabControlStartPointx + screenHelper.calcX(877f), tabControlStartPointy + differnceTabControlButtons), new Vector2f(buttonWidth, buttonHeight)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                saveControlValues();
                listener.save();
            }
        };
        btnSave.setText("Save");
        btnSave.setFontSize(labelFontSize);

        Button btnCancel = new ButtonBase(screen, new Vector2f(tabControlStartPointx + screenHelper.calcX(1060f), tabControlStartPointy + differnceTabControlButtons), new Vector2f(buttonWidth, buttonHeight)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.cancel();
            }
        };
        btnCancel.setText("Cancel");
        btnCancel.setFontSize(labelFontSize);

        this.addChild(btnSave);
        this.addChild(btnCancel);
    }

    private void createGeneral(float labelFontSize) {
        float startPointx = screenHelper.calcX(64f);
        float startPointy = screenHelper.calcY(36f);
        float marginLeftControls = screenHelper.calcX(115.8f);

        float startPointAudioVisualx = screenHelper.calcX(464f);
        float startPointAudioVisualy = screenHelper.calcY(36f);
        float marginLeftAudioVisualControl = screenHelper.calcX(190f);

        Label lblPlayerName = new Label(screen, new Vector2f(startPointx, startPointy), new Vector2f(screenHelper.calcX(120f), screenHelper.calcY(40f)));
        lblPlayerName.setText("Player Name");
        lblPlayerName.setFontSize(labelFontSize);

        txtPlayerName = new TextField(screen, new Vector2f(startPointx + marginLeftControls, startPointy + screenHelper.calcY(7f)), new Vector2f(screenHelper.calcX(200f), screenHelper.calcY(30f)));
        txtPlayerName.setFontSize(labelFontSize);

        Label lblEnableAudioVisualization = new Label(screen, new Vector2f(startPointAudioVisualx, startPointAudioVisualy), new Vector2f(screenHelper.calcX(200f), screenHelper.calcY(40f)));
        lblEnableAudioVisualization.setText("Enable Audio Visualization");
        lblEnableAudioVisualization.setFontSize(labelFontSize);

        chkEnableAudioVisualization = new CheckBox(screen, new Vector2f(startPointAudioVisualx + marginLeftAudioVisualControl, startPointAudioVisualy + screenHelper.calcY(12f)), new Vector2f(screenHelper.calcX(20f), screenHelper.calcX(20f)));

        this.addChild(lblPlayerName);
        this.addChild(txtPlayerName);
        this.addChild(lblEnableAudioVisualization);
        this.addChild(chkEnableAudioVisualization);
    }

    private void createVideo(float labelFontSize) {

        float startPointx = screenHelper.calcX(64f);
        float startPointy = screenHelper.calcY(90f);
        float marginLeftControls = screenHelper.calcX(115.8f);
        float labelWitdh = screenHelper.calcX(120f);
        float labelHeight = screenHelper.calcY(40f);

        float comboBoxWidth = screenHelper.calcX(200f);
        float comboBoxHeight = screenHelper.calcY(30f);

        float checkBoxWitdh = screenHelper.calcX(20f);
        float checkBoxHeight = screenHelper.calcX(20f);

        float sliderWitdh = screenHelper.calcX(200f);
        float sliderHeight = screenHelper.calcY(30f);

        Label lblFullscreen = new Label(screen, "lblFullscreen", new Vector2f(startPointx, startPointy), new Vector2f(labelWitdh, labelHeight));
        lblFullscreen.setText("Fullscreen");
        lblFullscreen.setFontSize(labelFontSize);

        chkFullscreen = new CheckBox(screen, new Vector2f(startPointx + marginLeftControls, startPointy + screenHelper.calcY(12f)), new Vector2f(checkBoxWitdh, checkBoxHeight));

        Label lblVSync = new Label(screen, "lblVSync", new Vector2f(startPointx + screenHelper.calcX(400f), startPointy), new Vector2f(labelWitdh, labelHeight));
        lblVSync.setText("VSync");
        lblVSync.setFontSize(labelFontSize);

        chkVSync = new CheckBox(screen, new Vector2f(startPointx + marginLeftControls + screenHelper.calcX(400f), startPointy + screenHelper.calcY(12f)), new Vector2f(checkBoxWitdh, checkBoxHeight));

        Label lblPostProcessing = new Label(screen, "lblPostProcessing", new Vector2f(startPointx + screenHelper.calcX(800f), startPointy), new Vector2f(labelWitdh, labelHeight));
        lblPostProcessing.setText("Show Weapon");
        lblPostProcessing.setFontSize(labelFontSize);

        chkShowWeapon = new CheckBox(screen, new Vector2f(startPointx + marginLeftControls + screenHelper.calcX(800f), startPointy + screenHelper.calcY(12f)), new Vector2f(checkBoxWitdh, checkBoxHeight));

        Label lblResolution = new Label(screen, "lblResolution", new Vector2f(startPointx, startPointy + screenHelper.calcY(50f)), new Vector2f(labelWitdh, labelHeight));
        lblResolution.setText("Resolution");
        lblResolution.setFontSize(labelFontSize);

        cboResolution = new ComboBox(screen, new Vector2f(startPointx + marginLeftControls, startPointy + screenHelper.calcY(57f)), new Vector2f(comboBoxWidth, comboBoxHeight)) {
            @Override
            public void onChange(int selectedIndex, Object value) {
            }
        };
        cboResolution.setFontSize(labelFontSize);
        for (String s : DisplayHelper.getResolutions()) {
            cboResolution.addListItem(s, s);
        }

        Label lblFrequency = new Label(screen, "lblFrequency", new Vector2f(startPointx + screenHelper.calcX(400f), startPointy + screenHelper.calcY(50f)), new Vector2f(labelWitdh, labelHeight));
        lblFrequency.setText("Frequency");
        lblFrequency.setFontSize(labelFontSize);

        cboFrequency = new ComboBox(screen, new Vector2f(startPointx + marginLeftControls + screenHelper.calcX(400f), startPointy + screenHelper.calcY(57f)), new Vector2f(comboBoxWidth, comboBoxHeight)) {
            @Override
            public void onChange(int selectedIndex, Object value) {
            }
        };
        cboFrequency.setFontSize(labelFontSize);
        for (String s : DisplayHelper.getFrequencys()) {
            cboFrequency.addListItem(s, s);
        }

        Label lblParticleDensity = new Label(screen, "lblParticleDensity", new Vector2f(startPointx + screenHelper.calcX(800f), startPointy + screenHelper.calcY(50f)), new Vector2f(labelWitdh, labelHeight));
        lblParticleDensity.setText("Particle Density");
        lblParticleDensity.setFontSize(labelFontSize);

        sldParticleDensity = new Slider(screen, new Vector2f(startPointx + marginLeftControls + screenHelper.calcX(800f), startPointy + screenHelper.calcY(57f)), new Vector2f(sliderWitdh, sliderHeight), Slider.Orientation.HORIZONTAL, false) {
            @Override
            public void onChange(int selectedIndex, Object value) {
            }
        };
        sldParticleDensity.setStepIntegerRange(0, 3, 1);


        this.addChild(lblVSync);
        this.addChild(chkVSync);
        this.addChild(lblFullscreen);
        this.addChild(chkFullscreen);
        this.addChild(lblFrequency);
        this.addChild(cboFrequency);
        this.addChild(lblResolution);
        this.addChild(cboResolution);
        this.addChild(lblPostProcessing);
        this.addChild(chkShowWeapon);
        this.addChild(lblParticleDensity);
        this.addChild(sldParticleDensity);
    }

    private void createAudio(float labelFontSize) {
        float startPointx = screenHelper.calcX(64f);
        float startPointy = screenHelper.calcY(290f);
        float marginLeftControls = screenHelper.calcX(115.8f);

        float labelWitdh = screenHelper.calcX(120f);
        float labelHeight = screenHelper.calcY(40f);

        float sliderWitdh = screenHelper.calcX(200f);
        float sliderHeight = screenHelper.calcY(30f);

        Label lblMasterVolume = new Label(screen, "lblMasterVolume", new Vector2f(startPointx, startPointy), new Vector2f(labelWitdh, labelHeight));
        lblMasterVolume.setText("Master Volume");
        lblMasterVolume.setFontSize(labelFontSize);

        sldMasterVolume = new Slider(screen, new Vector2f(startPointx + marginLeftControls, startPointy + screenHelper.calcY(7f)), new Vector2f(sliderWitdh, sliderHeight), Slider.Orientation.HORIZONTAL, false) {
            @Override
            public void onChange(int selectedIndex, Object value) {
            }
        };
        sldMasterVolume.setStepIntegerRange(0, 100, 1);

        Label lblFXVolume = new Label(screen, "lblFXVolume", new Vector2f(startPointx + screenHelper.calcX(400f), startPointy), new Vector2f(labelWitdh, labelHeight));
        lblFXVolume.setText("Menu Volume");
        lblFXVolume.setFontSize(labelFontSize);

        sldMenuVolume = new Slider(screen, new Vector2f(startPointx + marginLeftControls + screenHelper.calcX(400f), startPointy + screenHelper.calcY(7f)), new Vector2f(sliderWitdh, sliderHeight), Slider.Orientation.HORIZONTAL, false) {
            @Override
            public void onChange(int selectedIndex, Object value) {
            }
        };
        sldMenuVolume.setStepIntegerRange(0, 100, 1);

        Label lblSoundVolume = new Label(screen, "lblSoundVolume", new Vector2f(startPointx + screenHelper.calcX(800f), startPointy + screenHelper.calcY(0f)), new Vector2f(labelWitdh, labelHeight));
        lblSoundVolume.setText("Music Volume");
        lblSoundVolume.setFontSize(labelFontSize);

        sldMusicVolume = new Slider(screen, new Vector2f(startPointx + marginLeftControls + screenHelper.calcX(800f), startPointy + screenHelper.calcY(7f)), new Vector2f(sliderWitdh, sliderHeight), Slider.Orientation.HORIZONTAL, false) {
            @Override
            public void onChange(int selectedIndex, Object value) {
            }
        };
        sldMusicVolume.setStepIntegerRange(0, 100, 1);

        this.addChild(lblMasterVolume);
        this.addChild(sldMasterVolume);
        this.addChild(lblFXVolume);
        this.addChild(sldMenuVolume);
        this.addChild(lblSoundVolume);
        this.addChild(sldMusicVolume);
    }

    private void readControlValues() {
        String resolution = String.valueOf(settings.getWidth()) + " x " + String.valueOf(settings.getHeight());

        //General
        txtPlayerName.setText(settings.getUsername());
        chkEnableAudioVisualization.setIsChecked(settings.isAudioVisualizationEnabled());

        //Video
        setComboBoxValue(cboResolution, resolution);
        setComboBoxValue(cboFrequency, String.valueOf(settings.getFrequency()));
        chkFullscreen.setIsChecked(settings.isFullscreenEnabled());
        chkVSync.setIsChecked(settings.isVSyncEnabled());
        chkShowWeapon.setIsChecked(settings.isShowWeaponEnabled());
        sldParticleDensity.setSelectedIndex(settings.getParticleDensity());

        //Audio
        sldMasterVolume.setSelectedIndex(settings.getMasterVolume());
        sldMenuVolume.setSelectedIndex(settings.getMenuVolume());
        sldMusicVolume.setSelectedIndex(settings.getMusicVolume());
    }

    private void setComboBoxValue(ComboBox comboBox, String value) {
        for (int i = 0; i < comboBox.getListItems().size(); i++) {
            if (comboBox.getListItems().get(i).getValue().equals(value)) {
                comboBox.setSelectedIndex(i);
            }
        }
    }

    private void saveControlValues() {
        //General
        settings.setUsername(txtPlayerName.getText());
        settings.setEnableAudioVisualization(chkEnableAudioVisualization.getIsChecked());

        //Video
        String resolution[] = cboResolution.getSelectedListItem().getValue().toString().split(" x ");
        settings.setWidth(Integer.parseInt(resolution[0]));
        settings.setHeight(Integer.parseInt(resolution[1]));
        settings.setFrequency(Integer.parseInt(cboFrequency.getSelectedListItem().getValue().toString()));
        settings.setFullscreen(chkFullscreen.getIsChecked());
        settings.setVSync(chkVSync.getIsChecked());
        settings.setShowWeapon(chkShowWeapon.getIsChecked());
        settings.setParticleDensity(sldParticleDensity.getSelectedIndex());

        //Audio
        settings.setMasterVolume(sldMasterVolume.getSelectedIndex());
        settings.setMenuVolume(sldMenuVolume.getSelectedIndex());
        settings.setMusicVolume(sldMusicVolume.getSelectedIndex());

        settings.save();
    }

    public void refresh() {
    }
}
