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

import com.ecep.contract.ContractFileType;
import com.ecep.contract.Desktop;
import com.ecep.contract.service.ContractFileTypeService;
import com.ecep.contract.vo.ContractFileTypeLocalVo;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;

/**
 * @param <T>
 */
public class ContractFileTypeListCell<T> extends ListCell<ContractFileType> {
    public static Callback<ListView<ContractFileType>, ListCell<ContractFileType>> forListView(ContractFileTypeService typeService) {
        return listView -> new ContractFileTypeListCell<>(typeService);
    }

    private final ContractFileTypeService service;

    public ContractFileTypeListCell(ContractFileTypeService service) {
        this.service = service;
    }

    @Override
    protected void updateItem(ContractFileType item, boolean empty) {
        super.updateItem(item, empty);
        if (item == null || empty) {
            setText(null);
            return;
        }
        ContractFileTypeLocalVo local = service.findByType(Desktop.instance.getActiveEmployee().localeProperty().get(), item);
        if (local == null) {
            setText(item.name());
        } else {
            setText(local.getValue());
        }
    }


}
