package de.avatic.taricdb.helper.workbook.converters;

import java.util.ArrayList;
import java.util.Collection;

import de.avatic.taricdb.helper.workbook.ExcelTable;
import de.avatic.taricdb.model.CertificateType;
import de.avatic.taricdb.model.CertificateTypeDesc;
import de.avatic.taricdb.service.ReferenceStorageService;

public class CertificateTypeConverter implements ExcelTableConverter<CertificateType> {

	public CertificateTypeConverter(ReferenceStorageService referenceStorage) {
	}

	@Override
	public Collection<CertificateType> convert(ExcelTable table) {
		Collection<CertificateType> certTypes = new ArrayList<>();
		
		table.getRows().forEach((row) -> {
			String code = row.getAs("CERT_TYP_COD", String.class);
            
            if (row.index() == 0) {
                return;
            }

            if (code == null || code.isEmpty()) {
                return;
            }
            
            CertificateType certType = certTypes.stream().filter(s -> s.getCertificateTypeCode().equals(code)).findAny().orElse(null);
            
            if (certType == null) {
            	certType = new CertificateType(code);
                certTypes.add(certType);
            }
            
            certType.addCertificateTypeDesc(new CertificateTypeDesc(row.getAs("LANG_COD", String.class), row.getAs("DESCR", String.class), null));

        });
		
		
		return certTypes;
	}

}
