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

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;

public class ResetErrorListener extends ChangeListener {
    private final Skin skin;
    private final Actor target;
    private final String style;

    public ResetErrorListener(Skin skin) {
        this(skin, null);
    }

    public ResetErrorListener(Skin skin, Actor target) {
        this(skin, target, "default");
    }

    public ResetErrorListener(Skin skin, Actor target, String style) {
        this.skin = skin;
        this.target = target;
        this.style = style;
    }

    @Override
    public void changed(ChangeEvent event, Actor actor) {
        var target = this.target != null ? this.target : event.getTarget();
        if (target instanceof TextField textField) {
            textField.setStyle(skin.get(style, TextField.TextFieldStyle.class));
        } else if (target instanceof SelectBox<?> box) {
            box.setStyle(skin.get(style, SelectBox.SelectBoxStyle.class));
        } else if (target instanceof List<?> list) {
            list.setStyle(skin.get(style, List.ListStyle.class));
        } else if (target instanceof ScrollPane scrollPane) {
            scrollPane.setStyle(skin.get(style, ScrollPane.ScrollPaneStyle.class));
        }
    }
}
