package com.ecep.contract.controller.table.cell;

import com.ecep.contract.SpringApp;
import com.ecep.contract.service.InventoryCatalogService;
import com.ecep.contract.service.InventoryService;
import com.ecep.contract.vo.InventoryCatalogVo;
import com.ecep.contract.vo.InventoryVo;

import javafx.util.Callback;
import lombok.NoArgsConstructor;

@NoArgsConstructor
public class InventoryTableCell<V> extends AsyncUpdateTableCell<V, Integer, InventoryVo> {
    /**
     * 创建单元格工厂
     * 
     * @param inventoryService 库存服务
     * @return 单元格工厂
     */
    public static <V> Callback<javafx.scene.control.TableColumn<V, Integer>, javafx.scene.control.TableCell<V, Integer>> forTableColumn(
            InventoryService inventoryService) {
        return param -> new InventoryTableCell<>(inventoryService);
    }

    private InventoryCatalogService inventoryCatalogService;

    public InventoryTableCell(InventoryService service) {
        setService(service);
    }

    InventoryCatalogService getInventoryCatalogService() {
        if (inventoryCatalogService == null) {
            inventoryCatalogService = SpringApp.getBean(InventoryCatalogService.class);
        }
        return inventoryCatalogService;
    }

    @Override
    public String format(InventoryVo entity) {
        Integer catalogId = entity.getCatalogId();
        InventoryCatalogVo catalog = getInventoryCatalogService().findById(catalogId);
        return (catalog != null ? (catalog.getName() + " ") : "") + entity.getName();
    }

}
