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

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

import de.avatic.taricdb.helper.workbook.ExcelTable;
import de.avatic.taricdb.model.Footnote;
import de.avatic.taricdb.model.FootnotesDesc;
import de.avatic.taricdb.service.ReferenceStorageService;

public class FootnoteConverter implements ExcelTableConverter<Footnote> {
	
	public FootnoteConverter(ReferenceStorageService referenceStorage) {
	}
	
	public Collection<Footnote> convert(ExcelTable table) {
		List<Footnote> footnotes = new ArrayList<>();


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

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

            if (txt == null || txt.isEmpty()) {
                return;
            }
			
			Footnote footnote = footnotes.stream().filter(f -> f.getFootnote().equals(txt)).findAny().orElse(null);

			if (footnote == null) {
				footnote = new Footnote(txt, row.getAs("Description", String.class),
						row.getAs("Start date", LocalDate.class), row.getAs("End date", LocalDate.class));
				footnotes.add(footnote);
			}
			
			footnote.addFootnotesDesc(new FootnotesDesc(row.getAs("Description", String.class),
					row.getAs("Language", String.class), row.getAs("Descr. start date", LocalDate.class)));
		});
		
		
		return footnotes;

	}
}
