package zero1hd.rhythmbullet.ui.pages;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ProgressBar;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;

import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.audio.SongController;
import zero1hd.rhythmbullet.screens.CreativeScreen;
import zero1hd.rhythmbullet.screens.MainMenu;

public class OptionsPage extends Page {
	Table optionsTable = new Table();
	private ProgressBar musicVolSlider;
	private ProgressBar fxVolSlider;
	private TextField directoryField;
	
	public OptionsPage(RhythmBullet core, Vector3 targetPosition, MoreOptionsPage moreOptionsPage, SongController sc) {
		optionsTable.defaults().spaceLeft(40f).padTop(5f).padBottom(5f).left();
		
		Label optionGeneralTitle = new Label("General", core.getDefaultSkin(), "large-font", core.getDefaultSkin().getColor("default"));
		optionsTable.add(optionGeneralTitle).left().spaceBottom(10f);
		
		optionsTable.row();
		
		Label musicVolSliderLabel = new Label("Music Volume: ", core.getDefaultSkin());
		optionsTable.add(musicVolSliderLabel).padRight(12f).left();
		musicVolSlider = new Slider(0, 100, 0.1f, false, core.getDefaultSkin());
		musicVolSlider.setValue(core.getPrefs().getFloat("music vol", 100f)*100f);
		optionsTable.add(musicVolSlider).prefWidth(790).left();
		final Label musicVolPercentage = new Label(MathUtils.round(musicVolSlider.getValue()) + "%", core.getDefaultSkin());
		musicVolSlider.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				musicVolPercentage.setText(MathUtils.round(musicVolSlider.getValue()) + "%");
				sc.getCurrentSong().getPlaybackMusic().setVolume(musicVolSlider.getPercent());
				
				core.getPrefs().putFloat("music vol", musicVolSlider.getPercent());
			}
		});
		optionsTable.add(musicVolPercentage).expandX();
		
		optionsTable.row();
		
		Label fxVolSliderLabel = new Label("FX Volume: ", core.getDefaultSkin());
		optionsTable.add(fxVolSliderLabel).padRight(12f).left();
		fxVolSlider = new Slider(0, 100, 1, false, core.getDefaultSkin());
		fxVolSlider.setValue(core.getPrefs().getFloat("fx vol", 100f)*100f);
		optionsTable.add(fxVolSlider).prefWidth(790);
		final Label fxVolPercentage = new Label(MathUtils.round(fxVolSlider.getValue()) + "%", core.getDefaultSkin());
		fxVolSlider.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				fxVolPercentage.setText(MathUtils.round(fxVolSlider.getValue()) + "%");
				core.getPrefs().putFloat("fx vol", fxVolSlider.getPercent());
			}
		});
		
		optionsTable.add(fxVolPercentage);
		
		optionsTable.row();
		
		Label musicDirectoryLabel = new Label("Music Directory: ", core.getDefaultSkin());
		optionsTable.add(musicDirectoryLabel).left();
		directoryField = new TextField(null, core.getDefaultSkin());
		directoryField.setText(core.getPrefs().getString("music dir", System.getProperty("user.home")+System.getProperty("file.separator")+"Music"));
		optionsTable.add(directoryField).prefWidth(810).left();
		
		optionsTable.row();
		
		Label debugCodeLabel = new Label("Debug Code: ", core.getDefaultSkin());
		optionsTable.add(debugCodeLabel).left();
		final TextField debugCodeField = new TextField(null, core.getDefaultSkin());
		optionsTable.add(debugCodeField).prefWidth(810).left();
		
		optionsTable.top();
		
		
		//Back button
		TextButton backButton = new TextButton("Back", core.getDefaultSkin());
		backButton.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				targetPosition.x = 0.5f*Gdx.graphics.getWidth();
			}
		});
		backButton.setPosition(10, getHeight()-backButton.getHeight()-18);
		addActor(backButton);
		
		optionsTable.setPosition(backButton.getX()+ 20 + backButton.getWidth(), 0);
		optionsTable.setSize(getWidth()-backButton.getWidth()-10-20-10, getHeight());
		addActor(optionsTable);
		
		optionsTable.row();
		
		TextButton keybindSettings = new TextButton("Set Controls",  core.getDefaultSkin());
		keybindSettings.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				moreOptionsPage.setControlType((byte) 0);
				targetPosition.y = -0.5f*Gdx.graphics.getHeight();
			}
		});
		optionsTable.add(keybindSettings).colspan(2).fill();
		
		optionsTable.row();
		
		TextButton graphicsSettings = new TextButton("Graphics",  core.getDefaultSkin());
		graphicsSettings.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				moreOptionsPage.setControlType((byte) 1);
				targetPosition.y = -0.5f*Gdx.graphics.getHeight();					
			}
		});
		optionsTable.add(graphicsSettings).colspan(2).fill();
		addListener(new InputListener() {
			@Override
			public boolean keyUp(InputEvent event, int keycode) {
				if (keycode == Keys.ENTER) {
					Gdx.app.debug("Debug Field", debugCodeField.getText());
					if (debugCodeField.getText().equals("creative")) {
						Gdx.app.debug("Debug Field", "going to creative test room...");
						core.setScreen(new CreativeScreen(core, (MainMenu) core.getScreen(), sc.getSongList()));
					}
				}
				return super.keyUp(event, keycode);
			}
		});
	}
	
	public void saveOptions(Preferences prefs) {
		Gdx.app.debug("Preferences", "Saved all basic options page values.");
		prefs.putString("music dir", directoryField.getText());
	}
}
