<commit_before>package service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import ttable.Progeny;
import json.JSONFailureException;
import json.Json;
/**
 * 
 * @author <NAME>
 * @author <NAME>
 * @version 1.1
 */
public class UserService {
	
	/**
	 * Sets the answer to the user's password recovery question.
	 *
	 * @param answer the answer
	 * @param password the password
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean setAnswer(String answer, String password) throws JSONFailureException
	{
		Json json = new Json();
		json.sendRequest("https://jbaron6.cs2212.ca/setanswer?answer=" + answer + "&password=" + password);
		
		return true;
	}

	/**
	 * Posts a message to Facebook to indicate a child's score
	 *
	 * @param name	the child's name
	 * @param score	the score
	 * @param level	the level
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean postMessage(String name, int score, int level) throws JSONFailureException
	{
		Json json = new Json();
		String message = (name + " just reached a score of " + score + " on level " + level + "!");
		json.sendRequest("https://jbaron6.cs2212.ca/postmessage?message=" + message);
		
		return true;
	}
	
	/**
	 * Sets the user's password.
	 *
	 * @param oldPassword the old password
	 * @param newpassword the new password
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean setPassword(String oldPassword, String newpassword) throws JSONFailureException
	{
		Json json = new Json();
		
		json.sendRequest("https://jbaron6.cs2212.ca/setpassword?new_password=" + newpassword + "&old_password=" + oldPassword);
		
		return true;
	}
	
	/**
	 * Sets the user's password using the security question instead of old password.  If the
	 *  security question is input incorrectly, the password is automatically reset to the
	 *  default, cs2212.
	 *  
	 * @param questionAns			the input answer to the security question.
	 * @param newpassword			the new password to be changed to.
	 * @return						true if successful, false otherwise.
	 * @throws JSONFailureException	the JSON exception thrown if the password change is unsuccessful.
	 */
	public static boolean setPasswordWithQ(String questionAns, String newpassword) throws JSONFailureException {
		Json json = new Json();
		
		json.sendRequest("https://jbaron6.cs2212.ca/resetpassword?answer=" + questionAns + "&new_password=" + newpassword);
		
		return true;
	}
	
	/**
	 * Sets the user's password recovery question.
	 *
	 * @param question the question
	 * @param password the password
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean setQuestion(String questionNumber, String password) throws JSONFailureException
	{
		Json json = new Json();
		json.sendRequest("https://jbaron6.cs2212.ca/setquestion?question=" + questionNumber + "&password=" + password);		
		
		return true;
	}
	
	/**
	 * Sets the number of mistakes allowed for a specified level.
	 *
	 * @param mistakesAllowed the new mistakes allowed
	 */
	public static void setMistakesAllowed(int level, int mistakesAllowed) throws JSONFailureException {
		//TODO:  server call to update
	}
	
	/**
	 * Gets the current user's password recovery questions.
	 *
	 * @return the questions		the array of security questions
	 * @throws JSONFailureException the JSON failure exception
	 */	
	public static ArrayList<String> getSecurityQuestions() throws JSONFailureException
	{
		ArrayList<String> questions = new ArrayList<String>();
		
		Json json = new Json();
		JSONObject jsonObj = json.sendRequest("https://jbaron6.cs2212.ca/getquestions");
		
		
		JSONArray questionsArray = (JSONArray) jsonObj.get("questions");
		
		Iterator<?> questionsIt = questionsArray.iterator();
		
		while(questionsIt.hasNext())
			questions.add((String)questionsIt.next());
		
		return questions;
	}
	
	/**
	 * Authenticate the user.
	 *
	 * @param password the password
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean authenticate(String password) throws JSONFailureException
	{
		Json json = new Json();
		
		//Will throw error if success fail.  That error will have messages.
		json.sendRequest("https://jbaron6.cs2212.ca/authenticate?password=" + password);
		
		return true;
	}
	
	/**
	 * Gets the users friends list
	 *
	 * @return 						an array of friends
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static ArrayList<String> getFriends() throws JSONFailureException 
	{
		ArrayList<String> friendList;

		friendList = new ArrayList<String>();
		
		// TODO: make server call and parse list
		
		return friendList;
	}
	
	
	
	/**
	 * Reset the user's password.
	 *
	 * @param oldPassword			the <PASSWORD>
	 * @param newPassword			the new password
	 * @return 						true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean resetPassword(String oldPassword, String newPassword) throws JSONFailureException
	{
		Json json = new Json();
		json.sendRequest("https://jbaron6.cs2212.ca/resetpassword?answer=" + oldPassword + "&new_password=" + newPassword);
		
		return true;
	}
	
	/**
	 * Gets a friend's progeny.
	 *
	 * @return 						an array of progeny
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static ArrayList<Progeny> getFriendProgeny() throws JSONFailureException 
	{
		ArrayList<Progeny> progenyList;

		progenyList = new ArrayList<Progeny>();
		
		// TODO: make server call and parse list
		
		return progenyList;
	}

	// ** NOTE ** //
		/**
		 * This is a dummy method that emulates how getFriends will work I'm
		 * including it in lieu of just creating a string array in the StatsMenu2
		 * file.
		 * 
		 * @return an array of friends
		 * @throws JSONFailureException<commit_msg>added getQuestionIndex<commit_after>package service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import ttable.Progeny;
import json.JSONFailureException;
import json.Json;
/**
 * 
 * @author <NAME>
 * @author <NAME>
 * @version 1.1
 */
public class UserService {
	
