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.service.PurchaseOrderItemService;
import com.ecep.contract.service.PurchaseOrdersService;
import com.ecep.contract.vo.InventoryVo;
import com.ecep.contract.vo.PurchaseOrderItemVo;

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class PurchaseOrderItemTableCell<V> extends AsyncUpdateTableCell<V, Integer, PurchaseOrderItemVo> {
    PurchaseOrdersService purchaseOrdersService;
    PurchaseOrderItemService purchaseOrderItemService;
    private InventoryService inventoryService;
    private InventoryCatalogService inventoryCatalogService;

    public PurchaseOrderItemTableCell(PurchaseOrderItemService service) {
        purchaseOrderItemService = service;
    }

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

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

    PurchaseOrdersService getPurchaseOrdersService() {
        if (purchaseOrdersService == null)
            purchaseOrdersService = SpringApp.getBean(PurchaseOrdersService.class);
        return purchaseOrdersService;
    }

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

    @Override
    public String format(PurchaseOrderItemVo orderItem) {
        InventoryVo inventory = getInventoryService().findById(orderItem.getInventoryId());
        return (inventory != null ? (inventory.getName() + " x ") : "") + orderItem.getQuantity();
    }
}
