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

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import eu.jonahbauer.wizard.client.libgdx.WizardGame;

public class MainMenuScreen extends MenuScreen {

    Texture[] symbols;

    TextButton chooseServerButton;
    TextButton exitButton;

    ClickListener listener = new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            var target = event.getListenerActor();
            if (target == chooseServerButton) {
                game.setScreen(new ServerScreen(game));
                sfxClick();
            } else if (target == exitButton) {
                sfxClick();
                Gdx.app.exit();
            }
        }
    };

    public MainMenuScreen(WizardGame game) {
        super(game);
    }

    @Override
    public void show() {
        super.show();

        symbols = new Texture[4];
        symbols[0] = new Texture(Gdx.files.internal("symbol0.png"));
        symbols[1] = new Texture(Gdx.files.internal("symbol1.png"));
        symbols[2] = new Texture(Gdx.files.internal("symbol2.png"));
        symbols[3] = new Texture(Gdx.files.internal("symbol3.png"));

        chooseServerButton = new TextButton("Spiel beitreten", skin);
        chooseServerButton.setPosition((WizardGame.WIDTH - chooseServerButton.getWidth()) / 2f, 192 + 504 - 120f - 125f);
        exitButton = new TextButton("Verlassen", skin);
        exitButton.setPosition((WizardGame.WIDTH - exitButton.getWidth()) / 2f, 192 + 120f);

        Gdx.input.setInputProcessor(stage);
        stage.addActor(chooseServerButton);
        stage.addActor(exitButton);

        chooseServerButton.addListener(listener);
        exitButton.addListener(listener);
    }

    @Override
    protected void renderInternal(float delta) {
        int width = 160, height = 224;
        int left = 384, right = 384, top = 384, bottom = 192;
        this.game.batch.draw(symbols[0], left, bottom, width, height);
        this.game.batch.draw(symbols[1], left, WizardGame.HEIGHT - top - height, width, height);
        this.game.batch.draw(symbols[2], WizardGame.WIDTH - right - width, bottom, width, height);
        this.game.batch.draw(symbols[3], WizardGame.WIDTH - right - width, WizardGame.HEIGHT - top - height, width, height);
    }
}
