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

import com.imyeyu.fx.TimiFX;
import com.imyeyu.fx.task.RunAsyncScheduled;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.examples.component.AbstractDemoPane;
import com.imyeyu.fx.ui.examples.component.sidebar.SidebarItem;
import com.imyeyu.fx.utils.grid.Column;
import com.imyeyu.fx.utils.scroll.SmoothScroll;
import com.imyeyu.inject.annotation.Component;
import com.imyeyu.utils.Calc;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

/**
 * 平滑滚动
 *
 * @author 夜雨
 * @since 2022-09-01 14:29
 */
@Component
public class SmoothScrollDemo extends AbstractDemoPane {

	public SmoothScrollDemo() {
		title.setText(SidebarItem.SMOOTH_SCROLL.getText());
		document.sync("https://doc.imyeyu.net/timi-fx/net/imyeyu/timifx/util/SmoothScroll.html");
		source.sync("https://git.imyeyu.net/Timi/timi-fx/src/master/src/main/java/net/imyeyu/timifx/util/SmoothScroll.java");
		tips.setText(TimiFXUI.MULTILINGUAL.text("fx.example.smooth_scroll.tips"));

		// 下拉框
		ComboBox<String> comboBoxDef = new ComboBox<>();
		ComboBox<String> comboBox = new ComboBox<>();
		SmoothScroll.virtual.install(comboBox);

		// 滚动面板
		VBox spDefVB = new VBox();
		VBox spVB = new VBox();
		ScrollPane scrollPaneDef = new ScrollPane(spDefVB);
		ScrollPane scrollPane = new ScrollPane(spVB);
		scrollPaneDef.setBorder(Stroke.LT);
		scrollPane.setBorder(Stroke.LT);
		SmoothScroll.pane.install(scrollPane);

		// 文本域
		TextArea textAreaDef = new TextArea();
		TextArea textArea = new TextArea();
		textAreaDef.setBorder(Stroke.LT);
		textArea.setBorder(Stroke.LT);
		SmoothScroll.text.install(textArea);

		// 列表
		ListView<String> listViewDef = new ListView<>();
		ListView<String> listView = new ListView<>();
		listViewDef.setBorder(Stroke.LT);
		listView.setBorder(Stroke.LT);
		SmoothScroll.virtual.install(listView);

		// 表格
		TableView<String> tableViewDef = new TableView<>();
		TableView<String> tableView = new TableView<>();
		tableViewDef.setBorder(Stroke.LT);
		tableView.setBorder(Stroke.LT);
		TableColumn<String, Object> col = new TableColumn<>("col");
		col.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue()));
		tableViewDef.getColumns().add(col);
		tableView.getColumns().add(col);
		SmoothScroll.virtual.install(tableView);

		setCenter(new GridPane() {{
			getColumnConstraints().addAll(
					Column.builder().right().percentWidth(10).build(),
					Column.builder().center().percentWidth(18).fill().build(),
					Column.builder().center().percentWidth(18).fill().build(),
					Column.builder().center().percentWidth(18).fill().build(),
					Column.builder().center().percentWidth(18).fill().build(),
					Column.builder().center().percentWidth(18).fill().build()
			);

			add(new StackPane() {{
				setBorder(Stroke.TOP);
				setPadding(new Insets(0, 6, 0, 0));
				setAlignment(Pos.CENTER_RIGHT);
				getChildren().add(new Label(TimiFXUI.MULTILINGUAL.text("default")));
			}}, 0, 1, 1, 1);
			add(new StackPane() {{
				setBorder(Stroke.TOP);
				setPadding(new Insets(0, 6, 0, 0));
				setAlignment(Pos.CENTER_RIGHT);
				getChildren().add(new Label(TimiFXUI.MULTILINGUAL.text("fx.example.smooth_scroll")));
			}}, 0, 2, 1, 1);

			int col = 1;
			addColumn(col++, new Label("ComboBox"), new StackPane() {{
				setBorder(Stroke.LT);
				getChildren().add(comboBoxDef);
			}}, new StackPane() {{
				setBorder(Stroke.LT);
				getChildren().add(comboBox);
			}});
			addColumn(col++, new Label("ScrollPane"), scrollPaneDef, scrollPane);
			addColumn(col++, new Label("TextArea"), textAreaDef, textArea);
			addColumn(col++, new Label("ListView"), listViewDef, listView);
			addColumn(col, new Label("TableView"), tableViewDef, tableView);
		}});

		String text;
		for (int i = 0; i < 50; i++) {
			text = "item " + i;

			comboBoxDef.getItems().add(text);
			comboBox.getItems().add(text);
			spDefVB.getChildren().add(new Label(text));
			spVB.getChildren().add(new Label(text));
			textAreaDef.appendText(text + "\r\n");
			textArea.appendText(text + "\r\n");
			listViewDef.getItems().add(text);
			listView.getItems().add(text);
			tableViewDef.getItems().add(text);
			tableView.getItems().add(text);
		}

		comboBoxDef.getSelectionModel().select(0);
		comboBox.getSelectionModel().select(0);
	}
}
