package com.imyeyu.fx.ui.examples.component.animation;

import com.imyeyu.fx.bean.Interpolates;
import com.imyeyu.fx.ui.MinecraftFont;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.components.SelectableLabel;
import com.imyeyu.fx.utils.bg.BackgroundFillBuilder;
import com.sun.scenario.animation.SplineInterpolator;
import javafx.animation.TranslateTransition;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.util.Duration;
import lombok.Getter;

/**
 * 动画插值器示例组件
 *
 * @author 夜雨
 * @since 2022-09-01 14:44
 */
public class InterpolatorPane extends BorderPane implements TimiFXUI {

	@Getter
	private final TranslateTransition transition;

	private final Region block;

	public InterpolatorPane(Interpolates interpolator) {
		// 二次曲线
		Canvas canvas = new Canvas();
		canvas.setWidth(64);
		canvas.setHeight(64);

		// 插值
		SelectableLabel label = new SelectableLabel(TimiFXUI.MULTILINGUAL.text("fx.example.interpolator.demo"));

		// 方块
		block = new Region();
		block.setBackground(BackgroundFillBuilder.test());

		// 轨道
		HBox track = new HBox(block);
		track.setBorder(Stroke.TOP);
		track.setCursor(Cursor.HAND);
		track.setBackground(BG.WHITE);

		block.maxWidthProperty().bind(block.heightProperty());
		block.prefWidthProperty().bind(block.heightProperty());

		setMargin(label, new Insets(2, 4, 2, 4));
		setBorder(Stroke.DEFAULT);
		setPrefWidth(520);
		setBackground(BG.TITLE);
		setLeft(canvas);
		setCenter(new BorderPane() {{
			setBorder(Stroke.LEFT);
			setTop(label);
			setCenter(track);
		}});

		transition = new TranslateTransition();
		transition.setNode(block);
		transition.setFromX(0);
		transition.toXProperty().bind(track.widthProperty().subtract(block.widthProperty()));
		transition.setDuration(Duration.seconds(2));
		transition.setInterpolator(interpolator.getValue());

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

		// 绘制图示
		{
			double[][] list = interpolator.buildBezierPoint(canvas.getWidth(), (long) (canvas.getWidth() * .5));
			GraphicsContext g = canvas.getGraphicsContext2D();
			g.setFill(Colorful.WHITE);
			g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
			g.setLineWidth(2);

			// 文本
			g.setStroke(Colorful.LIGHT_GRAY);
			g.setFont(MinecraftFont.X16());
			g.strokeText("P", 4, 14);
			g.strokeText("T", canvas.getWidth() - 12, canvas.getHeight() - 6);

			// 坐标轴
			g.strokeLine(0, canvas.getHeight() * .5, canvas.getWidth(), canvas.getHeight() * .5);
			g.strokeLine(canvas.getWidth() * .5, 0, canvas.getWidth() * .5, canvas.getHeight());

			// 曲线
			g.setStroke(Colorful.DARK_GRAY);
			g.moveTo(0, canvas.getHeight());
			for (int i = 0; i < list.length; i++) {
				g.lineTo(list[i][0], canvas.getHeight() - list[i][1]);
			}
			g.stroke();
		}

		// 点击重播
		track.setOnMouseClicked(e -> transition.play());
	}

	/** 重置位置 */
	public void reset() {
		block.setTranslateX(0);
	}
}
