package com.bayer.edam.excelimport;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;

import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import com.univocity.parsers.csv.CsvWriter;
import com.univocity.parsers.csv.CsvWriterSettings;

/**
 * Removes all files from the import Excel spreadsheet that are not shown in a directory listing.
 * 
 * @author gezvf
 *
 */
public class ImportFileCleaner {
	
	private static Logger log = Logger.getLogger(ImportFileCleaner.class);
	
	private static String toolboxExportFilePath = "C:\\Temp\\Migration CS - SQL Server\\export-2017-08-15-update.csv";
	private static String listingFilePath = "D:\\largeListing.txt";
	private static String outputFilePath = "C:\\Temp\\Migration CS - SQL Server\\export-2017-08-15-update-largeSet_2.csv";
	private static String logfilePath = "C:\\Temp\\Migration CS - SQL Server\\export-2017-08-15-update-largeSet_clean_2.log";

	private CsvParserSettings parserSettings = new CsvParserSettings();
	private List<String[]> inputRows;
	private List<String> filenames;
	private List<String[]> linesToKeep = new ArrayList<String[]>();
	private List<String[]> linesToRemove = new ArrayList<String[]>();
	
	public ImportFileCleaner() throws IOException {
		parserSettings.getFormat().setLineSeparator("\r\n");
		parserSettings.getFormat().setDelimiter(';');
		parserSettings.getFormat().setCharToEscapeQuoteEscaping('"');
		parserSettings.setHeaderExtractionEnabled(false);
		CsvParser parser = new CsvParser(parserSettings);
		inputRows = parser.parseAll(new File(toolboxExportFilePath));
		
		filenames = FileUtils.readLines(new File(listingFilePath), "UTF-8");
		
		for (String[] line : inputRows) {
			if (filenames.contains(line[1]))
				linesToKeep.add(line);
			else {
				linesToRemove.add(line);
				log.info("Asset ID " + line[0] + " - File '" + line[1] + "' not found, removing from import");
			}
		}
		
		List<Object[]> csvOutputList = new ArrayList<Object[]>();
		for (String[] line : linesToKeep) {
			csvOutputList.add(line);
		}
		
		CsvWriterSettings writerSettings = new CsvWriterSettings();
		writerSettings.setFormat(parserSettings.getFormat());

		CsvWriter writer = new CsvWriter(new File(outputFilePath), writerSettings);
		writer.writeRowsAndClose(csvOutputList);
		
		log.info("Number of input lines: " + inputRows.size());
		log.info("Number of output lines: " + linesToKeep.size());
		
	}
	
	public static void main(String[] args) {
		try {
			Layout layout = new PatternLayout("%d{ISO8601} - %p %m%n"); // "%d{ISO8601} - %p %m%n"
			BasicConfigurator.configure(new ConsoleAppender(layout));
			BasicConfigurator.configure(new RollingFileAppender(layout, logfilePath, false));
			log.setLevel(Level.INFO);

			new ImportFileCleaner();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
