package foam.nanos.auth;

import foam.core.FObject;
import foam.core.X;
import foam.dao.ProxyDAO;
import foam.dao.Sink;
import foam.mlang.order.Comparator;
import foam.mlang.predicate.Predicate;

public class AuthenticatedUserDAO
  extends ProxyDAO
{

  @Override
  public FObject put_(X x, FObject fObject) {
    if ( fObject instanceof User ) {
      User user = (User) fObject;

      if ( getDelegate().find(user.getId()) != null ) {
        System.out.println("A user has already been registered with this account");
        return null;
      }

      AuthService service_ = (AuthService) getX().get("auth");
      if ( service_ == null ) {
        System.out.println("Auth Service not started");
        return null;
      }

      try {
        service_.validateUser(user);
        getDelegate().put_(x, fObject);
        return user;
      }
      catch (RuntimeException e) {
        e.printStackTrace();
        return null;
      }
    }
    return null;
  }

  @Override
  public FObject remove_(X x, FObject fObject) {
    return null;
  }

  @Override
  public FObject find_(X x, Object o) {
    if ( o instanceof User ) {
      User user   = (User) o;
      User xUser  = (User) this.getX().get("user");

      if ( xUser == null ) {
        System.out.println("User not logged in");
      }

      if ( user.getId() != xUser.getId() ) {
        System.out.println("You do not have access to this User");
        return null;
      }

      return getDelegate().find(o);
    }

    return null;
  }

  @Override
  public Sink select_(X x, Sink sink, long l, long l1, Comparator comparator, Predicate predicate) {
    return null;
  }
}

@@ -81,21 +81,16 @@
 oam.dao.
-Proxy
 DAO;%0Aimp
@@ -102,20 +102,24 @@
 oam.dao.
-Sink
+ProxyDAO
 ;%0Aimport
@@ -134,24 +134,13 @@
 ang.
-order.Comparator
+MLang
 ;%0Aim
@@ -159,84 +159,265 @@
 ang.
-predicate.Predicate;%0A%0Apublic class AuthenticatedUserDAO%0A  extends ProxyDAO%0A%7B
+sink.Count;%0A%0Aimport java.security.NoSuchAlgorithmException;%0A%0A//TODO: Throw exception for print statements when they are ready%0Apublic class AuthenticatedUserDAO%0A  extends ProxyDAO%0A%7B%0A  public AuthenticatedUserDAO(DAO delegate) %7B%0A    setDelegate(delegate);%0A  %7D
 %0A%0A  
@@ -555,30 +555,16 @@
    if ( 
-getDelegate().
 find(use
@@ -725,17 +725,16 @@
  service
-_
  = (Auth
@@ -742,22 +742,17 @@
 ervice) 
-getX()
+x
 .get(%22au
@@ -775,17 +775,16 @@
  service
-_
  == null
@@ -901,17 +901,16 @@
  service
-_
 .validat
@@ -934,516 +934,579 @@
     
-getDelegate().put_(x, fObject);%0A        return user;%0A      %7D%0A      catch (RuntimeException e) %7B%0A        e.printStackTrace();%0A        return null;%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%0A  @Override%0A  public FObject remove_(X x, FObject fObject) %7B%0A    return null;%0A  %7D%0A%0A  @Override%0A  public FObject find_(X x, Object o) %7B%0A    if ( o instanceof User ) %7B%0A      User user   = (User) o;%0A      User xUser  = (User) this.getX().get(%22
+Count count = (Count) this.limit(1).where(MLang.EQ(User.EMAIL, user.getEmail())).select(new Count());%0A%0A        if ( count.getValue() %3E 0 ) %7B%0A          System.out.println(%22An account is already registered with this email address%22);%0A          return null;%0A        %7D%0A%0A        String salt = UserAndGroupAuthService.generateRandomSalt();%0A        user.setPassword(UserAndGroupAuthService.hashPassword(user.getPassword(), salt) + %22:%22 + salt);%0A        return getDelegate().put_(x, 
 user
-%22
 );%0A
-%0A      if ( xUser == null ) %7B%0A        System.out.println(%22User not logged in%22)
+      %7D%0A      catch (RuntimeException e) %7B%0A        e.printStackTrace();%0A        return null
 ;%0A  
@@ -1515,194 +1515,115 @@
   %7D%0A
-%0A
       
-if ( user.getId() != xUser.getId() ) %7B%0A        System.out.println(%22You do not have access to this User%22);%0A        return null;%0A      %7D%0A%0A      return getDelegate().find(o);
+catch (NoSuchAlgorithmException e) %7B%0A        e.printStackTrace();%0A        return null;%0A      %7D
 %0A    %7D%0A
-%0A
     
@@ -1636,24 +1636,26 @@
  null;%0A  %7D%0A%0A
+//
   @Override%0A
@@ -1646,32 +1646,34 @@
 %0A%0A//  @Override%0A
+//
   public Sink se
@@ -1669,100 +1669,521 @@
 lic 
-Sink select_(X x, Sink sink, long l, long l1, Comparator comparator, Predicate predicate) %7B%0A
+FObject find_(X x, Object o) %7B%0A//    if ( o instanceof User ) %7B%0A//      User user               = (User) o;%0A//      User authenticatedUser  = (User) this.getX().get(%22user%22);%0A//%0A//      if ( authenticatedUser == null ) %7B%0A//        System.out.println(%22Please login%22);%0A//        return null;%0A//      %7D%0A//%0A//      if ( user.getId() != authenticatedUser.getId() ) %7B%0A//        System.out.println(%22You do not have access to this User%22);%0A//        return null;%0A//      %7D%0A//%0A//      return super.find_(x, o);%0A//    %7D%0A//%0A//
     
@@ -2199,10 +2199,11 @@
 ll;%0A
+//
   %7D%0A%7D
-%0A

Put method finished