package com.imyeyu.fx.ui.examples.view.pages;

import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.components.TextAreaEditor;
import com.imyeyu.fx.ui.components.TitleLabel;
import com.imyeyu.fx.ui.components.Hyperlink;
import com.imyeyu.fx.ui.examples.bean.Config;
import com.imyeyu.fx.ui.examples.component.AbstractDemoPane;
import com.imyeyu.fx.ui.examples.component.sidebar.SidebarItem;
import com.imyeyu.inject.annotation.Component;
import com.imyeyu.inject.annotation.PostConstruct;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

/**
 * 配置绑定示例
 *
 * @author 夜雨
 * @since 2022-09-01 14:31
 */
@Component
public class BindingsConfigDemo extends AbstractDemoPane {

	private final Config config;
	private final Label stageSize, interpolatorDuration;

	public BindingsConfigDemo(Config config) {
		this.config = config;
		title.setText(SidebarItem.BINDING_CONFIG.getText());
		document.sync("https://doc.imyeyu.net/timi-fx/net/imyeyu/timifx/config/BindingsConfig.html");
		source.sync("https://git.imyeyu.net/Timi/timi-fx/src/master/src/main/java/net/imyeyu/timifx/config/BindingsConfig.java");

		// timi-java 说明
		Hyperlink timijavaDescription = new Hyperlink("https://www.imyeyu.net/article/public/aid117.html#配置系统");
		Hyperlink timijavaDocument = new Hyperlink("https://doc.imyeyu.net/timi-java/net/imyeyu/timijava/config/package-summary.html");
		Hyperlink timijavaSource = new Hyperlink("https://git.imyeyu.net/Timi/timi-java/src/master/src/main/java/net/imyeyu/timijava/config");

		// 窗体尺寸
		stageSize = new Label();

		// 动画插值器持续时间
		interpolatorDuration = new Label();

		// 代码
		TextAreaEditor code = new TextAreaEditor();
		code.setEditable(false);
		code.setText("""
// config 为 timi-java 的配置对象

// 窗体尺寸，单向绑定
BindingsConfig.cfg(config).bindDoubleProperty(stage.widthProperty(), Config.section("Main").key("Width"));
BindingsConfig.cfg(config).bindDoubleProperty(stage.heightProperty(), Config.section("Main").key("Height"));

// 对组件双向绑定，duration 为 Slider
BindingsConfig.cfg(config).bindDoubleProperty(duration, Config.section("Interpolator").key("Duration"));
				""".trim());

		setCenter(new VBox() {{
			setSpacing(12);
			getChildren().addAll(new GridPane() {{
				setVgap(4);
				setHgap(6);
				setPadding(new Insets(20));

				int row = 0;
				addRow(row++, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("description")), timijavaDescription);
				addRow(row++, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("document")), timijavaDocument);
				addRow(row, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("source")), timijavaSource);
			}}, new BorderPane() {{
				setPadding(new Insets(20));
				setTop(new TitleLabel(TimiFXUI.MULTILINGUAL.text("example")));
				setCenter(new GridPane() {{
					setVgap(4);
					setHgap(6);
					setPadding(new Insets(20));

					addRow(0, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("fx.example.binding_config.stage_size")), stageSize);
					addRow(1, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("fx.example.binding_config.interpolator_duration")), interpolatorDuration);
				}});
				setBottom(new BorderPane() {{
					setMargin(code, new Insets(12, 0, 0, 0));
					setCenter(code);
				}});
			}});
		}});
	}

	@PostConstruct
	private void init() {
		stageSize.textProperty().bind(Bindings.createStringBinding(() -> "[%s, %s]".formatted(config.getWidth().get(), config.getHeight().get()), config.getWidth(), config.getHeight()));
		interpolatorDuration.textProperty().bind(config.getInterpolator().getDuration().asString());
	}
}
