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

import static com.ecep.contract.SpringApp.getBean;

import com.ecep.contract.service.EmployeeService;
import com.ecep.contract.vo.EmployeeVo;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;

import lombok.Setter;

@Setter
public class EmployeeTableCell<V> extends AsyncUpdateTableCell<V, Integer, EmployeeVo> {

    public static <S> Callback<TableColumn<S, Integer>, TableCell<S, Integer>> forTableColumn() {
        return forTableColumn(getBean(EmployeeService.class));
    }

    public static <S> Callback<TableColumn<S, Integer>, TableCell<S, Integer>> forTableColumn(EmployeeService service) {
        return param -> new EmployeeTableCell<S>(service);
    }

    public EmployeeTableCell() {
    }

    public EmployeeTableCell(EmployeeService employeeService) {
        setService(employeeService);
    }

    @Override
    protected EmployeeService getServiceBean() {
        return getBean(EmployeeService.class);
    }

    @Override
    public String format(EmployeeVo entity) {
        if (entity.getCode() == null) {
            return entity.getName();
        }
        return entity.getCode() + " " + entity.getName();
    }

}
