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

import com.ecep.contract.CompanyFileType;
import com.ecep.contract.SpringApp;
import com.ecep.contract.service.CompanyFileTypeService;
import com.ecep.contract.vo.CompanyFileTypeLocalVo;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;

/**
 * 公司文件类型单元格，用于在表格中显示公司文件类型信息
 */
public class CompanyFileTypeTableCell<T> extends AsyncUpdateTableCell<T, CompanyFileType, CompanyFileTypeLocalVo> {
    private CompanyFileTypeService companyFileTypeService;

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

    public CompanyFileTypeTableCell() {
    }

    public CompanyFileTypeTableCell(CompanyFileTypeService service) {
        setService(service);
    }

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

    @Override
    protected CompanyFileTypeLocalVo initialize() {
        CompanyFileType item = getItem();
        if (item == null) {
            return null;
        }
        return getServiceBean().findByLocaleAndType(com.ecep.contract.Desktop.instance.getActiveEmployee().localeProperty().get(), item);
    }

    @Override
    public String format(CompanyFileTypeLocalVo entity) {
        if (entity != null && entity.getValue() != null) {
            return entity.getValue();
        }
        CompanyFileType item = getItem();
        if (item != null) {
            // 根据枚举值返回对应的中文名称
            switch (item) {
                case General -> {
                    return "普通文件";
                }
                case CreditReport -> {
                    return "资信评估报告";
                }
                case BusinessLicense -> {
                    return "营业执照";
                }
                case QualificationCertificate -> {
                    return "资质证书";
                }
                case CreditInfoPublicityReport -> {
                    return "企业信用信息公示报告";
                }
                case OperationCertificate -> {
                    return "操作证";
                }
                case FrameworkAgreement -> {
                    return "框架协议";
                }
                default -> {
                    return item.name();
                }
            }
        }
        return "未知类型";
    }
}