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.ConditionType;
import de.avatic.taricdb.model.ConditionTypeDesc;
import de.avatic.taricdb.service.ReferenceStorageService;

public class ConditionTypeConverter implements ExcelTableConverter<ConditionType> {



	public ConditionTypeConverter(ReferenceStorageService referenceStorage) {

	}

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

            if (code == null || code.isEmpty()) {
                return;
            }
            
            ConditionType conditionType = conditionTypes.stream().filter(s -> s.getConditionTypeCode().equals(code)).findAny().orElse(null);
            
            if (conditionType == null) {
                conditionType = new ConditionType(code);
                conditionTypes.add(conditionType);
            }
            
            conditionType.addConditionTypeDesc(new ConditionTypeDesc(row.getAs("LANG_COD", String.class), row.getAs("DESCR", String.class), null));
            
        });
		
		
		return conditionTypes;
	}

}
