package edu.tamu.tcat.account.apacheds.test.application;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

import edu.tamu.tcat.account.apacheds.LdapHelperAdFactory;
import edu.tamu.tcat.account.apacheds.LdapHelperReader;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication
{

   /*
    * (non-Javadoc)
    * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
    */
   public Object start(IApplicationContext context) throws Exception
   {
      List<String> args = Arrays.asList(Platform.getCommandLineArgs());
      if (args.contains("-h"))
      {
         printHelp();
      }
      else if (!args.contains("-f"))
      {
         System.out.println("ldap configuration file must be specified");
         printHelp();
      }
      else if (!args.contains("-u"))
      {
         System.out.println("user distinguished name must be specified");
         printHelp();
      }
      else
      {
         int index = args.indexOf("-f");
         LdapHelperReader helper = createLdap(args.get(index + 1));
         index = args.indexOf("-u");
         String user = args.get(index + 1);
         if (args.contains("testUser"))
         {
            helper.checkValidUser(user);
            System.out.println("User [" + user + "] is a valid user");
         }
         else if (args.contains("testPassword"))
         {
            index = args.indexOf("-p");
            helper.checkValidPassword(user, args.get(index + 1));
            System.out.println("User [" + user + "] has a valid credentials");
         }
         else if (args.contains("groups"))
         {
            for (String g : helper.getGroupNames(user))
               System.out.println("User [" + user + "] is in group [" + g + "]");
         }
         else if (args.contains("attribute"))
         {
            index = args.indexOf("-a");
            String attr = args.get(index + 1);
            for (Object a : helper.getAttributes(user, attr))
               System.out.println("User [" + user + "] is has attribute ["+attr+"] value [" + a + "]");
         }
         else if (args.contains("matches"))
         {
            index = args.indexOf("-a");
            String att = args.get(index + 1);
            index = args.indexOf("-v");
            String val = args.get(index + 1);
            for (Object a : helper.getMatches(null, att, val))
               System.out.println("User [" + a + "] has attribute ["+att+"] value [" + val + "]");
         }
      }
      return IApplication.EXIT_OK;
   }

   private LdapHelperReader createLdap(String string) throws IOException
   {
      try (InputStream is = new FileInputStream(string))
      {
         Properties p = new Properties();
         p.load(is);
         return new LdapHelperAdFactory().buildReader(p);
      }
   }

   /*
    * (non-Javadoc)
    * @see org.eclipse.equinox.app.IApplication#stop()
    */
   public void stop()
   {
      // nothing to do
   }

   private void printHelp()
   {
      System.out.println("Options are:");
      System.out.println("\t-f <configurationFile>");
      System.out.println("\t-u <user distinguished name>");
      System.out.println("\t<testUser {does this user exist} | ");
      System.out.println("\t\ttestPassword {does this user password combination validate} | ");
      System.out.println("\t\tgroups {get the groups of user} | ");
      System.out.println("\t\tmatches {get the users matching an attribute value pair} | ");
      System.out.println("\t\tattribute {get the specified attributes for this user} >");
      System.out.println("\t[-p <user password>]");
      System.out.println("\t[-a <attribute id>]");
      System.out.println("\t[-v <value>]");

      //TODO add mutate

   }
}

@@ -1087,32 +1087,514 @@
 Help();%0A      %7D%0A
+      else if (args.contains(%22matches%22))%0A      %7B%0A         int index = args.indexOf(%22-f%22);%0A         LdapHelperReader helper = createLdap(args.get(index + 1));%0A         index = args.indexOf(%22-a%22);%0A         String att = args.get(index + 1);%0A         index = args.indexOf(%22-v%22);%0A         String val = args.get(index + 1);%0A         for (Object a : helper.getMatches(null, att, val))%0A            System.out.println(%22User %5B%22 + a + %22%5D has attribute %5B%22+att+%22%5D value %5B%22 + val + %22%5D%22);%0A      %7D%0A
       else if (!
@@ -2915,38 +2915,37 @@
 args.contains(%22m
-atches
+ember
 %22))%0A         %7B%0A 
@@ -2970,33 +2970,33 @@
  args.indexOf(%22-
-a
+g
 %22);%0A            
@@ -3006,187 +3006,37 @@
 ing 
-att = args.get(index + 1);%0A            index = args.indexOf(%22-v%22);%0A            String val = args.get(index + 1);%0A            for (Object a : helper.getMatches(null, att, val))%0A   
+group = args.get(index + 1);%0A
     
@@ -3077,56 +3077,99 @@
 %22 + 
-a
+user
  + %22%5D 
-has attribute %5B%22+att+%22%5D value %5B%22 + val + 
+is %22+ (helper.isMemberOf(group, user) ? %22%22 : %22not%22)+%22 a member of group %5B%22+group+
 %22%5D%22)
@@ -4251,24 +4251,107 @@
  user%7D %3E%22);%0A
+      System.out.println(%22%5Ct%5Ctmember %7Btests membership in a group for a user%7D %3E%22);%0A
       System
@@ -4484,16 +4484,79 @@
 lue%3E%5D%22);
+%0A      System.out.println(%22%5Ct%5B-g %3Cgroup distinguished name%3E%5D%22);
 %0A%0A      

add member of test to test application