	/**
	 * Sets the answer to the user's password recovery question.
	 *
	 * @param answer the answer
	 * @param password the password
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean setAnswer(String answer, String password) throws JSONFailureException
	{
		Json json = new Json();
		json.sendRequest("https://jbaron6.cs2212.ca/setanswer?answer=" + answer + "&password=" + password);
		
		return true;
	}

	/**
	 * Posts a message to Facebook to indicate a child's score
	 *
	 * @param name	the child's name
	 * @param score	the score
	 * @param level	the level
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean postMessage(String name, int score, int level) throws JSONFailureException
	{
		Json json = new Json();
		String message = (name + " just reached a score of " + score + " on level " + level + "!");
		json.sendRequest("https://jbaron6.cs2212.ca/postmessage?message=" + message);
		
		return true;
	}
	
	/**
	 * Sets the user's password.
	 *
	 * @param oldPassword the old password
	 * @param newpassword the new password
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean setPassword(String oldPassword, String newpassword) throws JSONFailureException
	{
		Json json = new Json();
		
		json.sendRequest("https://jbaron6.cs2212.ca/setpassword?new_password=" + newpassword + "&old_password=" + oldPassword);
		
		return true;
	}
	
	/**
	 * Sets the user's password using the security question instead of old password.  If the
	 *  security question is input incorrectly, the password is automatically reset to the
	 *  default, cs2212.
	 *  
	 * @param questionAns			the input answer to the security question.
	 * @param newpassword			the new password to be changed to.
	 * @return						true if successful, false otherwise.
	 * @throws JSONFailureException	the JSON exception thrown if the password change is unsuccessful.
	 */
	public static boolean setPasswordWithQ(String questionAns, String newpassword) throws JSONFailureException {
		Json json = new Json();
		
		json.sendRequest("https://jbaron6.cs2212.ca/resetpassword?answer=" + questionAns + "&new_password=" + newpassword);
		
		return true;
	}
	
	/**
	 * Sets the user's password recovery question.
	 *
	 * @param question the question
	 * @param password the password
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean setQuestion(String questionNumber, String password) throws JSONFailureException
	{
		Json json = new Json();
		json.sendRequest("https://jbaron6.cs2212.ca/setquestion?question=" + questionNumber + "&password=" + password);		
		
		return true;
	}
	
	/**
	 * Sets the number of mistakes allowed for a specified level.
	 *
	 * @param mistakesAllowed the new mistakes allowed
	 */
	public static void setMistakesAllowed(int level, int mistakesAllowed) throws JSONFailureException {
		//TODO:  server call to update
	}
	
	/**
	 * Gets the current user's password recovery questions.
	 *
	 * @return the questions		the array of security questions
	 * @throws JSONFailureException the JSON failure exception
	 */	
	public static ArrayList<String> getSecurityQuestions() throws JSONFailureException
	{
		ArrayList<String> questions = new ArrayList<String>();
		
		Json json = new Json();
		JSONObject jsonObj = json.sendRequest("https://jbaron6.cs2212.ca/getquestions");
		
		
		JSONArray questionsArray = (JSONArray) jsonObj.get("questions");
		
		Iterator<?> questionsIt = questionsArray.iterator();
		
		while(questionsIt.hasNext())
			questions.add((String)questionsIt.next());
		
		return questions;
	}
	
	/**
	 * Authenticate the user.
	 *
	 * @param password the password
	 * @return true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean authenticate(String password) throws JSONFailureException
	{
		Json json = new Json();
		
		//Will throw error if success fail.  That error will have messages.
		json.sendRequest("https://jbaron6.cs2212.ca/authenticate?password=" + password);
		
		return true;
	}
	
	/**
	 * Gets the users friends list
	 *
	 * @return 						an array of friends
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static ArrayList<String> getFriends() throws JSONFailureException 
	{
		ArrayList<String> friendList;

		friendList = new ArrayList<String>();
		
		// TODO: make server call and parse list
		
		return friendList;
	}
	
	
	
	/**
	 * Reset the user's password.
	 *
	 * @param oldPassword			the old password
	 * @param newPassword			the <PASSWORD>
	 * @return 						true, if successful
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static boolean resetPassword(String oldPassword, String newPassword) throws JSONFailureException
	{
		Json json = new Json();
		json.sendRequest("https://jbaron6.cs2212.ca/resetpassword?answer=" + oldPassword + "&new_password=" + newPassword);
		
		return true;
	}
	
	public static int getQuestionIndex() throws JSONFailureException
	{
		Json json = new Json();
		JSONObject result =  json.sendRequest("https://jbaron6.cs2212.ca/getQuestionIndex");
		
		return result.get("question_index") == null ? -1 : Integer.parseInt((String)result.get("question_index"));
	}
	
	/**
	 * Gets a friend's progeny.
	 *
	 * @return 						an array of progeny
	 * @throws JSONFailureException the jSON failure exception
	 */
	public static ArrayList<Progeny> getFriendProgeny() throws JSONFailureException 
	{
		ArrayList<Progeny> progenyList;

		progenyList = new ArrayList<Progeny>();
		
		// TODO: make server call and parse list
		
		return progenyList;
	}

	// ** NOTE ** //
		/**
		 * This is a dummy method that emulates how getFriends will work I'm
		 * including it in lieu of just creating a string array in the StatsMenu2
		 * file.
		 * 
		 * @return an array of friends
		 * @throws JSONFailureException
{'fraction_non_alphanumeric': 0.09667093469910372, 'fraction_numerical': 0.008002560819462228, 'mean_word_length': 3.7553272450532726, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 17, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7459860', 'n_tokens_mistral': 3927, 'n_tokens_neox': 3359, 'n_words': 1168}