package com.ecep.contract.controller.vendor.purchase.purchase_bill;

import java.util.Map;

import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.controller.table.AbstEntityTableTabSkin;
import com.ecep.contract.controller.table.cell.ContractTableCell;
import com.ecep.contract.controller.table.cell.InventoryTableCell;
import com.ecep.contract.controller.table.cell.InvoiceTableCell;
import com.ecep.contract.controller.table.cell.PurchaseOrderItemTableCell;
import com.ecep.contract.controller.vendor.PurchaseBillVoucherWindowController;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.Inventory;
import com.ecep.contract.model.Invoice;
import com.ecep.contract.model.PurchaseBillVoucher;
import com.ecep.contract.model.PurchaseBillVoucherItem;
import com.ecep.contract.model.PurchaseOrderItem;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.service.InventoryService;
import com.ecep.contract.service.InvoiceService;
import com.ecep.contract.service.PurchaseBillVoucherItemService;
import com.ecep.contract.service.PurchaseOrderItemService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.PurchaseBillVoucherItemViewModel;
import com.ecep.contract.vm.PurchaseBillVoucherViewModel;

import javafx.geometry.Pos;
import javafx.scene.control.Tab;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
import lombok.Setter;

@FxmlPath("/ui/contract/purchase-bill-voucher-tab-item.fxml")
public class PurchaseBillVoucherTabSkinItems
        extends AbstEntityTableTabSkin<PurchaseBillVoucherWindowController, PurchaseBillVoucher, PurchaseBillVoucherViewModel, PurchaseBillVoucherItem, PurchaseBillVoucherItemViewModel>
        implements TabSkin {

    /*        以下是合同内容          */
    public TableColumn<PurchaseBillVoucherItemViewModel, Number> idColumn;
    public TableColumn<PurchaseBillVoucherItemViewModel, Number> refIdColumn;
    public TableColumn<PurchaseBillVoucherItemViewModel, Inventory> inventoryColumn;
    public TableColumn<PurchaseBillVoucherItemViewModel, Invoice> invoiceColumn;
    public TableColumn<PurchaseBillVoucherItemViewModel, PurchaseOrderItem> orderItemColumn;
    public TableColumn<PurchaseBillVoucherItemViewModel, Contract> contractColumn;

    public TableColumn<PurchaseBillVoucherItemViewModel, Double> quantityColumn;
    public TableColumn<PurchaseBillVoucherItemViewModel, Double> priceColumn;
    public TableColumn<PurchaseBillVoucherItemViewModel, String> descriptionColumn;

    @Setter
    private PurchaseBillVoucherItemService service;
    @Setter
    private ContractService contractService;
    @Setter
    private InventoryService inventoryService;
    @Setter
    private InvoiceService invoiceService;
    @Setter
    private PurchaseOrderItemService purchaseOrderItemService;

    public PurchaseBillVoucherTabSkinItems(PurchaseBillVoucherWindowController controller) {
        super(controller);
    }

    @Override
    protected PurchaseBillVoucherItemService getViewModelService() {
        if (service == null) {
            service = SpringApp.getBean(PurchaseBillVoucherItemService.class);
        }
        return service;
    }

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

    InventoryService getInventoryService() {
        if (inventoryService == null) {
            inventoryService = SpringApp.getBean(InventoryService.class);
        }
        return inventoryService;
    }

    InvoiceService getInvoiceService() {
        if (invoiceService == null) {
            invoiceService = SpringApp.getBean(InvoiceService.class);
        }
        return invoiceService;
    }

    PurchaseOrderItemService getPurchaseOrderItemService() {
        if (purchaseOrderItemService == null) {
            purchaseOrderItemService = SpringApp.getBean(PurchaseOrderItemService.class);
        }
        return purchaseOrderItemService;
    }

    @Override
    public Map<String, Object> getSpecification(PurchaseBillVoucher parent) {
        Map<String, Object> params = getSpecification();
        params.put("voucher", parent.getId());
        return params;
    }

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

    @Override
    public void initializeTab() {
        idColumn.setCellValueFactory(param -> param.getValue().getId());
        refIdColumn.setCellValueFactory(param -> param.getValue().getRefId());
        inventoryColumn.setCellValueFactory(param -> param.getValue().getInventory());
        inventoryColumn.setCellFactory(col -> new InventoryTableCell<>(getInventoryService()));
        invoiceColumn.setCellValueFactory(param -> param.getValue().getInvoice());
        invoiceColumn.setCellFactory(col -> new InvoiceTableCell<>(getInvoiceService()));
        contractColumn.setCellValueFactory(param -> param.getValue().getContract());
        contractColumn.setCellFactory(col -> new ContractTableCell<>(getContractService()));
        orderItemColumn.setCellValueFactory(param -> param.getValue().getOrderItem());
        orderItemColumn.setCellFactory(col -> new PurchaseOrderItemTableCell<>(getPurchaseOrderItemService()));
        NumberCellFactory v1 = new NumberCellFactory("%,.1f", null);
        NumberCellFactory v2 = new NumberCellFactory("%,.2f", null);

        quantityColumn.setCellValueFactory(param -> param.getValue().getQuantity().asObject());
        quantityColumn.setCellFactory(v1);

        priceColumn.setCellValueFactory(param -> param.getValue().getPrice().asObject());
        priceColumn.setCellFactory(v2);

        descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());


        super.initializeTab();
    }

    static class NumberCellFactory implements Callback<TableColumn<PurchaseBillVoucherItemViewModel, Double>, TableCell<PurchaseBillVoucherItemViewModel, Double>> {
        private final String format;

        public NumberCellFactory(String format, String styleClass) {
            this.format = format;
        }

        @Override
        public TableCell<PurchaseBillVoucherItemViewModel, Double> call(TableColumn<PurchaseBillVoucherItemViewModel, Double> param) {
            NumberTableCell cell = new NumberTableCell(format);
            cell.setAlignment(Pos.CENTER_RIGHT);
            return cell;
        }
    }

    static class NumberTableCell extends TableCell<PurchaseBillVoucherItemViewModel, Double> {
        private final String format;

        public NumberTableCell(String format) {
            this.format = format;
        }

        @Override
        protected void updateItem(Double item, boolean empty) {
            super.updateItem(item, empty);
            if (empty || item == null) {
                setText(null);
                return;
            }
            setText(String.format(format, item));
        }
    }


}
