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

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import eu.jonahbauer.wizard.client.libgdx.WizardGame;

public abstract class MenuScreen implements Screen {
    protected final float BUTTON_BAR_Y = WizardGame.HEIGHT * 0.15f;

    protected WizardGame game;

    // shared game.data between all menu screens
    protected boolean dispose;

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

    @Override
    public void show() {
        if (game.data == null) {
            game.data = new WizardGame.Data();
        } else {
            game.data.reset();
        }
    }

    @Override
    public final void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT | (Gdx.graphics.getBufferFormat().coverageSampling?GL20.GL_COVERAGE_BUFFER_BIT_NV:0));

        game.data.extendViewport.apply(true);
        game.batch.setProjectionMatrix(game.data.extendViewport.getCamera().combined);
        game.batch.begin();
        renderBackground(delta);
        game.batch.end();

        game.data.fitViewport.apply();
        game.batch.setProjectionMatrix(game.data.fitViewport.getCamera().combined);
        game.batch.begin();
        renderForeground(delta);
        game.batch.end();

        game.data.stage.act(delta);
        game.data.stage.draw();
    }

    protected void renderBackground(float delta) {
        game.batch.setColor(1, 1, 1, 1);
        float scale = Math.max(
                game.data.extendViewport.getWorldWidth() / WizardGame.WIDTH,
                game.data.extendViewport.getWorldHeight() / WizardGame.HEIGHT
        );
        game.batch.draw(game.data.background, 0, 0, scale * WizardGame.WIDTH, scale * WizardGame.HEIGHT);
        game.batch.draw(game.data.corners[0], 0, game.data.extendViewport.getWorldHeight() - game.data.corners[0].getRegionHeight());
        game.batch.draw(game.data.corners[1], 0, 0);
        game.batch.draw(game.data.corners[2], game.data.extendViewport.getWorldWidth() - game.data.corners[2].getRegionWidth(), 0);
        game.batch.draw(game.data.corners[3], game.data.extendViewport.getWorldWidth() - game.data.corners[3].getRegionWidth(), game.data.extendViewport.getWorldHeight() - game.data.corners[3].getRegionHeight());
    }

    protected void renderForeground(float delta) {
        game.batch.setColor(1, 1, 1, 1);
        game.batch.draw(game.data.title, 555, WizardGame.HEIGHT - 192 - 96, 810, 192);
    }

    @Override
    public final void resize(int width, int height) {
        game.data.extendViewport.update(width, height);
        game.data.fitViewport.update(width, height);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        if (dispose) {
            game.data.dispose();
        }
    }

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