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

import com.imyeyu.fx.bean.Interpolates;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.examples.bean.Config;
import com.imyeyu.fx.ui.examples.component.AbstractDemoPane;
import com.imyeyu.fx.ui.examples.component.animation.InterpolatorPane;
import com.imyeyu.fx.ui.examples.component.sidebar.SidebarItem;
import com.imyeyu.fx.utils.scroll.SmoothScroll;
import com.imyeyu.inject.annotation.Component;
import com.imyeyu.inject.annotation.PostConstruct;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
import javafx.util.StringConverter;

/**
 * 动画插值器
 *
 * @author 夜雨
 * @since 2022-09-01 14:29
 */
@Component
public class InterpolatorDemo extends AbstractDemoPane {

	private final Config config;
	private final Slider duration;

	public InterpolatorDemo(Config config) {
		this.config = config;
		title.setText(SidebarItem.INTERPOLATOR.getText());
		document.sync("https://doc.imyeyu.net/timi-fx/net/imyeyu/timifx/bean/Interpolators.html");
		source.sync("https://git.imyeyu.net/Timi/timi-fx/src/master/src/main/java/net/imyeyu/timifx/bean/Interpolators.java");
		tips.setText(TimiFXUI.MULTILINGUAL.text("fx.example.interpolator.tips"));

		// 播放
		Button play = new Button(TimiFXUI.MULTILINGUAL.text("play"));

		// 重置
		Button reset = new Button(TimiFXUI.MULTILINGUAL.text("reset"));
		reset.setBorder(Stroke.EX_LEFT);

		// 持续时间
		duration = new Slider();
		duration.setMin(0);
		duration.setMax(3000);
		duration.setPrefWidth(460);
		duration.setSnapToTicks(true);
		duration.setMajorTickUnit(400);
		duration.setShowTickMarks(true);
		duration.setMinorTickCount(7);
		duration.setShowTickLabels(true);
		duration.setLabelFormatter(new StringConverter<>() {

			@Override
			public String toString(Double ms) {
				return "%.0f".formatted(ms);
			}

			@Override
			public Double fromString(String string) {
				return null;
			}
		});
		Label value = TimiFXUI.label();
		value.textProperty().bind(duration.valueProperty().asString("%.0f ms"));

		// 插值器列表
		Interpolates[] interpolators = Interpolates.values();
		InterpolatorPane[] panes = new InterpolatorPane[interpolators.length];
		for (int i = 0; i < interpolators.length; i++) {
			panes[i] = new InterpolatorPane(interpolators[i]);
			panes[i].getTransition().durationProperty().bind(Bindings.createObjectBinding(() -> Duration.millis(duration.getValue()), duration.valueProperty()));
		}

		setCenter(new BorderPane() {{
			setTop(new HBox() {{
				setSpacing(32);
				setPadding(new Insets(20, 20, 10, 20));
				getChildren().addAll(new HBox(play, reset), new HBox() {{
					setPadding(new Insets(5, 0, 0, 0));
					setSpacing(16);
					getChildren().addAll(TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("duration")), duration, value);
				}});
			}});
			setCenter(new ScrollPane() {{
				getStyleClass().add(CSS.SP_BORDER);
				setBorder(Stroke.TOP);
				setFitToWidth(true);
				setContent(new VBox() {{
					setSpacing(32);
					setFillWidth(false);
					setAlignment(Pos.TOP_CENTER);
					setPadding(new Insets(12, 0, 12, 0));
					getChildren().addAll(panes);
				}});
				SmoothScroll.pane.install(this);
			}});
		}});

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

		// 播放
		play.setOnAction(e -> {
			for (int i = 0; i < panes.length; i++) {
				panes[i].getTransition().play();
			}
		});

		// 重置
		reset.setOnAction(e -> {
			for (int i = 0; i < panes.length; i++) {
				panes[i].reset();
			}
		});
	}

	@PostConstruct
	private void init() {
		duration.valueProperty().bindBidirectional(config.getInterpolator().getDuration());
	}
}
