package cn.forevermc.launcher.view.components.control;

import cn.forevermc.launcher.util.Sound;
import javafx.beans.binding.Bindings;
import javafx.beans.property.StringProperty;
import javafx.event.Event;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.Skin;
import javafx.scene.control.TextField;
import javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.Border;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import com.imyeyu.fx.ui.MinecraftFont;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.utils.BgFill;
import com.imyeyu.fx.utils.BorderStroke;
import com.imyeyu.java.ref.Ref;

/**
 * MC 输入框
 *
 * @author 夜雨
 * @since 2022-11-23 00:27
 */
public class MCTextField extends TextField implements TimiFXUI {

	private static final Border BORDER_FOCUSED = new BorderStroke(Colorful.WHITE).width(2).build();
	private static final Border BORDER_DEFAULT = new BorderStroke("#A0A0A0").width(2).build();
	private static final Background BG = new BgFill("#000A").build();
	private static final Background BG_DISABLED = new BgFill("#444C").build();

	protected final Label label;

	public MCTextField() {
		this("");
	}

	public MCTextField(String label) {
		this.label = new Label(label);
		this.label.setTextFill(Colorful.LIGHT_GRAY);
		this.label.setMouseTransparent(true);
		this.label.translateXProperty().bind(this.label.widthProperty().negate());

		getStyleClass().setAll("mc-text-field");
		setFont(MinecraftFont.X16());

		borderProperty().bind(Bindings.when(focusedProperty()).then(BORDER_FOCUSED).otherwise(BORDER_DEFAULT));
		paddingProperty().bind(Bindings.createObjectBinding(() -> new Insets(8, 0, 8, this.label.getWidth() + 8), this.label.widthProperty()));
		backgroundProperty().bind(Bindings.when(disabledProperty()).then(BG_DISABLED).otherwise(BG));
		addEventFilter(MouseEvent.MOUSE_PRESSED, e -> Sound.CLICK.play());
		addEventFilter(ContextMenuEvent.CONTEXT_MENU_REQUESTED, Event::consume);
	}

	@Override
	protected Skin<?> createDefaultSkin() {
		Skin<?> skin = super.createDefaultSkin();
		if (skin instanceof TextFieldSkin textFieldSkin) {
			try {
				Pane textGroup = Ref.getFieldValue(textFieldSkin, "textGroup", Pane.class);

				StackPane root = new StackPane();
				StackPane.setAlignment(label, Pos.CENTER_LEFT);
				StackPane.setMargin(textGroup, new Insets(0, 0, 0, 4));
				root.getChildren().addAll(label, textGroup);

				textFieldSkin.getChildren().setAll(root);
			} catch (IllegalAccessException e) {
				throw new RuntimeException(e);
			}
		}
		return skin;
	}

	/** @return 标签 */
	public String getLabel() {
		return label.getText();
	}

	/** @param label 标签 */
	public void setLabel(String label) {
		this.label.setText(label);
	}

	/** @return 标签监听 */
	public StringProperty labelProperty() {
		return label.textProperty();
	}
}
