package com.ecep.contract.controller.tab;

import java.time.LocalDate;

import com.ecep.contract.BlackReasonType;
import com.ecep.contract.controller.company.AbstCompanyTableTabSkin;
import com.ecep.contract.controller.company.CompanyWindowController;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.model.CloudRk;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyBlackReason;
import com.ecep.contract.service.CloudRkService;
import com.ecep.contract.service.CompanyBlackReasonService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.CompanyBlackReasonViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import lombok.Setter;

/**
 * 子合同
 */
@FxmlPath("/ui/company/company-tab-black-list.fxml")
public class CompanyTabSkinBlackReason
        extends AbstCompanyTableTabSkin<CompanyBlackReason, CompanyBlackReasonViewModel>
        implements TabSkin, EditableEntityTableTabSkin<CompanyBlackReason, CompanyBlackReasonViewModel> {
    /**
     * 以下是黑名单列定义
     */
    public TableColumn<CompanyBlackReasonViewModel, Number> blackReasonTable_idColumn;
    public TableColumn<CompanyBlackReasonViewModel, BlackReasonType> blackReasonTable_typeColumn;
    public TableColumn<CompanyBlackReasonViewModel, String> blackReasonTable_applyNameColumn;
    public TableColumn<CompanyBlackReasonViewModel, String> blackReasonTable_blackReasonColumn;
    public TableColumn<CompanyBlackReasonViewModel, String> blackReasonTable_descriptionColumn;
    public TableColumn<CompanyBlackReasonViewModel, LocalDate> blackReasonTable_applyDateColumn;
    public TableColumn<CompanyBlackReasonViewModel, LocalDate> blackReasonTable_updateTimeColumn;
    public TableColumn<CompanyBlackReasonViewModel, LocalDate> blackReasonTable_createTimeColumn;
    public TableColumn<CompanyBlackReasonViewModel, LocalDate> blackReasonTable_includeDateColumn;
    public TableColumn<CompanyBlackReasonViewModel, String> blackReasonTable_keyColumn;

    public TextField blackListSearchKeyField;
    public Button blackListSearchBtn;
    public MenuItem blackReasonTable_menu_refresh;
    public MenuItem blackReasonTable_menu_update;

    @Setter
    private CompanyBlackReasonService companyBlackReasonService;
    @Setter
    private CloudRkService cloudRkService;

    public CompanyTabSkinBlackReason(CompanyWindowController controller) {
        super(controller);
    }

    @Override
    public Tab getTab() {
        return controller.blackReasonTab;
    }

    @Override
    public void initializeTab() {

        blackListSearchKeyField.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.ENTER) {
                blackListSearchBtn.fire();
            }
        });

        blackListSearchBtn.setOnAction(this::onTableRefreshAction);

        blackReasonTable_idColumn.setCellValueFactory(param -> param.getValue().getId());
        blackReasonTable_typeColumn.setCellValueFactory(param -> param.getValue().getType());
        blackReasonTable_applyNameColumn.setCellValueFactory(param -> param.getValue().getApplyName());
        blackReasonTable_blackReasonColumn.setCellValueFactory(param -> param.getValue().getBlackReason());
        blackReasonTable_descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());

        blackReasonTable_applyDateColumn.setCellValueFactory(param -> param.getValue().getApplyDate());
        blackReasonTable_updateTimeColumn.setCellValueFactory(param -> param.getValue().getUpdateTime());
        blackReasonTable_createTimeColumn.setCellValueFactory(param -> param.getValue().getUpdateTime());
        blackReasonTable_includeDateColumn.setCellValueFactory(param -> param.getValue().getCreateTime());

        blackReasonTable_keyColumn.setCellValueFactory(param -> param.getValue().getKey());

        blackReasonTable_menu_refresh.setOnAction(this::onTableRefreshAction);
        blackReasonTable_menu_update.setOnAction(this::onTableUpdateAction);


        super.initializeTab();
    }


    private void onTableUpdateAction(ActionEvent event) {
        Company company = getParent();

        CloudRkService cloudRkService = getCloudRkService();
        CloudRk cloudRk = cloudRkService.getOrCreateCloudRk(company);
        if (cloudRkService.checkBlackListUpdateElapse(company, cloudRk)) {
            try {
                cloudRkService.updateBlackList(company, cloudRk);
            } catch (Exception e) {
                UITools.showExceptionAndWait("更新黑名单时发生错误", e);
            }
        }
    }

    @Override
    protected void onTableRowDoubleClickedAction(CompanyBlackReasonViewModel item) {
//        ContractWindowController.show(item, controller.root.getScene().getWindow());
    }

    @Override
    public CompanyBlackReason loadRowData(CompanyBlackReasonViewModel row) {
        return getViewModelService().findById(row.getId().get());
    }

    @Override
    public CompanyBlackReason saveRowData(CompanyBlackReason entity) {
        return getViewModelService().save(entity);
    }

    @Override
    public void deleteRowData(CompanyBlackReason entity) {
        getViewModelService().delete(entity);
    }

    protected CompanyBlackReasonService getViewModelService() {
        if (companyBlackReasonService == null) {
            companyBlackReasonService = getBean(CompanyBlackReasonService.class);
        }
        return companyBlackReasonService;
    }

    public CloudRkService getCloudRkService() {
        if (cloudRkService == null) {
            cloudRkService = getBean(CloudRkService.class);
        }
        return cloudRkService;
    }
}
