package de.samdev.colorrunner.screens.menu;

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.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
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 com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.map.CRMapStorage;
import de.samdev.colorrunner.game.world.map.provider.EndlessMapProvider;
import de.samdev.colorrunner.game.world.map.provider.StaticMapProvider;

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

public class MainMenu implements Screen {

	private Preferences prefs = Gdx.app.getPreferences("settings");
	private Stage stage = new Stage();
	private Table table = new Table();

	private OrthographicCamera cam = new OrthographicCamera();
	private Texture background = new Texture("images/background2.jpg");

	private Skin skin = new Skin(Gdx.files.internal("skins/menuSkin.json"),
	        new TextureAtlas(Gdx.files.internal("skins/menuSkin.pack")));


	private TextButton buttonPlay1 = new TextButton("Play Level 1", skin);
	private TextButton buttonPlay2 = new TextButton("Play Endless", skin);
	private TextButton buttonOption = new TextButton("Option", skin);
	private TextButton buttonExit = new TextButton("Exit", skin);

    private Texture myTexture = new Texture(Gdx.files.internal("menu/Settings.png"));
	private TextureRegion myTextureRegion= new TextureRegion(myTexture);
	private TextureRegionDrawable myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
	private ImageButton optionButton = new ImageButton(myTexRegionDrawable);

	// folgendes mal als Beispiel :)
	/*
	button3.setSize(col_width*4,(float)(row_height*2));
button3.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("switch_off.png"))));
button3.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("switch_on.png"))));
button3.setPosition(col_width,Gdx.graphics.getHeight()-row_height*6);
button3.addListener(new InputListener(){
  @Override
  public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
    outputLabel.setText("Press a Button");
  }
  @Override
  public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
    outputLabel.setText("Pressed Image Button");
    return true;
  }
});



	 */

	private Label title = new Label("Color Runner", skin);

	public Music music;

	public MainMenu() {
        music = CRGame.manager.get("sound/menusound.mp3", Music.class);
        music.setLooping(true);

        if(prefs.getBoolean("sound", true))
            music.play();
        else
            music.stop();
	}

	@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) {
		// NOP
	}

	@Override
	public void show() {
		buttonPlay1.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y){
				CRGame.Inst().setScreen(new GameScreen(new StaticMapProvider(CRMapStorage.lvl_01)));
				music.stop();
			}
		});

		buttonPlay2.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y){
				//CRGame.Inst().setScreen(new GameScreen(new EndlessMapProvider(System.currentTimeMillis())));
				CRGame.Inst().setScreen(new EndlessGameMenu(music));
			}
		});
		
        buttonOption.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
            	CRGame.Inst().setScreen(new OptionMenuScreen(music));
            }
        });
		
        buttonExit.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Gdx.app.exit();
            }
        });
        
        table.add(title).padBottom(40).row();
		table.add(buttonPlay1).size((int)((double)Gdx.graphics.getWidth() / 2.0D),80).padBottom(20).row();
		table.add(buttonPlay2).size((int)((double)Gdx.graphics.getWidth() / 2.0D),80).padBottom(20).row();
        table.add(buttonOption).size((int)((double)Gdx.graphics.getWidth() / 2.0D),80).padBottom(20).row();
        table.add(buttonExit).size((int)((double)Gdx.graphics.getWidth() / 2.0D),80).padBottom(20).row();

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

        Gdx.input.setInputProcessor(stage);
	}

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

	@Override
	public void pause() {
		// NOP
	}

	@Override
	public void resume() {
		// NOP
	}

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

}
