package com.ecep.contract.controller.cloud.u8;

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ManagerSkin;
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.service.CompanyService;
import com.ecep.contract.service.YongYouU8Service;
import com.ecep.contract.vm.CloudYuInfoViewModel;
import com.ecep.contract.vo.CloudYuVo;
import com.ecep.contract.vo.CompanyVo;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;

public class YongYouU8ManagerSkin
        extends
        AbstEntityManagerSkin<CloudYuVo, CloudYuInfoViewModel, YongYouU8ManagerSkin, YongYouU8ManagerWindowController>
        implements ManagerSkin {

    public YongYouU8ManagerSkin(YongYouU8ManagerWindowController controller) {
        super(controller);
    }

    YongYouU8Service getU8Service() {
        return getBean(YongYouU8Service.class);
    }

    CompanyService getCompanyService() {
        return getBean(CompanyService.class);
    }

    @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.descriptionColumn.setCellValueFactory(param -> param.getValue().getVendorCode());
    }

    @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<CloudYuInfoViewModel> selectedItems = getTableView().getSelectionModel().getSelectedItems();
        if (selectedItems.isEmpty()) {
            return;
        }
        for (CloudYuInfoViewModel selectedItem : selectedItems) {
            CloudYuVo yongYouU8Vo = getU8Service().findById(selectedItem.getId().get());
            selectedItem.getCustomerCode().set("");
            if (selectedItem.copyTo(yongYouU8Vo)) {
                CloudYuVo saved = getU8Service().save(yongYouU8Vo);
                selectedItem.update(saved);
            }
        }
    }

    @Override
    protected void onTableRowDoubleClickedAction(CloudYuInfoViewModel item) {
        Integer companyId = item.getCompany().get();
        CompanyVo company = getCompanyService().findById(companyId);
        CompanyWindowController.show(company, getTableView().getScene().getWindow());
    }
}
