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.DictItem;

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

public class OptionDictController {

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

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

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

    public void setData(List<DictItem> 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 getDictItems() {
        return dictItems;
    }

    public void setDictItems(TableView dictItems) {
        this.dictItems = dictItems;
    }

    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() {
        dictItems.setEditable(true);
        dictItems.setSortPolicy(new Callback<TableView, Boolean>() {
            @Override
            public Boolean call(TableView param) {
                //禁止点击列头排序
                return false;
            }
        });
        ObservableList<TableColumn> columns = dictItems.getColumns();
        columns.get(0).setCellValueFactory(new PropertyValueFactory("key"));
        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();
                        DictItem field = data.get(row);
                        field.setKey((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("value"));
        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();
                        DictItem 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();
                    }
                };
            }
        });

        ObservableList<DictItem> ObservableList = FXCollections.observableArrayList();
        ObservableList.addAll(data);
        dictItems.setItems(ObservableList);
    }

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

    public DictItem getNewDictItem() {
        String baseKey = "KEY";
        String baseValue = "VALUE";
        String key = baseKey;
        String value = baseValue;
        int k = 0;
        do {
            int i;
            for (i = 0; i < data.size(); i++) {
                if (key.equals(data.get(i).getKey())) {
                    break;
                }
            }
            if (i < data.size()) {
                k++;
                key = baseKey + k;
                value = baseValue + k;
            } else {
                DictItem dictItem = new DictItem();
                dictItem.setKey(key);
                dictItem.setValue(value);
                return dictItem;
            }
        } while (true);
    }
}
