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

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.inject.annotation.Component;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.GridPane;

/**
 * 可编辑表格单元格
 *
 * @author 夜雨
 * @since 2022-08-29 16:56
 */
@Component
public class EditableTableCellDemo extends AbstractDemoPane {

	public EditableTableCellDemo() {
		title.setText(SidebarItem.EDITABLE_TABLE_CELL.getText());
		document.sync("https://doc.imyeyu.net/timi-fx/net/imyeyu/timifx/component/EditableTableCell.html");
		source.sync("https://git.imyeyu.net/Timi/timi-fx/src/master/src/main/java/net/imyeyu/timifx/component/EditableTableCell.java");
		tips.setText(TimiFXUI.MULTILINGUAL.text("fx.example.editable_table_cell.tips"));

		// 默认
		Label labelDef = TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("default"));
		labelDef.setAlignment(Pos.CENTER);
		TableColumn<Item, String> defCol = new TableColumn<>("col");
		defCol.setCellFactory(TextFieldTableCell.forTableColumn());
		defCol.setCellValueFactory(new PropertyValueFactory<>("value"));
		TableView<Item> def = new TableView<>();
		def.setEditable(true);
		def.getColumns().add(TimiFXUI.emptyTableColumn(40, Item.class, Object.class));
		def.getColumns().add(defCol);
		defCol.prefWidthProperty().bind(def.widthProperty().subtract(50));

		// TFX
		Label labelTFX = TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("fx.example.better"));
		labelTFX.setAlignment(Pos.CENTER);
		TableColumn<Item, String> tfxCol = new TableColumn<>("col");
		tfxCol.setCellValueFactory(new PropertyValueFactory<>("value"));
		tfxCol.setCellFactory(cell -> new com.imyeyu.fx.ui.components.table.TextFieldTableCell<>() {

			@Override
			protected StringProperty property(Item item) {
				return item.value;
			}
		});
		TableView<Item> tfx = new TableView<>();
		tfx.setEditable(true);
		tfx.getStyleClass().add("editable-table");
		tfx.getColumns().add(TimiFXUI.emptyTableColumn(40, Item.class, Object.class));
		tfx.getColumns().add(tfxCol);
		tfxCol.prefWidthProperty().bind(tfx.widthProperty().subtract(50));

		setCenter(new GridPane() {{
			getColumnConstraints().addAll(Column.valueFill(), Column.valueFill());
			setVgap(6);
			setHgap(20);
			setPadding(new Insets(32));

			addRow(0, labelDef, labelTFX);
			addRow(1, def, tfx);
		}});

		for (int i = 0; i < 32; i++) {
			def.getItems().add(new Item(String.valueOf(i)));
			tfx.getItems().add(new Item(String.valueOf(i)));
		}
	}

	/**
	 * 数据对象
	 *
	 * @author 夜雨
	 * @since 2022-08-29 21:10
	 */
	public static class Item {

		/** 可监听值 */
		StringProperty value;

		public Item(String value) {
			this.value = new SimpleStringProperty(value);
		}

		/** @param value 设置当前值 */
		public void setValue(String value) {
			this.value.set(value);
		}

		/** @return 当前值 */
		public String getValue() {
			return value.get();
		}

		/** @return 值监听 */
		public StringProperty valueProperty() {
			return value;
		}
	}
}
