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.helper.workbook.wrapper.DeclarableCodesWrapper;
import de.avatic.taricdb.service.ReferenceStorageService;

public class DeclarableCodesConverter implements ExcelTableConverter<DeclarableCodesWrapper> {

	public DeclarableCodesConverter(ReferenceStorageService referenceStorageService) {

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

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

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

			String hsCode = goodsCode.substring(0, 10);
			String suffix = goodsCode.substring(11, 13);
			Boolean isLeaf = row.getAs("IS_LEAF", Boolean.class);
			LocalDate declStartDate = row.getAs("Decl. start date", LocalDate.class);
			
			declarableGoods.add(new DeclarableCodesWrapper(hsCode, suffix, declStartDate, isLeaf));
			
		});
		
		return declarableGoods;
	}

}
