package com.sudoku.data.manager;

/**
 *
 * @author clesaege
 */

import com.sudoku.data.model.User;
import org.springframework.security.crypto.codec.Base64;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public final class UserManager { // This is the manager for users.
  /*
  * Attributs
  */
  private static volatile UserManager instance = null;
  private User loggedUser; // The user actualy logged on this machine
  private List<User> localUsers; // Local Users
  private List<User> distantUsers; // Users connected but not on this machine

  private UserManager() {
    this.loggedUser = null;
    this.localUsers = new ArrayList<>(); // Will need to load and deserialise
    this.distantUsers = new ArrayList<>(); //
  }

  public final static UserManager getInstance() {
    if (instance == null) {
      synchronized (UserManager.class) {
        if (instance == null) {
          instance = new UserManager();
        }
      }
    }
    return instance;
  }

  public void save() { // Serialize and save localUsers, we do not keep track of distantUsers

    // TO BE COMPLETED

  }

  public User getLoggedUser() {
    return this.loggedUser;
  }

  public List<User> getLocalUsers() {
    return this.localUsers;
  }

  public List<User> getDistantUsers() {
    return this.distantUsers;
  }

  public boolean addLocalUser(User u) {
    return this.localUsers.add(u);
  }

  public boolean addDistantUser(User u) {
    return this.distantUsers.add(u);
  }


  public boolean removeLocalUser(User u) {
    return localUsers.remove(u);
  }

  public boolean removeDistantUser(User u) {
    return distantUsers.remove(u);
  }

  public User authenticate(String pseudo, String password)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
    for (User u : this.localUsers) { //For all users
      if (u.getPseudo().equals(pseudo)) //For all correspoding pseudos
      {
        String toBeHashed = password + u.getSalt();
        if (new String(Base64.encode(
            mDigest.digest(toBeHashed.getBytes("UTF-8"))))
            .equals(u.getPassword())) { //If the hash of pwd+salt is good
          loggedUser = u; // If the password is correct, log the user
          return u; // and return the identified user
        }
      }
    }
    return null; // If we didn't manage to authenticate
  }

  public void logoff() {
    this.loggedUser = null;
  }


  public User createUser() {
    return null;
  }

  public List<User> getConnectedUsers() {
    return null; // Appeler com
  }

}

       

@@ -854,24 +854,152 @@
 (); //%0A  %7D%0A%0A
+  public void save()%7B // Serialize and save localUsers, we do not keep track of distantUsers%0A      // TO BE COMPLETED   %0A  %7D%0A  %0A
   public fin
@@ -1231,132 +1231,8 @@
  %7D%0A%0A
-  public void save() %7B // Serialize and save localUsers, we do not keep track of distantUsers%0A%0A    // TO BE COMPLETED%0A%0A  %7D%0A%0A
   pu
@@ -1321,32 +1321,33 @@
 etLocalUsers() %7B
+ 
 %0A    return this
@@ -1355,33 +1355,32 @@
 localUsers;%0A  %7D%0A
-%0A
   public List%3CUs
@@ -1394,32 +1394,33 @@
 DistantUsers() %7B
+ 
 %0A    return this
@@ -1434,24 +1434,26 @@
 tUsers;%0A  %7D%0A
+  
 %0A  public bo
@@ -1512,33 +1512,32 @@
 ers.add(u);%0A  %7D%0A
-%0A
   public boolean
@@ -1599,24 +1599,26 @@
 add(u);%0A  %7D%0A
+  
 %0A%0A  public b
@@ -1679,33 +1679,32 @@
 .remove(u);%0A  %7D%0A
-%0A
   public boolean
@@ -2201,16 +2201,18 @@
         
+  
 mDigest.
@@ -2528,24 +2528,26 @@
 nticate%0A  %7D%0A
+  
 %0A  public vo
@@ -2557,23 +2557,27 @@
 logoff()
+%0A 
  %7B%0A    
+  
 this.log
@@ -2575,35 +2575,33 @@
  this.loggedUser
- = 
+=
 null;%0A  %7D%0A%0A%0A  pu
@@ -2594,16 +2594,18 @@
 ll;%0A  %7D%0A
+  
 %0A%0A  publ
@@ -2732,15 +2732,325 @@
  %7D%0A%0A
-%7D%0A%0A       
+  %0A  public boolean exportLoggedUserToFile(String path) %7B%0A        // TO BE COMPLETED%0A        // Serialize this.loggedUser and write in a file.%0A    return true;%0A%7D%0A  %0A  public User importUser(String path) %7B%0A        // TO BE COMPLETED%0A        // Derialize from a file and add to local users%0A    return null;%0A%7D  %0A%0A       %0A%7D%0A
 %0A

Add TO BE COMPLETED functions