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

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.Align;
import eu.jonahbauer.wizard.client.libgdx.ClientSocket;
import eu.jonahbauer.wizard.client.libgdx.WizardGame;
import eu.jonahbauer.wizard.client.libgdx.state.AwaitingConnection;
import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
import lombok.SneakyThrows;

import java.net.URI;

public class ConnectScreen extends MenuScreen {

    private TextButton buttonBack;
    private TextButton buttonConnect;

    private TextField uri;

    public ConnectScreen(WizardGame game) {
        super(game);
    }
    
    private final ChangeListener listener = new ChangeListener() {
        @SneakyThrows
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            if (actor == buttonBack) {
                game.setScreen(new MainMenuScreen(game));
                sfxClick();
            } else if (actor == buttonConnect) {
                // TODO error handling, uri syntax, etc.
                ClientSocket socket = new ClientSocket(new URI(uri.getText()));
                game.getClient().setSocket(socket);
                game.getClient().transition(new AwaitingConnection());
                socket.connect();
                sfxClick();
            }
        }
    };

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

        buttonBack = new TextButton(game.messages.get("menu.connect.back"), game.data.skin);
        buttonBack.setPosition(WizardGame.WIDTH * 0.275f, BUTTON_BAR_Y);
        buttonConnect = new TextButton(game.messages.get("menu.connect.connect"), game.data.skin);
        buttonConnect.setPosition(WizardGame.WIDTH * 0.725f - buttonConnect.getWidth(), BUTTON_BAR_Y);

        var label = new Label(game.messages.get("menu.connect.address.label"), game.data.skin);
        label.setSize(0.4f * WizardGame.WIDTH, 64);
        label.setAlignment(Align.center);
        label.setPosition(0.5f * (WizardGame.WIDTH - label.getWidth()), 0.55f * (WizardGame.HEIGHT - label.getHeight()));

        // TODO sensible default value
        uri = new TextField("wss://webdev.jonahbauer.eu/wizard/", game.data.skin);
        uri.setMessageText(game.messages.get("menu.connect.uri.hint"));
        uri.setSize(0.4f * WizardGame.WIDTH, 64);
        uri.setPosition(0.5f * (WizardGame.WIDTH - uri.getWidth()), 0.45f * (WizardGame.HEIGHT - uri.getHeight()));

        Gdx.input.setInputProcessor(game.data.stage);
        game.data.stage.addActor(buttonBack);
        game.data.stage.addActor(buttonConnect);
        game.data.stage.addActor(uri);
        game.data.stage.addActor(label);

        buttonBack.addListener(listener);
        buttonConnect.addListener(listener);
    }
}
