package xyz.wbsite.dbtool.javafx.ctrl;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.Callback;
import javafx.util.converter.DefaultStringConverter;
import xyz.wbsite.dbtool.javafx.po.SelectItem;

import java.util.ArrayList;
import java.util.List;

public class OptionSelectController {

    @FXML
    private Button start;
    @FXML
    private Button cancel;
    @FXML
    private TableView datas;
    @FXML
    private Button add;
    @FXML
    private Button sub;

    private List<SelectItem> data = new ArrayList<>();

    public List<SelectItem> getData() {
        return data;
    }

    public void setData(List<SelectItem> data) {
        this.data = data;
    }

    public Button getStart() {
        return start;
    }

    public void setStart(Button start) {
        this.start = start;
    }

    public Button getCancel() {
        return cancel;
    }

    public void setCancel(Button cancel) {
        this.cancel = cancel;
    }

    public TableView getDatas() {
        return datas;
    }

    public void setDatas(TableView datas) {
        this.datas = datas;
    }

    public Button getAdd() {
        return add;
    }

    public void setAdd(Button add) {
        this.add = add;
    }

    public Button getSub() {
        return sub;
    }

    public void setSub(Button sub) {
        this.sub = sub;
    }

    public void initData() {
        datas.setEditable(true);
        datas.setSortPolicy(new Callback<TableView, Boolean>() {
            @Override
            public Boolean call(TableView param) {
                //禁止点击列头排序
                return false;
            }
        });
        ObservableList<TableColumn> columns = datas.getColumns();
        columns.get(0).setCellValueFactory(new PropertyValueFactory("value"));
        columns.get(0).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {

                param.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent event) {
                        int row = event.getTablePosition().getRow();
                        SelectItem field = data.get(row);
                        field.setValue((String) event.getNewValue());
                    }
                });
                return new TextFieldTableCell(new DefaultStringConverter()) {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        int index = this.getTableRow().getIndex();
                    }
                };
            }
        });
        columns.get(1).setCellValueFactory(new PropertyValueFactory("label"));
        columns.get(1).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {

                param.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent event) {
                        int row = event.getTablePosition().getRow();
                        SelectItem field = data.get(row);
                        field.setLabel((String) event.getNewValue());
                    }
                });
                return new TextFieldTableCell(new DefaultStringConverter()) {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        int index = this.getTableRow().getIndex();
                    }
                };
            }
        });

        ObservableList<SelectItem> ObservableList = FXCollections.observableArrayList();
        ObservableList.addAll(data);
        datas.setItems(ObservableList);
    }

    public void refresh() {
        ObservableList<SelectItem> ObservableList = FXCollections.observableArrayList();
        ObservableList.addAll(data);
        datas.setItems(ObservableList);
    }

    public SelectItem getNewSelectItem() {
        String baseValue = "VALUE";
        String baseLabel = "值";
        String value = baseValue;
        String label = baseLabel;
        int k = 0;
        do {
            int i;
            for (i = 0; i < data.size(); i++) {
                if (value.equals(data.get(i).getValue())) {
                    break;
                }
            }
            if (i < data.size()) {
                k++;
                value = baseValue + k;
                label = baseLabel + k;
            } else {
                SelectItem SelectItem = new SelectItem();
                SelectItem.setValue(value);
                SelectItem.setLabel(label);
                return SelectItem;
            }
        } while (true);
    }
}
