package ilearnrw.user.profile;

import ilearnrw.prototype.application.JsonHandler;
import ilearnrw.textclassification.Word;
import ilearnrw.user.problems.EnglishProblems;
import ilearnrw.user.problems.GreekProblems;
import ilearnrw.user.problems.ProblemDefinition;
import ilearnrw.user.problems.ProblemDefinitionIndex;
import ilearnrw.user.problems.ProblemDescription;
import ilearnrw.user.problems.Problems;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;

public class UserProblems implements Serializable {
	private static final long serialVersionUID = 1L;

	private ProblemDefinitionIndex problems;
	private UserSeverities userSeverities;
	private ArrayList<Word> trickyWords;
	
	public UserProblems(){
		problems = new ProblemDefinitionIndex();
		userSeverities = new UserSeverities();
		setTrickyWords(new ArrayList<Word>());
	}
	
	public UserProblems(ProblemDefinitionIndex theProblems){
		initialize(theProblems);
		setTrickyWords(new ArrayList<Word>());
	}
	
	private void initialize(ProblemDefinitionIndex theProblems){
		this.problems = theProblems;
		userSeverities = new UserSeverities(theProblems.getIndexLength());
		
		int idxLen = theProblems.getIndexLength();
		for (int i=0; i<idxLen; i++){
			userSeverities.constructRow(i, theProblems.getRowLength(i));
		}

	}
	
	// TODO: eliminate these functions and replace with db fetches!!!
	public void loadTestGreekProblems(){
		JsonHandler handler = new JsonHandler("data/problem_definitions_greece.json", true);
		//GreekProblems greekProbs = (GreekProblems)handler.fromJson(GreekProblems.class);
		//System.out.println(greekProbs.getAllProblems().toString());
		
		GreekProblems greekProbs = new GreekProblems();
		initialize(greekProbs.getAllProblems());
		Random rand = new Random();
		for (int i=0;i<problems.getIndexLength(); i++){
			userSeverities.setWorkingIndex(i, rand.nextInt(problems.getRowLength(i)));
			for (int j=0; j<userSeverities.getSeverityLength(i); j++){
				userSeverities.setSeverity(i, j, 0);//rand.nextInt(4));
			}
		}
	}
	
	public void loadTestEnglishProblems(){
		JsonHandler handler = new JsonHandler("data/problem_definitions_en.json", true);
		EnglishProblems enProbs = (EnglishProblems)handler.fromJson(EnglishProblems.class);
		//System.out.println(greekProbs.getAllProblems().toString());
		
		//GreekProblems greekProbs = new GreekProblems();
		initialize(enProbs.getAllProblems());
		Random rand = new Random();
		for (int i=0;i<problems.getIndexLength(); i++){
			userSeverities.setWorkingIndex(i, rand.nextInt(problems.getRowLength(i)));
			for (int j=0; j<userSeverities.getSeverityLength(i); j++){
				userSeverities.setSeverity(i, j, rand.nextInt(4));
			}
		}
	}

	public ProblemDefinitionIndex getProblems(){
		return problems;
	}
	public UserSeverities getUserSeverities(){
		return userSeverities;
	}

	public void setUserSeverities(UserSeverities userSeverities) {
		this.userSeverities = userSeverities;
	}

	public int getSeverity(int i, int j) {
		return userSeverities.getSeverity(i,j);
	}

	public ArrayList<Word> getTrickyWords() {
		return trickyWords;
	}

	public void setTrickyWords(ArrayList<Word> trickyWords) {
		this.trickyWords = trickyWords;
	}

	public void addTrickyWord(Word trickyWord) {
		if (!this.trickyWords.contains(trickyWord))
			this.trickyWords.add(trickyWord);
	}

	public boolean removeTrickyWord(Word trickyWord) {
		if (!this.trickyWords.contains(trickyWord))
			return false;
		else {
			this.trickyWords.remove(trickyWord);
			return true;
		}
	}

	public int getWorkingIndex(int i) {
		return userSeverities.getWorkingIndex(i);
	}
	
	public int getNumerOfRows() {
		return userSeverities.getNumberOfRows();
	}
	
	public int getRowLength(int i) {
		return userSeverities.getSeverityLength(i);
	}

	public ProblemDescription getProblemDescription(int i, int j) {
		return problems.getProblemDescription(i,j);
	}

	public ProblemDefinition getProblemDefinition(int i) {
		return problems.getProblemDefinition(i);
	}
	
	@Override
	public String toString(){
		if (problems == null || userSeverities == null)
			return "null indices or problems matrix";
		String res = "";
		res = res + problems.toString();
		res = res + "\n\n";
		res = res + userSeverities.toString();
		return res;
	}
	
}

@@ -947,16 +947,212 @@
 Problems
+, true);%0A%09%09setTrickyWords(new ArrayList%3CWord%3E());%0A%09%7D%0A%09public UserProblems(ProblemDefinitionIndex theProblems, boolean initializeUserSeveruties) %7B%0A%09%09initialize(theProblems, initializeUserSeveruties
 );%0A%09%09set
@@ -1247,24 +1247,58 @@
  theProblems
+, boolean initializeUserSeverities
 )%7B%0A%09%09this.pr
@@ -1391,16 +1391,50 @@
 ());%0A%09%09%0A
+%09%09if(initializeUserSeverities) %7B%0A%09
 %09%09int id
@@ -1468,24 +1468,25 @@
 Length();%0A%09%09
+%09
 for (int i=0
@@ -1495,32 +1495,33 @@
 i%3CidxLen; i++)%7B%0A
+%09
 %09%09%09userSeveritie
@@ -1570,18 +1570,22 @@
 (i));%0A%09%09
+%09
 %7D%0A
+%09%09%7D
 %0A%09%7D%0A%09%0A%09/

Allow to not initialize the userSeverities in the constructor when it will eventually be set through the setUserSeverities() function.