package app.mar.game.challenges;

import android.app.Activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import app.mar.activities.QuestionFragment;
import app.mar.game.Game;
import app.mar.utils.Levenshtein;

public class Question extends Challenge {

    protected String mQuestion;
    protected ArrayList<String> mAnswer = new ArrayList<String>();
    protected String mGoodMessage="";
    protected String mBadMessage="";
    public static final int REQUEST_CODE=2004;


    public int getResultCode() { return REQUEST_CODE; }


    public Question(Game g,JSONObject root, Activity a) {
        super(g, root, QuestionFragment.class, a);


        try {
            mQuestion = root.getString("question");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            mAnswer.clear();
            String s = root.getString("answer");
            mAnswer.add(s);
        }catch(JSONException e)
        {
            try{
                JSONArray js = root.getJSONArray("answer");
                for(int i=0; i<js.length(); i++)
                    mAnswer.add(js.getString(i));
            }catch(JSONException ee) {
                ee.printStackTrace();
            }
        }


        try {mGoodMessage = root.getString("good_message"); } catch (JSONException e) {}
        try {mBadMessage = root.getString("bad_message"); } catch (JSONException e) {}
    }



    public boolean checkAnswer(String a, int i)
    {
        int length = mAnswer.get(i).length();
        int n = length/3;
        return Levenshtein.test(mAnswer.get(i), a);
    }

    public boolean checkAnswer(String answer)
    {
        for(int i=0; i< mAnswer.size(); i++)
            if(checkAnswer(answer, i)) return true;
        return false;
    }


    public String getQuestion() {
        return mQuestion;
    }

    public ArrayList<String> getAnswer() {
        return mAnswer;
    }

    public String getGoodMessage() {
        return mGoodMessage;
    }

    public String getBadMessage() {
        return mBadMessage;
    }
}
