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

import com.ecep.contract.SpringApp;
import com.ecep.contract.VendorType;
import com.ecep.contract.service.VendorTypeService;
import com.ecep.contract.vo.VendorTypeLocalVo;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;

/**
 * 供应商类型单元格，用于在表格中显示供应商类型信息
 */
public class VendorTypeTableCell<T> extends AsyncUpdateTableCell<T, VendorType, VendorTypeLocalVo> {
    private VendorTypeService vendorTypeService;

    /**
     * 创建一个用于表格列的单元格工厂
     */
    public static <T> Callback<TableColumn<T, VendorType>, TableCell<T, VendorType>> forTableColumn(
            VendorTypeService service) {
        return param -> new VendorTypeTableCell<>(service);
    }


    public VendorTypeTableCell() {
    }

    public VendorTypeTableCell(VendorTypeService service) {
        setService(service);
    }

    @Override
    protected VendorTypeService getServiceBean() {
        if (vendorTypeService == null) {
            vendorTypeService = SpringApp.getBean(VendorTypeService.class);
        }
        return vendorTypeService;
    }

    @Override
    protected VendorTypeLocalVo initialize() {
        VendorType item = getItem();
        VendorTypeLocalVo localVo = getServiceBean().findByType(item);
        System.out.println("item = " + item + ", localVo = " + getServiceBean().getStringConverter().toString(localVo));
        return localVo;
    }

    @Override
    public String format(VendorTypeLocalVo entity) {
        return getServiceBean().getStringConverter().toString(entity);
    }
}