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

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jdbc.core.mapping.AggregateReference;

import de.avatic.taricdb.helper.stats.HeapStat;
import de.avatic.taricdb.helper.workbook.ExcelTable;
import de.avatic.taricdb.helper.workbook.wrapper.AppliedMeasureWrapper;
import de.avatic.taricdb.helper.workbook.wrapper.ImportWrapper;
import de.avatic.taricdb.model.AdditionalCode;
import de.avatic.taricdb.model.AppliedMeasure;
import de.avatic.taricdb.model.Geo;
import de.avatic.taricdb.model.GeoGroup;
import de.avatic.taricdb.model.Import;
import de.avatic.taricdb.model.Nomenclature;
import de.avatic.taricdb.service.ReferenceStorageService;
import de.avatic.taricdb.service.SetupStatusService;

public class ImportConverter implements ExcelTableConverter<Import> {
	
	Logger log = LoggerFactory.getLogger(ImportConverter.class);
	
	int count = 0;
	private ReferenceStorageService referenceStorage;

	private SetupStatusService statusService;

	private int recordOffset;

	public ImportConverter(ReferenceStorageService referenceStorage, SetupStatusService statusService, int recordOffset) {
		this.referenceStorage = referenceStorage;
		this.statusService = statusService;
		this.recordOffset = recordOffset;
	}

	@Override
	public Collection<Import> convert(ExcelTable table) {
		
		
		
//		Collection<ImportWrapper> imports = new ArrayList<>();
//		
//		Collection<Geo> geos = referenceStorage.getEntites(Geo.class);
//		Collection<GeoGroup> geoGroups = referenceStorage.getEntites(GeoGroup.class);
//		Collection<AppliedMeasureWrapper> appliedMeasures = referenceStorage.getEntites(AppliedMeasureWrapper.class);
//		Collection<AdditionalCode> additionalCodes = referenceStorage.getEntites(AdditionalCode.class);
//		Collection<Nomenclature> nomenclatures = referenceStorage.getEntites(Nomenclature.class);
//		
//		table.getRows().forEach((row) -> {
//			count++;
//			if (row.index() == 0) {
//				return;
//			}
//			
//			
//			if (count % 10000 == 0) {
//				int percentage = Math.round((count*100)/table.getRows().size());
//				log.info("import duties processed: "+percentage+"% - " + count + " of "  + table.getRows().size() + " " + HeapStat.getHeapUtilization(), true, null);
//				statusService.setStatus(recordOffset, "Convert import duties measures ... " + percentage + "%", true , null);
//			}
//			
//			String hsCode = row.getAs("Goods code", String.class);
//			String origin = row.getAs("Origin code", String.class);
//			String addCode = row.getAs("Add code", String.class);
//			
//			ImportWrapper im = imports.stream().filter(i -> i.getHsCode().equals(hsCode) && i.getOrigin().equals(origin) && i.getAddCode().equals(addCode)).findFirst().orElse(null);
//			
//			if(im == null) {
//				Nomenclature nomenclature = nomenclatures.stream().filter(n -> n.getHscode().equals(hsCode)).findFirst().orElse(null);
//				AdditionalCode additionalCode = additionalCodes.stream().filter(a -> a.getAdditionalCode().equals(addCode)).findFirst().orElse(null);
//				Set<AppliedMeasure> foundAppliedMeasures = appliedMeasures.stream().filter(a -> a.getHsCode().equals(hsCode) && a.getOrigin().equals(origin) && a.getAdditionalCode().equals(addCode)).map(AppliedMeasureWrapper::getAppliedMeasure).collect(Collectors.toSet());
//				
//				Geo geo = geos.parallelStream().filter(g -> g.getIso3166Code().equals(origin)).findFirst().orElse(null);
//				GeoGroup geoGroup = geoGroups.parallelStream().filter(g -> g.getGeoGroupCode().equals(origin)).findFirst().orElse(null);
//
//				ImportWrapper imw = new ImportWrapper(new Import(AggregateReference.to(nomenclature.getId()), additionalCode == null ? null : AggregateReference.to(additionalCode.getId()), foundAppliedMeasures, geo == null ? null : AggregateReference.to(geo.getId()), geoGroup == null ? null : AggregateReference.to(geoGroup.getId())), hsCode, origin, addCode);
//				imports.add(imw);
//			}	
//		});	
		
//		return imports.stream().map(ImportWrapper::getImport).collect(Collectors.toSet());
		
		return null;
	}

}
