package macampcorp.macamp.game.challenges;

import android.app.Activity;
import android.util.Log;

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

import java.util.ArrayList;

import macampcorp.macamp.activities.fragments.QuestionFragment;
import macampcorp.macamp.exceptions.BadChallengeConfigException;
import macampcorp.macamp.game.Game;
import macampcorp.macamp.utils.Levenshtein;

public class Question extends Challenge {

    protected String mQuestion;
    protected ArrayList<String> mAnswer = new ArrayList<String>();
    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();
            throw new BadChallengeConfigException("Bad parameter 'question' ('"+mName+"')");
        }

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


    }



    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;
    }

}
