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

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

public class ServerScreen extends MenuScreen {

    TextButton backButton;
    TextButton continueButton;

    TextField ip;
    TextField port;
    TextField playerName;

    Group inputs;

    ClickListener listener = new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            var target = event.getListenerActor();
            if (target == backButton) {
                game.setScreen(new MainMenuScreen(game));
                sfxClick();
            } else if (target == continueButton) {
                game.setScreen(new SessionListScreen(game));
                sfxClick();
            }
        }
    };

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

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

        backButton = new TextButton("Zurück", skin);
        backButton.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.18f);
        backButton.setSize(120, backButton.getHeight());
        continueButton = new TextButton("Fortfahren", skin);
        continueButton.setPosition(WizardGame.WIDTH * 0.7f - continueButton.getWidth(), WizardGame.HEIGHT * 0.18f);

        inputs = new Group();
        inputs.setSize(300, continueButton.getHeight());
        inputs.setPosition(WizardGame.WIDTH / 2f - inputs.getWidth() / 2, WizardGame.HEIGHT * 0.5f);

        ip = new TextField("", skin);
        //ip.setPosition(WizardGame.WIDTH*0.4f, WizardGame.HEIGHT*0.5f);
        ip.setSize(200, continueButton.getHeight());
        port = new TextField("", skin);
        port.setPosition(ip.getX() + ip.getWidth() + WizardGame.WIDTH * 0.005f, ip.getY());
        port.setSize(80, continueButton.getHeight());
        playerName = new TextField("", skin);
        playerName.setSize(250, continueButton.getHeight());
        playerName.setPosition(WizardGame.WIDTH / 2f - playerName.getWidth() / 2, inputs.getY() - WizardGame.HEIGHT * 0.12f);

        inputs.addActor(ip);
        inputs.addActor(port);

        Gdx.input.setInputProcessor(stage);
        stage.addActor(backButton);
        stage.addActor(continueButton);
        stage.addActor(playerName);
        stage.addActor(inputs);
        //stage.addActor(port);

        backButton.addListener(listener);
        continueButton.addListener(listener);
    }

    @Override
    protected void renderInternal(float delta) {}
}
