package com.ecep.contract.ds.customer;

import java.util.function.Function;

import org.hibernate.Hibernate;

import com.ecep.contract.ds.converter.EntityStringConverter;
import com.ecep.contract.model.CustomerCatalog;

public class CustomerClassStringConverter extends EntityStringConverter<CustomerCatalog> {

    private Function<CustomerCatalog, CustomerCatalog> initialized;
    private Function<String, CustomerCatalog> fromString;

    @Override
    public String toString(CustomerCatalog cc) {
        if (cc == null) {
            return "-";
        }
        if (initialized != null && !Hibernate.isInitialized(cc)) {
            cc = initialized.apply(cc);
        }
        return cc.getCode() + " " + cc.getName();
    }

    @Override
    public CustomerCatalog fromString(String string) {
        if (fromString == null) {
            return null;
        }
        return fromString.apply(string);
    }

    public void setFromString(Function<String, CustomerCatalog> fromString) {
        this.fromString = fromString;
    }

    public void setInitialized(Function<CustomerCatalog, CustomerCatalog> initialized) {
        this.initialized = initialized;
    }
}
