package com.ecep.contract.converter;

import java.util.List;

import org.springframework.util.StringUtils;

import com.ecep.contract.model.ContractGroup;

import javafx.util.StringConverter;

public class ContractGroupStringConverter extends EntityStringConverter<ContractGroup> {

    private List<ContractGroup> dataset;

    public ContractGroupStringConverter() {
    }

    public ContractGroupStringConverter(List<ContractGroup> dataset) {
        this.dataset = dataset;
    }

    @Override
    public String toString(ContractGroup group) {
        return group == null ? "All" : ContractGroup.toString(group);
    }

    @Override
    public ContractGroup fromString(String string) {
        if (dataset == null) {
            return null;
        }
        if (!StringUtils.hasText(string)) {
            return null;
        }

        for (ContractGroup group : dataset) {
            if (group == null) {
                continue;
            }
            if (ContractGroup.toString(group).equals(string)) {
                return group;
            }
            if (group.getCode().equals(string)) {
                return group;
            }
            if (group.getName().contains(string)) {
                return group;
            }
        }
        return null;
    }
}
