package edu.pdx.cs410J.grader;

import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import java.io.PrintStream;
import java.util.Properties;

public class GraderEmailAccount {
  private final String password;

  public GraderEmailAccount(String password) {
    this.password = password;
  }


  public void fetchProjectSubmissions() {
    Store store = connectToGmail();
    Folder folder = openProjectSubmissionsFolder(store);
    printFolderInformation(folder);


    try {
      folder.close(false);
      store.close();

    } catch (MessagingException ex) {
      throw new IllegalStateException("While closing folder and store", ex);
    }
  }

  private void printFolderInformation(Folder folder) {
    try {
      PrintStream out = System.out;
      out.println("Folder: " + folder.getFullName());
      out.println("Message count: " + folder.getMessageCount());
      out.println("Unread messages: " + folder.getUnreadMessageCount());

    } catch (MessagingException ex) {
      throw new IllegalStateException("While getting folder information", ex);
    }
  }

  private Folder openProjectSubmissionsFolder(Store store) {
    try {
      Folder folder = store.getDefaultFolder();
      checkForValidFolder(folder, "Default");
      folder = folder.getFolder("INBOX");
      folder.open(Folder.READ_ONLY);
      return folder;

    } catch (MessagingException ex) {
      throw new IllegalStateException("While opening project submissions folder", ex);
    }
  }

  private void checkForValidFolder(Folder folder, String folderName) {
    if (folder == null) {
      throw new IllegalStateException("Folder \"" + folderName + "\" does not exist");
    }

  }

  private Store connectToGmail() {
    Properties props = new Properties();
    Session session = Session.getInstance(props, null);
    try {
      Store store = session.getStore("imaps");
      store.connect("imap.gmail.com", "sjavata", this.password);
      return store;

    } catch (MessagingException ex) {
      throw new IllegalStateException("While connecting to Gmail", ex);
    }
  }
}

@@ -47,104 +47,9 @@
 ail.
-Folder;%0Aimport javax.mail.MessagingException;%0Aimport javax.mail.Session;%0Aimport javax.mail.Store
+*
 ;%0Aim
@@ -426,16 +426,49 @@
 older);%0A
+    printUnreadMessages(folder);%0A
 %0A%0A    tr
@@ -638,32 +638,1284 @@
 ex);%0A    %7D%0A  %7D%0A%0A
+  private void printUnreadMessages(Folder folder) %7B%0A    try %7B%0A      int unreadMessageCount = folder.getUnreadMessageCount();%0A      Message%5B%5D messages = folder.getMessages();%0A%0A      FetchProfile profile = new FetchProfile();%0A      profile.add(FetchProfile.Item.ENVELOPE);%0A      profile.add(FetchProfile.Item.FLAGS);%0A%0A      folder.fetch(messages, profile);%0A%0A      for (Message message : messages) %7B%0A        printMessageInformation(message);%0A      %7D%0A%0A    %7D catch (MessagingException ex) %7B%0A      throw new IllegalStateException(%22While printing unread messages%22, ex);%0A    %7D%0A  %7D%0A%0A  private void printMessageInformation(Message message) throws MessagingException %7B%0A    PrintStream out = System.out;%0A    out.println(%22Message%22);%0A    out.println(%22  To: %22 + addresses(message.getRecipients(Message.RecipientType.TO)));%0A    out.println(%22  From: %22 + addresses(message.getFrom()));%0A    out.println(%22  Subject: %22 + message.getSubject());%0A%0A  %7D%0A%0A  private StringBuilder addresses(Address%5B%5D addresses) %7B%0A    StringBuilder sb = new StringBuilder();%0A    for (int i = 0; i %3C addresses.length; i++) %7B%0A      Address address = addresses%5Bi%5D;%0A      sb.append(address.toString());%0A      if (i %3E addresses.length - 1) %7B%0A        sb.append(%22, %22);%0A      %7D%0A    %7D%0A%0A    return sb;%0A  %7D%0A%0A
   private void p
@@ -2186,24 +2186,91 @@
 ageCount());
+%0A      out.println(%22New messages: %22 + folder.getNewMessageCount());
 %0A%0A    %7D catc
@@ -2628,12 +2628,13 @@
 EAD_
-ONLY
+WRITE
 );%0A 

Print some information about messages.