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

public class MeasureSeriesConverter implements ExcelTableConverter<MeasureSeries> {

	
	public MeasureSeriesConverter(ReferenceStorageService referenceStorage) {
	}
	
	@Override
	public Collection<MeasureSeries> convert(ExcelTable table) {

		Collection<MeasureSeries> series = new ArrayList<>();

		table.getRows().forEach((row) -> {
			String code = row.getAs("MEAS_TYP_SER_ID", String.class);

			if (row.index() == 0) {
				return;
			}

            if (code == null || code.isEmpty()) {
                return;
            }

			MeasureSeries measureSeries = series.stream().filter(s -> s.getMeasureSeriesCode().equals(code)).findAny().orElse(null);

			if (measureSeries == null) {
				measureSeries = new MeasureSeries(code);
				series.add(measureSeries);
			}

			measureSeries.addMeasureSeriesDesc(new MeasureSeriesDesc(row.getAs("LANG_COD", String.class),
					row.getAs("DESCR", String.class)));
		});

		return series;
	}

}
