package com.expee.ml;

import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.expee.ml.utils.Email;
import com.expee.ml.utils.EmailParser;
import com.expee.ml.utils.GetFeature;
import com.expee.ml.utils.OldGetFeature;

public class ExtractEmails {
  private static final String BASE_DIR = "/Users/Ananya/Downloads/enron/maildir";
  private static final String OUTPUT = "EmailData.csv";
  private static final boolean INBOX_ONLY = true; // Set to true to restrict to "inbox" folders
  private static final boolean OLD_FEATURE = false; //Set to true for Bag of Words for email body

  private static void addDirectedEdge(Map<Email, Email> emails, Email parent, Email child) {
    emails.get(parent).addChild(emails.get(child));
  }

  private static void updateChildren(Map<Email, Email> emails, ArrayList<Email> emailChain) {
    for (int i = emailChain.size() - 1; i > 0; i--) {
      addDirectedEdge(emails, emailChain.get(i), emailChain.get(i - 1));
    }
  }
  
  public static Set<Email> extractStructure(List<File> files) throws IOException {
    Map<Email, Email> emails = new HashMap<Email, Email>();
    
    for (File emailFile : files) {
     if (!emailFile.isFile()) continue;
      
      ArrayList<Email> emailChain = EmailParser.parseEmails(emailFile);
      for (Email email : emailChain) {
        if (!emails.containsKey(email)) {
          emails.put(email, email);
        }
      }
      updateChildren(emails, emailChain);
    }

    return emails.keySet();
  }

  public static List<File> getEmailFiles(String dir) throws IOException {
    return getEmailFiles(dir, Integer.MAX_VALUE);
  }

  public static List<File> getLinearEmailFiles(String dir) {
    File base = new File(dir);
    List<File> emailFiles = new ArrayList<File>();

    for (File emailFile : base.listFiles()) {
      if (!emailFile.isFile() || emailFile.isHidden()) continue;
      emailFiles.add(emailFile);
    }

    return emailFiles;
  }

  // TODO(Peijin): This code is buggy. It only goes down 3 levels, instead of recursing.
  public static List<File> getEmailFiles(String dir, int maxUsers) throws IOException {
    List<File> emailFiles = new ArrayList<File>();
    File base = new File(dir);
    int numUsers = 0;

    for (File user : base.listFiles()) {
      if (!user.isDirectory()) continue;
      
      for (File folder : user.listFiles()) {
        if (!folder.isDirectory()) continue;
        if (INBOX_ONLY && !folder.getName().equals("inbox")) continue;
        
        for (File emailFile : folder.listFiles()) {
          if (!emailFile.isFile() || emailFile.isHidden()) continue;

          emailFiles.add(emailFile);
        }
      }

      System.out.println(user.getName() + " " + emailFiles.size());
      if (++numUsers >= maxUsers) break;
    }

    return emailFiles;
  }

  public static void main(String[] args) throws Exception {
    System.out.println("Getting email list");
    // List<File> emailFiles = getLinearEmailFiles("../testsets/simple");
    List<File> emailFiles = getEmailFiles(BASE_DIR, 10);
    System.out.println("Extracting email structure");
    Set<Email> emails = extractStructure(emailFiles);
    System.out.println("Making features");
    if (OLD_FEATURE) {
      OldGetFeature.makeEmailSetFeatures(emails, OUTPUT);
    }
    else {
      GetFeature.makeEmailSetFeatures(emails, OUTPUT);
    }
  }
}

@@ -2056,18 +2056,267 @@
 s;%0A  %7D%0A%0A
-  
+public static void getEmailRecursive(File emailFile, ArrayList%3CFile%3E emailFiles)%7B%0A%09if (!emailFile.isDirectory())%7B%0A%09%09emailFiles.add(emailFile);%0A%09%7D else %7B%0A%09%09for (File subFile : emailFile.listFiles())%7B%0A%09%09%09getEmailRecursive(subFile,emailFiles);%0A%09%09%7D%0A%09%7D%0A%7D%0A%0A
 // TODO(
@@ -2758,32 +2758,41 @@
 ry()) continue;%0A
+        %0A
         if (INBO
@@ -2913,33 +2913,32 @@
 %7B%0A          if (
-!
 emailFile.isFile
@@ -2937,50 +2937,149 @@
 e.is
-File() %7C%7C emailFile.isHidden()) continue;%0A
+Hidden()) continue;%0A          %0A          if (emailFile.isDirectory())%7B%0A        %09  getEmailRecursive(emailFile,emailFiles);%0A          %7D else %7B
 %0A   
@@ -3079,24 +3079,25 @@
 e %7B%0A        
+%09
   emailFiles
@@ -3113,33 +3113,54 @@
 lFile);%0A        
-%7D
+  %7D%0A        %7D%0A        
 %0A      %7D%0A%0A      

Update ExtractEmails.java