package com.ecep.contract.controller.inventory;

import java.text.NumberFormat;
import java.time.LocalDate;
import java.util.Map;

import com.ecep.contract.controller.contract.ContractWindowController;
import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.controller.table.AbstEntityTableTabSkin;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.Inventory;
import com.ecep.contract.service.ContractItemService;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.service.InventoryHistoryPriceService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.ContractViewModel;
import com.ecep.contract.vm.InventoryViewModel;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.util.converter.CurrencyStringConverter;
import lombok.Setter;

@FxmlPath("/ui/inventory/inventory-contract.fxml")
public class InventoryTabSkinContracts
        extends
        AbstEntityTableTabSkin<InventoryWindowController, Inventory, InventoryViewModel, Contract, ContractViewModel>
        implements TabSkin {
    public TableColumn<ContractViewModel, Number> idColumn;
    public TableColumn<ContractViewModel, String> nameColumn;
    public TableColumn<ContractViewModel, String> codeColumn;
    public TableColumn<ContractViewModel, LocalDate> setupDateColumn;

    @Setter
    InventoryHistoryPriceService inventoryHistoryPriceService;
    @Setter
    ContractService contractService;
    @Setter
    ContractItemService contractItemService;

    public InventoryTabSkinContracts(InventoryWindowController controller) {
        super(controller);
    }

    @Override
    protected ContractService getViewModelService() {
        return getContractService();
    }

    InventoryHistoryPriceService getInventoryHistoryPriceService() {
        if (inventoryHistoryPriceService == null) {
            inventoryHistoryPriceService = getBean(InventoryHistoryPriceService.class);
        }
        return inventoryHistoryPriceService;
    }

    ContractItemService getContractItemService() {
        if (contractItemService == null) {
            contractItemService = getBean(ContractItemService.class);
        }
        return contractItemService;
    }

    ContractService getContractService() {
        if (contractService == null) {
            contractService = getBean(ContractService.class);
        }
        return contractService;
    }

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

    @Override
    public void initializeTable() {
        super.initializeTable();
        NumberFormat numberInstance = NumberFormat.getNumberInstance(getLocale());
        numberInstance.setMaximumFractionDigits(2);
        numberInstance.setMinimumFractionDigits(2);
        CurrencyStringConverter currencyStringConverter = new CurrencyStringConverter(numberInstance);

        idColumn.setCellValueFactory(param -> param.getValue().getId());
        nameColumn.setCellValueFactory(param -> param.getValue().getName());
        codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        setupDateColumn.setCellValueFactory(param -> param.getValue().getSetupDate());
    }

    @Override
    public Map<String, Object> getSpecification(Inventory parent) {
        Map<String, Object> params = getSpecification();
        params.put("inventory", parent);

        // return SpecificationUtils.and(getSpecification(), (root, query, builder) -> {
        // // 创建ContractItem的子查询
        // Subquery<Integer> subquery = query.subquery(Integer.class);
        // Root<ContractItem> from = subquery.from(ContractItem.class);
        // // 子查询选择与指定库存相关的合同ID
        // subquery.select(from.get("contract").get("id"))
        // .where(builder.equal(from.get("inventory"), parent));

        // // 主查询筛选ID在子查询结果中的合同
        // return builder.in(root.get("id")).value(subquery);
        // });
        return params;
    }

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

    @Override
    protected void onTableRowDoubleClickedAction(ContractViewModel item) {
        showInOwner(ContractWindowController.class, item);
    }
}
