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.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.CRGame;
import de.samdev.colorrunner.game.world.map.provider.EndlessMapProvider;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;

/**
 * Created by benza on 16.04.2017.
 */

public class EndlessGameMenu implements Screen {

    private OrthographicCamera cam = new OrthographicCamera();
    private Texture background = new Texture("images/background2.jpg");
    private Music music;
    private Preferences endlessGameInfos = Gdx.app.getPreferences("endlessGameInfos");

    private Stage stage = new Stage();
    private Table table = new Table();

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

    private int highScore = endlessGameInfos.getInteger("Highscore", 0);
    private int lastBest = endlessGameInfos.getInteger("lastBest", 0);
    private int tries = endlessGameInfos.getInteger("Tries", 0);

    private TextButton buttonPlay = new TextButton("Play", skin);
    private TextButton buttonBack = new TextButton("Back", skin);
    private Label labelTitle = new Label("Endless Game", skin);
    private Label labelHighscore = new Label("Highscore: " + highScore, skin);
    private Label labelLastBest = new Label("Last Best: " +  lastBest, skin);
    private Label labelTries = new Label("Tries: " + tries, skin);



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

    @Override
    public void show() {
        buttonPlay.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y){
                CRGame.Inst().setScreen(new GameScreen(new EndlessMapProvider(System.currentTimeMillis())));
                music.stop();
            }
        });

        buttonBack.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y){
                CRGame.Inst().setScreen(new MainMenu());
            }
        });

        table.add(labelTitle).padBottom(40).row();
        table.add(labelHighscore).padBottom(20).row();
        table.add(labelLastBest).padBottom(20).row();
        table.add(labelTries).padBottom(20).row();
        table.add(buttonPlay).size((int)((double)Gdx.graphics.getWidth() / 2.0D),80).padBottom(20).row();
        table.add(buttonBack).size((int)((double)Gdx.graphics.getWidth() / 2.0D),80).padBottom(20).row();

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

        Gdx.input.setInputProcessor(stage);
    }

    @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) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

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