package eu.jonahbauer.wizard.client.libgdx.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import eu.jonahbauer.wizard.client.libgdx.WizardGame;

public abstract class MenuScreen implements Screen {
    protected WizardGame game;

    private Sound buttonClickSound;

    private Viewport viewport;

    private Texture title;
    private Texture background;
    private Texture[] corners; // counterclockwise starting top left

    protected Skin skin;
    protected Stage stage;

    public MenuScreen(WizardGame game) {
        this.game = game;
    }

    @Override
    public void show() {
        this.buttonClickSound = Gdx.audio.newSound(Gdx.files.internal("button_click_s.mp3"));

        this.title = new Texture(Gdx.files.internal("title.png"));
        this.background = new Texture(Gdx.files.internal("background.png"));

        this.corners = new Texture[4];
        this.corners[0] = new Texture(Gdx.files.internal("ecke_lo.png"));
        this.corners[1] = new Texture(Gdx.files.internal("ecke_lu.png"));
        this.corners[2] = new Texture(Gdx.files.internal("ecke_ru.png"));
        this.corners[3] = new Texture(Gdx.files.internal("ecke_ro.png"));

        this.viewport = new FitViewport(WizardGame.WIDTH, WizardGame.HEIGHT);

        this.skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
        this.stage = new Stage(viewport);
    }

    @Override
    public final void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        viewport.getCamera().update();
        game.batch.setProjectionMatrix(viewport.getCamera().combined);
        game.batch.begin();
        game.batch.draw(background, 0, 0);
        game.batch.draw(corners[0], 0, WizardGame.HEIGHT - corners[0].getHeight());
        game.batch.draw(corners[1], 0, 0);
        game.batch.draw(corners[2], WizardGame.WIDTH - corners[2].getWidth(), 0);
        game.batch.draw(corners[3], WizardGame.WIDTH - corners[3].getWidth(), WizardGame.HEIGHT - corners[3].getHeight());
        game.batch.draw(title, 555, WizardGame.HEIGHT - 192 - 96, 810, 192);
        renderInternal(delta);
        game.batch.end();

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

    protected abstract void renderInternal(float delta);

    @Override
    public final void resize(int width, int height) {
        viewport.update(width, height);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }

    protected void sfxClick() {
        buttonClickSound.play(0.6f);
    }
}
