package com.bayer.edam.excelimport;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
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;

/**
 * Reads the file names of the release files from an Toolbox SQL export and copies the actual files another directory. This is used to sort
 * out which release files are actually in use and need to be uploaded to eDAM.
 * 
 * @author gexce
 *
 */
public class CropReleaseCopyTool {
	
	private boolean dryRun = false;
	
	private static Logger log = Logger.getLogger(CropReleaseCopyTool.class);
	private static String logfilePath = "C:\\Temp\\Migration CS - SQL Server\\export_2017-08-23_incl_Releases.releasecopytool.log";
	
	private static String toolboxExportFilePath = "C:\\Temp\\Migration CS - SQL Server\\export-2017-08-15-update_incl_Releases.csv";
	
	private static String srcDir = "D:\\Toolbox\\ToolboxReleases\\";
	private static String targetDir = "D:\\ToolboxReleases\\";
	
	private CsvParserSettings parserSettings = new CsvParserSettings();
	private List<String[]> inputRows;
	
	HashSet<Integer> copiedReleases = new HashSet<Integer>();

	
	
	public CropReleaseCopyTool() {
		readToolboxExport();
		
		int lineNo = 0;
		
		for (String[] line : inputRows) {
			lineNo++;
			if (!line[28].equals("NULL")) {
				int releaseId = Integer.parseInt(line[28]);
				
				if (!copiedReleases.contains(releaseId)) {
					copiedReleases.add(releaseId);
					log.info(line[0] + " '" + line[1] + "' --> " + line[28] + " '" + line[29] + "'");
					
					if (!dryRun) {
						File srcFile = new File(srcDir + line[34]);
						File targetFile = new File(targetDir + line[34]);
						
						try {
							FileUtils.copyFile(srcFile, targetFile);
						}
						catch (IOException ioe) {
							log.error("Line " + lineNo + ", AssetID " + line[0] + ", ReleaseID " + line[28] + " - IOException: '" + ioe.getMessage() + "'");
							ioe.printStackTrace();
						}
					}

				}
			}
		}
		
		log.info("noReleases: " + copiedReleases.size());
	}
	
	private void readToolboxExport() {
		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));
	}

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

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

}
