package app.mar.game.challenges;

import android.app.Activity;

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

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import app.mar.activities.ChallengeDriver;
import app.mar.game.Game;

public abstract class Challenge implements Serializable{

    public static final String CHALLENGE_NULL="null";


    protected String mType=CHALLENGE_NULL;
    protected String mName="null";

    protected char mCharPassword;
    protected int  mCharPasswordIndex;
    protected boolean mDone=false;
    protected Class  mStartWith = null;
    protected ArrayList<Indice> mIndices = new ArrayList<Indice>();
    protected int mLastIndiceDiscoveredIndex = 0;

    public Challenge(JSONObject root, Class start)
    {
        mStartWith=start;
        mDone = false;
        try {
            mType = root.getString("type");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            mName = root.getString("name");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            String s = root.getString("password_char");
            if(s.length()==0) throw new JSONException("password_char is an empty string");
            mCharPassword = s.charAt(0);
            mCharPasswordIndex = root.getInt("password_index");
        } catch (JSONException e) {
            mCharPassword=0;
            mCharPasswordIndex=-1;
        }

        try {
            JSONArray arr = root.getJSONArray("indices");
            for(int i=0; i<arr.length(); i++)
                mIndices.add(Indice.fromJSONObject(arr.getJSONObject(i)));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void exec(ChallengeDriver act)
    {
        act.setFragment(mStartWith);
    }

    public String getType() {
        return mType;
    }

    public String getName() {
        return mName;
    }

    public abstract int getResultCode();

    public boolean isDone() {
        return mDone;
    }

    public void setDone()
    {
        mDone=true;
    }

    public static Class getChallengeClass(String classe)
    {
        try {
            Class cls = Class.forName("app.mar.game.challenges."+classe);
            return cls;
        }
        catch(ClassNotFoundException cnfe)
        {
            cnfe.printStackTrace();
            return null;
        }
    }

    public static Constructor<?> getChallengeConstructor(String classe)
    {
        try {
            Class[] type = { Game.class, JSONObject.class, Activity.class };
            return getChallengeClass(classe).getConstructor(type);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Challenge callChallengeConstructor(String classe, Game g,JSONObject o, Activity a)
    {
        Constructor constr = getChallengeConstructor(classe);
        try {
            return ((Challenge) constr.newInstance(g, o, a));
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    public char getCharPassword() {
        return mCharPassword;
    }

    public int getCharPasswordIndex() {
        return mCharPasswordIndex;
    }

    public boolean hasNextIndice()
    {
        int i=0;
        for(i=0; i<mIndices.size(); i++)
            if(!mIndices.get(i).isDiscovered())
                return true;
        return false;
    }

    public Indice nextIndice()
    {
        int i=0;
        for(i=0; i<mIndices.size(); i++)
            if(!mIndices.get(i).isDiscovered())
                return mIndices.get(mLastIndiceDiscoveredIndex=i);
        mLastIndiceDiscoveredIndex=-1;
        return null;
    }

    public int getIndiceIndex(){
        return mLastIndiceDiscoveredIndex;
    }

    public ArrayList<Indice> getIndices() { return mIndices; }

}
