package com.ZombieRun.persistence;

import com.ZombieRun.persistence.dao.implementations.UserDAOImpl;
import com.ZombieRun.persistence.data.User;

public class UserUtils {

	public static int createUser(String username, String password, String email, boolean premium) {
		try {
			UserDAOImpl userDAO = new UserDAOImpl();
			User user = new User(username, password, email, premium);
			int idUser = userDAO.create(user);
			// Main.logger.info("SignUp successful !");
			System.out.println("SignUp successful !");
			return idUser;
		} catch (Exception e) {
			e.printStackTrace();
		}
		// Main.logger.info("SignUp unsuccessful !");
		System.out.println("SignUp unsuccessful !");
		return -1;
	}

	public static boolean logIn(String username, String password) {
		try {
			UserDAOImpl userDAO = new UserDAOImpl();
			User user = userDAO.getUser(username);
			if (user.getPassword().equals(password)) {
				user.setLastLoginRetry(0);
				user.setLastLoginTime(SqlUtils.getTimestamp());
				userDAO.update(user);
				// Main.logger.info("LogIn successful !");
				System.out.println("LogIn successful !");
				return true;
			}
			user.setLastLoginRetry(user.getLastLoginRetry() + 1);
			userDAO.update(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// Main.logger.info("LogIn unsuccessful !");
		System.out.println("LogIn unsuccessful !");
		return false;
	}
}
