package com.imyeyu.fx.ui.components;

import com.imyeyu.fx.icon.TimiFXIcon;
import com.imyeyu.fx.icon.TimiIcon;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.utils.Windows;
import com.imyeyu.java.bean.CallbackArg;
import com.imyeyu.java.ref.Ref;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Skin;
import javafx.scene.control.TextField;
import javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import lombok.Getter;
import lombok.Setter;

/**
 * 复杂文本域编辑器 {@link TextAreaEditor} 的文本框显示方式，需要时弹出文本域编辑器
 *
 * @author 夜雨
 * @since 2022-07-26 16:42
 */
public class TextAreaEditorField extends TextField implements TimiFXUI {

	/** 显示编辑器回调事件，触发时窗体并未显示 */
	@Getter
	@Setter
	private CallbackArg<Stage> onShowEditorEvent;

	/** 编辑器弹窗 */
	@Getter
	private final EditorStage editorStage;

	/** 标题 */
	private final StringProperty title;

	/** 默认构造 */
	public TextAreaEditorField() {
		this("");
	}

	/**
	 * 构造文本编辑器型文本框（可打开文本编辑器）
	 *
	 * @param text 文本内容
	 */
	public TextAreaEditorField(String text) {
		super(text);
		title = new SimpleStringProperty();

		editorStage = new EditorStage();
		editorStage.titleProperty().bind(title);
		editorStage.editor.textProperty().bindBidirectional(textProperty());
		editorStage.editor.editableProperty().bind(editableProperty());

		getStyleClass().add(CSS.PADDING_N);
		setPrefHeight(28);
	}

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

				// 插入编辑器图标
				IconButton editor = new IconButton(TimiFXIcon.get(TimiIcon.WRITING)).withBackground();
				editor.setBorder(Stroke.LEFT);
				editor.setCursor(Cursor.DEFAULT);

				BorderPane root = new BorderPane();
				BorderPane.setMargin(textGroup, new Insets(0, 0, 0, 4));
				root.setCenter(textGroup);
				root.setRight(editor);

				textFieldSkin.getChildren().setAll(root);

				// ---------- 事件 ----------

				editor.setOnAction(e -> {
					if (onShowEditorEvent != null) {
						onShowEditorEvent.handler(editorStage);
					}
					Windows.showCentered(getScene().getWindow(), editorStage);
				});

			} catch (IllegalAccessException e) {
				throw new RuntimeException(e);
			}
		}
		return skin;
	}

	/**
	 * 获取编辑器标题
	 *
	 * @return 编辑器标题
	 */
	public String getTitle() {
		return title.get();
	}

	/**
	 * 获取编辑器标题属性
	 *
	 * @return 编辑器标题属性
	 */
	public StringProperty titleProperty() {
		return title;
	}

	/**
	 * 设置编辑器标题
	 *
	 * @param title 编辑器标题
	 */
	public void setTitle(String title) {
		this.title.set(title);
	}

	/**
	 * 编辑器弹窗
	 *
	 * @author 夜雨
	 * @since 2022-07-26 16:54
	 */
	public static class EditorStage extends Stage {

		/** 编辑器 */
		final TextAreaEditor editor;

		EditorStage() {
			editor = new TextAreaEditor();
			editor.setBorder(Stroke.TOP);

			Scene scene = new Scene(editor);
			TimiFXUI.applyStyle(scene);
			setScene(scene);
			getIcons().add(TimiFXIcon.rerender(TimiIcon.WRITING));
			setWidth(850);
			setHeight(620);
		}
	}
}
