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

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.WizardGame;
import eu.jonahbauer.wizard.client.libgdx.listeners.KeyboardFocusManager;
import eu.jonahbauer.wizard.client.libgdx.listeners.ResetErrorListener;
import eu.jonahbauer.wizard.client.libgdx.state.Menu;

import java.net.URI;
import java.net.URISyntaxException;

public class ConnectScreen extends MenuScreen {
    private TextButton buttonBack;
    private TextButton buttonConnect;

    private TextField uriField;

    public ConnectScreen(WizardGame game) {
        super(game);
    }
    
    private final ChangeListener listener = new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            if (actor == buttonBack) {
                game.getClient().execute(Menu.class, Menu::showMenuScreen);
            } else if (actor == buttonConnect) {
                try {
                    var uriString = ConnectScreen.this.uriField.getText();
                    var uri = new URI(uriString);
                    game.storage.uri = uriString;
                    game.getClient().execute(Menu.class, (s, c) -> s.connect(c, uri));
                } catch (URISyntaxException e) {
                    uriField.setStyle(getTextFieldErrorStyle());
                }
            }
        }
    };

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

        buttonBack = new TextButton(messages.get("menu.connect.back"), skin);
        buttonBack.addListener(listener);
        getButtonGroup().addActor(buttonBack);

        buttonConnect = new TextButton(messages.get("menu.connect.connect"), skin);
        buttonConnect.addListener(listener);
        getButtonGroup().addActor(buttonConnect);

        var label = new Label(messages.get("menu.connect.address.label"), 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
        uriField = new TextField(game.storage.uri, skin);
        uriField.setMessageText(messages.get("menu.connect.uri.hint"));
        uriField.setSize(0.4f * WizardGame.WIDTH, 64);
        uriField.setPosition(0.5f * (WizardGame.WIDTH - uriField.getWidth()), 0.45f * (WizardGame.HEIGHT - uriField.getHeight()));
        uriField.addListener(new ResetErrorListener(skin));

        stage.addActor(uriField);
        stage.addActor(label);
        stage.addCaptureListener(new KeyboardFocusManager(uriField, buttonBack, buttonConnect));

        buttonBack.setName("button_back");
        buttonConnect.setName("button_connect");
        uriField.setName("uri");
    }
}
