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.VerticalGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import eu.jonahbauer.wizard.client.libgdx.WizardGame;
import eu.jonahbauer.wizard.client.libgdx.listeners.KeyboardFocusManager;
import eu.jonahbauer.wizard.client.libgdx.state.BaseState;

public class ErrorScreen extends MenuScreen {
    private final String message;
    private TextButton buttonBack;
    
    private final ChangeListener listener = new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            if (actor == buttonBack) {
                game.getClient().execute(BaseState.class, BaseState::dismissErrorScreen);
            }
        }
    };
    
    public ErrorScreen(WizardGame game, String message) {
        super(game);
        this.message = message;
    }
    
    @Override
    public void show() {
        super.show();
    
        buttonBack = new TextButton(messages.get("menu.error.back"), skin);
        var label = new Label(message, skin);
        
        var content = new VerticalGroup();
        content.setPosition(WizardGame.WIDTH * 0.5f, WizardGame.HEIGHT*0.5f);
        content.addActor(label);
        content.addActor(buttonBack);
        
        buttonBack.addListener(listener);
        
        stage.addActor(content);
        stage.addCaptureListener(new KeyboardFocusManager(buttonBack));
    }
    
}
