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

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;

import de.avatic.taricdb.helper.workbook.ExcelTable;
import de.avatic.taricdb.model.AdditionalCode;
import de.avatic.taricdb.model.AdditionalCodeDesc;
import de.avatic.taricdb.service.ReferenceStorageService;

public class AdditionalCodeConverter implements ExcelTableConverter<AdditionalCode> {

	private ReferenceStorageService referenceStorage;

	public AdditionalCodeConverter(ReferenceStorageService referenceStorage) {
		this.referenceStorage = referenceStorage;
	}

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

            if (code == null || code.isEmpty()) {
                return;
            }
			
			AdditionalCode addCode = codes.stream().filter(c -> c.getAdditionalCode().equals(code)).findAny().orElse(null);
			
			if (addCode == null) {
				addCode = new AdditionalCode(code, row.getAs("Start date", LocalDate.class), row.getAs("End date", LocalDate.class));
				codes.add(addCode);
			}
			
			addCode.addDesc(new AdditionalCodeDesc(row.getAs("Language", String.class), row.getAs("Description", String.class), row.getAs("Descr. start date", LocalDate.class)));
			
			
		});
		
		return codes;
	}

}
