package com.bayer.edam.migration;

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

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bayer.edam.migration.mam.Entry;
import com.bayer.edam.migration.mam.Entry.Download;
import com.fasterxml.jackson.databind.ObjectMapper;

public class EntryProcessor {

	private static final Logger log = LoggerFactory.getLogger(EntryProcessor.class);

	static final String fieldSeparator = "|~|";

	private static final File entriesFile = new File("C:\\Temp\\Migration AH\\Entry Export\\migration.csv");

	private ObjectMapper jsonMapper = new ObjectMapper();

	public EntryProcessor() throws IOException {
		log.info("Starting up");
		
		LineIterator iter = FileUtils.lineIterator(entriesFile, "UTF-8");

		// skip header
		iter.next();
		
		int count = 0;
		long aggregatedFilesize = 0;
		
		while (iter.hasNext()) {
			String line = iter.next();
			
			String status = line.substring(19, line.indexOf(fieldSeparator, 20));
			if (status.equals("10") || status.equals("7") || status.equals("8")) {
				count++;
				continue;
			}
			
			line = line.substring(line.lastIndexOf(fieldSeparator) + fieldSeparator.length());
			Entry entry = jsonMapper.readValue(line, Entry.class);
			
			List<Download> downloadSelection = new ArrayList<Download>();
			Download selectedDownload = null;
			for (Download download : entry.downloadList) {
				if (download.entryFileVariation.equals("ORIGINAL")) {
					selectedDownload = download;
					break;
				}
				
				if (selectedDownload == null) {
					selectedDownload = download;
				}
				else if (selectedDownload.size < download.size) {
					selectedDownload = download;
				}
			}
			
			if (selectedDownload != null)
				downloadSelection.add(selectedDownload);
			
			for (Download download : entry.downloadListPublicFolder) {
				downloadSelection.add(download);
			}
			
			HashSet<String> hrefs = new HashSet<String>();
			List<Download> cleanedDownloads = new ArrayList<Download>();
			for (Download download : downloadSelection) {
				if (hrefs.add(download.href)) {
					cleanedDownloads.add(download);
				}
			}
			
			HashSet<String> filenames = new HashSet<String>();
			for (Download download : cleanedDownloads) {
				String filenameToUse;
				if (download.fileNameOnDisc != null)
					filenameToUse = download.fileNameOnDisc;
				else
					filenameToUse = download.fileName;
				
				if (!filenames.add(filenameToUse)) {
					log.error(entry.id + ": " + filenameToUse);
					System.exit(1);
				}
				
				aggregatedFilesize += download.size;
			}
			
		}
		
		log.info("Count: " + count);
		log.info("aggregatedFilesize: " + aggregatedFilesize);

	}

	public static void main(String[] args) {
		try {
			new EntryProcessor();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
