package com.ecep.contract.controller;

import com.ecep.contract.util.ProxyUtils;

import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.company.CompanyWindowController;
import com.ecep.contract.controller.table.cell.CompanyTableCell;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;
import com.ecep.contract.model.CloudRk;
import com.ecep.contract.model.Company;
import com.ecep.contract.service.CloudRkService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.vm.CloudRkViewModel;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.CheckBoxTableCell;
import lombok.Setter;

public class CloudRkManagerSkin
        extends AbstEntityManagerSkin<CloudRk, CloudRkViewModel, CloudRkManagerSkin, CloudRkManagerWindowController> {
    @Setter
    private CompanyService companyService;

    public CloudRkManagerSkin(CloudRkManagerWindowController controller) {
        super(controller);
    }

    CloudRkService getCloudRkService() {
        return controller.getViewModelService();
    }

    CompanyService getCompanyService() {
        if (companyService == null) {
            companyService = SpringApp.getBean(CompanyService.class);
        }
        return companyService;
    }

    @Override
    public void initializeTable() {
        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());

        controller.companyColumn.setCellValueFactory(param -> param.getValue().getCompany());
        controller.companyColumn.setCellFactory(param -> new CompanyTableCell<>(getCompanyService()));

        controller.cloudIdColumn.setCellValueFactory(param -> param.getValue().getCloudId());

        controller.latestUpdateColumn.setCellValueFactory(param -> param.getValue().getLatestUpdate());
        controller.latestUpdateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        controller.cloudLatestColumn.setCellValueFactory(param -> param.getValue().getCloudLatest());
        controller.cloudLatestColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        controller.cloudBlackListUpdatedColumn
                .setCellValueFactory(param -> param.getValue().getCloudBlackListUpdated());
        controller.cloudBlackListUpdatedColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        controller.cloudEntUpdateColumn.setCellValueFactory(param -> param.getValue().getCloudEntUpdate());
        controller.cloudEntUpdateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        controller.autoUpdateColumn.setCellValueFactory(param -> param.getValue().getAutoUpdate());
        controller.autoUpdateColumn.setCellFactory(CheckBoxTableCell.forTableColumn(controller.autoUpdateColumn));

        controller.descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());
    }

    private void onAutoUpdateColumnEditCommit(TableColumn.CellEditEvent<CloudRkViewModel, Boolean> event) {
        CloudRkViewModel row = event.getRowValue();
        row.getAutoUpdate().set(event.getNewValue());
        saveRowData(row);
    }

    @Override
    protected void createContextMenu(ContextMenu contextMenu) {
        MenuItem item2 = new MenuItem("刷新");
        item2.setOnAction(this::onTableRefreshAction);

        MenuItem item3 = new MenuItem("清空备注");
        item3.setOnAction(this::onTableClearDescriptionAction);

        contextMenu.getItems().addAll(item2, item3);
    }

    /**
     * 请空选择行的注释
     *
     * @param event event
     */
    public void onTableClearDescriptionAction(ActionEvent event) {
        ObservableList<CloudRkViewModel> selectedItems = getTableView().getSelectionModel().getSelectedItems();
        if (selectedItems.isEmpty()) {
            return;
        }
        CloudRkService service = getCloudRkService();
        for (CloudRkViewModel selectedItem : selectedItems) {
            selectedItem.getDescription().set("");
            selectedItem.saveInFxApplicationThread(service);
        }
    }

    @Override
    protected void onTableRowDoubleClickedAction(CloudRkViewModel item) {
        Company company = item.getCompany().get();
        if (!ProxyUtils.isInitialized(company)) {
            company = getCompanyService().findById(company.getId());
            item.getCompany().set(company);
        }
        CompanyWindowController.show(company, getTableView().getScene().getWindow());
    }
}
