package com.ecep.contract.controller.tab;

import java.time.format.DateTimeFormatter;
import java.util.Map;

import com.ecep.contract.ContractPayWay;
import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ComboBoxUtils;
import com.ecep.contract.controller.ManagerSkin;
import com.ecep.contract.controller.contract.ContractManagerWindowController;
import com.ecep.contract.controller.contract.ContractWindowController;
import com.ecep.contract.controller.table.cell.CompanyTableCell;
import com.ecep.contract.controller.table.cell.ContractGroupTableCell;
import com.ecep.contract.controller.table.cell.ContractKindTableCell;
import com.ecep.contract.controller.table.cell.ContractTypeTableCell;
import com.ecep.contract.controller.table.cell.EmployeeTableCell;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.ContractGroup;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.ContractGroupService;
import com.ecep.contract.service.ContractKindService;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.service.ContractTypeService;
import com.ecep.contract.vm.ContractViewModel;

import javafx.application.Platform;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.converter.CurrencyStringConverter;
import javafx.util.converter.LocalDateTimeStringConverter;
import lombok.Setter;

public class ContractManagerSkin
        extends AbstEntityManagerSkin<Contract, ContractViewModel, ContractManagerSkin, ContractManagerWindowController>
        implements ManagerSkin {
    @Setter
    private CompanyService companyService;
    @Setter
    private ContractGroupService contractGroupService;
    @Setter
    private ContractKindService contractKindService;
    @Setter
    private ContractTypeService contractTypeService;

    public ContractManagerSkin(ContractManagerWindowController controller) {
        super(controller);
    }

    public ContractService getContractService() {
        return controller.getViewModelService();
    }
    public CompanyService getCompanyService() {
        return getBean(CompanyService.class);
    }
    private ContractTypeService getContractTypeService() {
        return getBean(ContractTypeService.class);
    }
    private ContractKindService getContractKindService() {
        return getBean(ContractKindService.class);
    }
    private ContractGroupService getContractGroupService() {
        return getBean(ContractGroupService.class);
    }
    public Map<String, Object> getSpecification() {
        Map<String, Object> params = super.getSpecification();
        if (controller.composeViewBtn.isSelected()) {

            // TODO 完善查询条件
            params.put("payWay", ContractPayWay.RECEIVE);

            // spec = SpecificationUtils.and(spec, (root, query, builder) -> {
            // Path<String> parentCode = root.get("parentCode");
            // Path<ContractPayWay> payWay = root.get("payWay");
            // return builder.or(
            // builder.equal(payWay, ContractPayWay.RECEIVE),
            // builder.and(
            // builder.equal(payWay, ContractPayWay.PAY),
            // builder.or(
            // builder.equal(parentCode, ""),
            // parentCode.isNull())));
            // });

        }
        ContractGroup selectedGroup = controller.groupSelector.getValue();
        if (selectedGroup != null) {
            params.put("group", selectedGroup);
        }
        return params;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void initializeTable() {
        ComboBoxUtils.initialComboBox(controller.groupSelector, getContractGroupService().findAll(), true);
        controller.groupSelector.valueProperty().addListener((observable, oldValue, newValue) -> {
            loadTableDataSet(false);
        });

        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());
        controller.nameColumn.setCellValueFactory(param -> param.getValue().getName());
        controller.codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        controller.groupColumn.setCellValueFactory(param -> param.getValue().getGroup());
        controller.groupColumn.setCellFactory(param -> new ContractGroupTableCell<>(getContractGroupService()));
        controller.typeColumn.setCellValueFactory(param -> param.getValue().getType());
        controller.typeColumn.setCellFactory(param -> new ContractTypeTableCell<>(getContractTypeService()));
        controller.kindColumn.setCellValueFactory(param -> param.getValue().getKind());
        controller.kindColumn.setCellFactory(param -> new ContractKindTableCell<>(getContractKindService()));

        controller.parentCodeColumn.setCellValueFactory(param -> param.getValue().getParentCode());

        controller.setupDateColumn.setCellValueFactory(param -> param.getValue().getSetupDate());
        controller.orderDateColumn.setCellValueFactory(param -> param.getValue().getOrderDate());
        controller.startDateColumn.setCellValueFactory(param -> param.getValue().getStartDate());

        controller.employeeColumn.setCellValueFactory(param -> param.getValue().getEmployee());
        controller.employeeColumn
                .setCellFactory(param -> new EmployeeTableCell<>(controller.getEmployeeService()));

        controller.createdColumn.setCellValueFactory(param -> param.getValue().getCreated());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(MyDateTimeUtils.DEFAULT_DATETIME_FORMAT_PATTERN);
        controller.createdColumn
                .setCellFactory(TextFieldTableCell.forTableColumn(new LocalDateTimeStringConverter(formatter, null)));

        controller.amountColumn.setCellValueFactory(param -> param.getValue().getAmount());
        controller.amountColumn
                .setCellFactory(TextFieldTableCell.forTableColumn(new CurrencyStringConverter(getLocale(), "#,##0")));

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

        Platform.runLater(() -> {
            controller.composeViewBtn.selectedProperty().addListener((observable, oldValue, newValue) -> {
                loadTableDataSet(false);
            });
            controller.parentCodeColumn.visibleProperty().bind(controller.composeViewBtn.selectedProperty().not());
            getTableView().getSortOrder().addAll(controller.createdColumn, controller.setupDateColumn);
        });
    }

    @Override
    protected void onTableRowDoubleClickedAction(ContractViewModel item) {
        showInOwner(item);
    }

    private void showInOwner(ContractViewModel model) {
        showInOwner(ContractWindowController.class, model);
    }

}
