package com.bayer.edam.excelimport;

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

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;

public class FilenameComparator {
	private static Logger log = Logger.getLogger(FilenameComparator.class);
	private static String logfilePath = "C:\\Temp\\Migration CS - PhotoGallery\\FilenameComparator.log";
	
	private static String migrationFilePath = "C:\\Temp\\Migration CS - PhotoGallery\\Frederic - Toolbox Migration 2 DB export.csv";
	private static String importFilePath = "C:\\Temp\\Migration CS - PhotoGallery\\all imported IDs - toolbox export.csv";
	private static String outputFilePath = "C:\\Temp\\Migration CS - PhotoGallery\\all imported IDs - toolbox export - without Migration 2.csv";

	private CsvParserSettings parserSettings = new CsvParserSettings();
	private List<String[]> migrationRows;
	private HashSet<String> migration2filenames = new HashSet<String>();
	private List<String[]> importRows;
	private List<Object[]> csvOutputList = new ArrayList<Object[]>();

	public FilenameComparator() throws IOException {
		parserSettings.getFormat().setLineSeparator("\r\n");
		parserSettings.getFormat().setDelimiter(';');
		parserSettings.getFormat().setCharToEscapeQuoteEscaping('"');
		parserSettings.setHeaderExtractionEnabled(false);
		CsvParser parser = new CsvParser(parserSettings);
		migrationRows = parser.parseAll(new File(migrationFilePath));
		
		for (String[] line : migrationRows) {
			migration2filenames.add(line[1]);
		}
		
		importRows = parser.parseAll(new File(importFilePath));
		
		int lineNo = 0;
		for (String[] importLine : importRows) {
			lineNo++;
			
			if (migration2filenames.contains(importLine[1])) {
				log.info("Line " + lineNo + " - Removing file " + importLine[1]);
				continue;
			}
			
			String[] csvLine = new String[importLine.length];
			for (int i = 0; i < importLine.length; i++)
				csvLine[i] = importLine[i];
			
			csvOutputList.add(csvLine);
		}
		
		CsvWriterSettings writerSettings = new CsvWriterSettings();
		writerSettings.setFormat(parserSettings.getFormat());

		CsvWriter writer = new CsvWriter(new File(outputFilePath), writerSettings);
		writer.writeRowsAndClose(csvOutputList);
		
	}

	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 FilenameComparator();

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

}
