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

public class MonetaryUnitConverter implements ExcelTableConverter<MonetaryUnit> {

	
	public MonetaryUnitConverter(ReferenceStorageService referenceStorage) {

	}
	
	@Override
	public Collection<MonetaryUnit> convert(ExcelTable table) {
		Collection<MonetaryUnit> units = new ArrayList<>();
		
		table.getRows().forEach((row) -> {
			String code = row.getAs("MON_UNIT_COD", String.class);


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

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

			MonetaryUnit monetaryUnit = units.stream().filter(
					s -> s.getMonetaryUnitCode().equals(code))
					.findAny().orElse(null);

			if (monetaryUnit  == null) {

				monetaryUnit = new MonetaryUnit(code);
				units.add(monetaryUnit);
			}

			monetaryUnit.addMonetaryUnitDesc(
					new MonetaryUnitDesc(row.getAs("LANG_COD", String.class), row.getAs("DESCR", String.class), null));
		});

		return units;
	}

}
