package com.ecep.contract.controller.inventory;

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

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.service.ContractItemService;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.service.InventoryHistoryPriceService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.ContractViewModel;
import com.ecep.contract.vm.InventoryViewModel;
import com.ecep.contract.vo.ContractVo;
import com.ecep.contract.vo.InventoryVo;

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, InventoryVo, InventoryViewModel, ContractVo, 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 ParamUtils.Builder getSpecification(InventoryVo parent) {
        ParamUtils.Builder params = getSpecification();
        params.equals("inventory", parent);
        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);
    }
}
