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

import com.ecep.contract.ContractFileType;
import com.ecep.contract.SpringApp;
import com.ecep.contract.service.ContractFileTypeService;
import com.ecep.contract.vo.ContractFileTypeLocalVo;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;

/**
 * 合同文件类型单元格，用于在表格中显示合同文件类型信息
 */
public class ContractFileTypeTableCell<T> extends AsyncUpdateTableCell<T, ContractFileType, ContractFileTypeLocalVo> {
    private ContractFileTypeService contractFileTypeService;

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

    public ContractFileTypeTableCell() {
    }

    public ContractFileTypeTableCell(ContractFileTypeService service) {
        setService(service);
    }

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

    @Override
    protected ContractFileTypeLocalVo initialize() {
        ContractFileType item = getItem();
        ContractFileTypeLocalVo localVo = getServiceBean().findByType(com.ecep.contract.Desktop.instance.getActiveEmployee().localeProperty().get(), item);
        return localVo;
    }

    @Override
    public String format(ContractFileTypeLocalVo entity) {
        if (entity == null) {
            return null;
        }
        return entity.getValue();
    }
}
