/*
 * Copyright (c) 2014 Personal-Health-Monitoring-System
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cse3310.phms.model;

import com.cse3310.phms.model.utils.BCrypt;
import com.cse3310.phms.ui.utils.UserSingleton;
import com.cse3310.phms.ui.utils.DatabaseHandler;

public class LoginManager {

    private LoginManager() {}

    public static boolean register(String userName, String password, PersonalInfo personalInfo) {
        if (!isUserNameExists(userName)) {
            String hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());

            // create new user
            personalInfo.save();
            User newUser = new User(userName, hashedPassword);
            newUser.setPersonalInfo(personalInfo);
            newUser.save(); // save user to database

            // set current user to the new user
            UserSingleton.getInstance().setCurrentUser(newUser);
            return true;
        }

        return false;
    }

    public static boolean login(String userName, String password) {
        User user = DatabaseHandler.getUserByUserName(userName);

        // check if username is valid
        if (user == null) { return false; }

        // check if password is valid
        if (BCrypt.checkpw(password, user.getHashedPassword())) {
            // Valid password! Set current user to the logging in user.
            UserSingleton.getInstance().setCurrentUser(user);
            return true;
        }

        return false;
    }

    public static void logOut() {
        UserSingleton.getInstance().setCurrentUser(null);
    }

    public static boolean isUserNameExists(String userName) {
        User user = DatabaseHandler.getUserByUserName(userName);
        return user != null;
    }
}

@@ -788,16 +788,17 @@
 ndler;%0A%0A
+%0A
 public c
@@ -826,34 +826,506 @@
     
-private LoginManager() %7B%7D%0A
+// restrict instantiation of this class.%0A    private LoginManager() %7B%7D%0A%0A    /**%0A     * Register a new user and set the new user as the current user.%0A     *%0A     * @param userName the user name for the registering account%0A     * @param password the password for the registering account%0A     * @param personalInfo the personal info to associate with the account%0A     * @return true if registration is successful, else return false if failed%0A     * @see com.cse3310.phms.ui.utils.UserSingleton%0A     */
 %0A   
@@ -1944,32 +1944,346 @@
 n false;%0A    %7D%0A%0A
+    /**%0A     * Log in and set the the logging in user as the current user.%0A     *%0A     * @param userName the user name used to log in%0A     * @param password the password used to log in%0A     * @return true if log in is successful, otherwise, return false%0A     * @see com.cse3310.phms.ui.utils.UserSingleton%0A     */%0A
     public stati
@@ -2794,102 +2794,420 @@
     
-public static void logOut() %7B%0A        UserSingleton.getInstance().setCurrentUser(null);%0A    %7D%0A
+/**%0A     * Log out and set the current user to null.%0A     * @see com.cse3310.phms.ui.utils.UserSingleton%0A     */%0A    public static void logOut() %7B%0A        UserSingleton.getInstance().setCurrentUser(null);%0A    %7D%0A%0A    /**%0A     * check if the user name exists in the database.%0A     *%0A     * @param userName the user name to check%0A     * @return true if the username exits in the db, otherwise, return false.%0A     */
 %0A   

add javadoc to LoginManager