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

public class MeasureActionConverter implements ExcelTableConverter<MeasureAction> {

	
	public MeasureActionConverter(ReferenceStorageService referenceStorage) {
	}
	
	
	@Override
	public Collection<MeasureAction> convert(ExcelTable table) {
		Collection<MeasureAction> actions = new ArrayList<>();
		
		table.getRows().forEach((row) -> {
			String code = row.getAs("MEAS_ACT_COD", String.class);
            
            if (row.index() == 0) {
                return;
            }

            if (code == null || code.isEmpty()) {
                return;
            }
            
            MeasureAction measureAction = actions.stream().filter(s -> s.getMeasureActionCode().equals(code)).findAny().orElse(null);
            
            if (measureAction == null) {
                measureAction = new MeasureAction(code);
                actions.add(measureAction);
            }
            
            measureAction.addMeasureActionDesc(new MeasureActionDesc(row.getAs("LANG_COD", String.class), row.getAs("DESCR", String.class), null));
            
        });
		
		
		return actions;
	}

}
