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

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

import org.springframework.data.jdbc.core.mapping.AggregateReference;

import de.avatic.taricdb.helper.workbook.ExcelTable;
import de.avatic.taricdb.helper.workbook.wrapper.MeasureFootnoteWrapper;
import de.avatic.taricdb.model.Footnote;
import de.avatic.taricdb.model.MeasureFootnote;
import de.avatic.taricdb.service.ReferenceStorageService;

public class MeasureFootnoteConverter implements ExcelTableConverter<MeasureFootnoteWrapper> {

	private final ReferenceStorageService referenceStorage;

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

	@Override
	public Collection<MeasureFootnoteWrapper> convert(ExcelTable table) {
		Collection<MeasureFootnoteWrapper> measureFootnoteWrappers = new ArrayList<>();
		Collection<Footnote> footnotes = referenceStorage.getEntites(Footnote.class);

		table.getRows().forEach((row) -> {

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

			String footnote = row.getAs("Footnote", String.class);

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

			Integer measureCode = row.getAs("Meas. type code", Integer.class);
			String hsCode = row.getAs("Goods code", String.class);
			String origin = row.getAs("Origin code", String.class);
			String addCode = row.getAs("Add code", String.class);
			Integer orderNumber = row.getAs("Order No.", Integer.class);

            LocalDate startDate = row.getAs("Start date", LocalDate.class);
            LocalDate endDate = row.getAs("End date", LocalDate.class);


			Footnote foundFootnote = footnotes.stream().filter(s -> s.getFootnote().equals(footnote)).findAny()
					.orElse(null);

			MeasureFootnoteWrapper wrapper = measureFootnoteWrappers.stream()
					.filter(s ->
                               Objects.equals(s.getMeasureFootnote().getFootnote().getId(), foundFootnote != null ? foundFootnote.getId() : null)
							&& s.getMeasureCode().equals(measureCode)
                            && s.getHsCode().equals(hsCode)
							&& s.getOrigin().equals(origin)
                            && Objects.equals(s.getAddCode(), addCode)
							&& Objects.equals(s.getOrderNumber(),orderNumber)
                            && Objects.equals(s.getStartDate(),startDate)
                            && Objects.equals(s.getEndDate(),endDate))
					.findAny().orElse(null);

			if (wrapper == null) {
				wrapper = new MeasureFootnoteWrapper(new MeasureFootnote(foundFootnote != null ? AggregateReference.to(foundFootnote.getId() ): null),
						measureCode, hsCode, addCode, origin, orderNumber, startDate, endDate);
                measureFootnoteWrappers.add(wrapper);
			} else {
				throw new RuntimeException("MeasureFootnoteWrapper already exists: " + measureCode + " " + hsCode + " "
						+ origin + " " + addCode + " " + orderNumber);
			}

		});

		return measureFootnoteWrappers;
	}

}
