package de.samdev.colorrunner.screens.menu;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

import de.samdev.colorrunner.screens.gameScreen.GameScreen;

public class OptionMenuScreen implements Screen{

	private Preferences prefs = Gdx.app.getPreferences("settings");
	private Stage stage = new Stage();
	private Table table = new Table();
	private Texture background = new Texture("images/background2.jpg");
	private OrthographicCamera cam = new OrthographicCamera();
	
	private Skin skin = new Skin(Gdx.files.internal("skins/menuSkin.json"),
	        new TextureAtlas(Gdx.files.internal("skins/menuSkin.pack")));
    private Music music;

	public OptionMenuScreen(Music music) {
        this.music = music;
	}

	private Label title = new Label("Option", skin);
	
	@Override
	public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		cam.setToOrtho(false, (float)Gdx.graphics.getWidth() / 2.0F, (float)Gdx.graphics.getHeight() / 2.0F);

		SpriteBatch sb = new SpriteBatch();
		sb.setProjectionMatrix(cam.combined);
		sb.begin();
		sb.draw(background, 0, 0);
		sb.end();

        stage.act();
        stage.draw();
		
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		TextButton buttonSound;
		if(prefs.getBoolean("sound", true))
			buttonSound = new TextButton("Sound On", skin);
		else
			buttonSound = new TextButton("Sound Off", skin);

		 TextButton buttonBack = new TextButton("Back", skin);
		
		buttonSound.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y){
				if(prefs.getBoolean("sound", true)) {
					prefs.putBoolean("sound", false);
                    music.stop();
				}
				else {
					prefs.putBoolean("sound", true);
                    music.play();

				}
				prefs.flush();
				((Game)Gdx.app.getApplicationListener()).setScreen(new OptionMenuScreen(music));
			}
		});
		
        buttonBack.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                ((Game)Gdx.app.getApplicationListener()).setScreen(new MainMenu());
            }
        });
        
        table.add(title).padBottom(40).row();
        table.add(buttonSound).size((int)((double)Gdx.graphics.getWidth() / 2.0D),70).padBottom(20).row();
        table.add(buttonBack).size((int)((double)Gdx.graphics.getWidth() / 2.0D), 70).padBottom(20).row();

        table.setFillParent(true);
        stage.addActor(table);

        Gdx.input.setInputProcessor(stage);
		
		
	}

	@Override
	public void hide() {
		dispose();
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		stage.dispose();
		skin.dispose();
		
	}

}
