package macampcorp.macamp.game;

import android.app.Activity;

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

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

import macampcorp.macamp.activities.HostActivity;
import macampcorp.macamp.game.challenges.Challenge;

public class Stage  implements Serializable {
    protected String mName;
    protected ArrayList<Challenge> mChallenge = new ArrayList<Challenge>();
    protected ArrayList<Resource> mResources = new ArrayList<Resource>();
    protected boolean mDone=false;
    protected int     mStep=-1;
    protected int   mCharIndex=-1;

    protected ArrayList<Event> mStartEvent = new ArrayList<Event>();
    protected ArrayList<Event> mEndEvent = new ArrayList<Event>();





    public Stage(Game g, JSONObject root, Activity act)
    {
        try {
            mName = root.getString("name");
            mDone = false;

        } catch (JSONException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage() + " Name:"+mName);
        }


        try {
            JSONArray chall = null;
            chall = root.getJSONArray("challenges");
            for(int i=0; i<chall.length(); i++)
            {
                JSONObject obj = chall.getJSONObject(i);
                String name = obj.getString("type");
                Challenge c = Challenge.callChallengeConstructor(name, g, obj, act);
                mChallenge.add(c);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage() + " Name:"+mName);
        }


        try{

            JSONArray chall = root.getJSONArray("resources");
            for(int i=0; i<chall.length(); i++)
                mResources.add(new Resource(chall.getString(i), act));
        } catch (JSONException e) {
        }

        try{
            mCharIndex = root.getInt("charIndex");
        } catch (JSONException e) {
        }

        /**
         * event_start
         */
        try {
            JSONArray arr = root.getJSONArray("event_start");
            for(int i=0; i<arr.length(); i++)
                mStartEvent.add(new Event(arr.getJSONObject(i)));

        } catch (JSONException e) {
        }

        try {
            mStartEvent.add(new Event(root.getJSONObject("event_start")));
        } catch (JSONException e) {
        }

        /**
         * event_start
         */
        try {
            JSONArray arr = root.getJSONArray("event_end");
            for(int i=0; i<arr.length(); i++)
                mEndEvent.add(new Event(arr.getJSONObject(i)));

        } catch (JSONException e) {
        }

        try {
            mEndEvent.add(new Event(root.getJSONObject("event_end")));
        } catch (JSONException e) {
        }
    }




    public int getCharIndex() {return mCharIndex; }


    public String getName() {
        return mName;
    }

    public void setName(String mName) {
        this.mName = mName;
    }

    public ArrayList<Resource> getResources() {
        return mResources;
    }

    public void setResources(ArrayList<Resource> mResources) {
        this.mResources = mResources;
    }

    public boolean ismDone() {
        return mDone;
    }

    public void setDone(boolean mDone) {
        this.mDone = mDone;
    }


    public int getStep() {
        return mStep;
    }

    public int getStepCount() {
        return mChallenge.size();
    }

    Challenge nextChallenge(HostActivity dri, Game g)
    {
        mStep++;
        if(mStep<mChallenge.size())
            return mChallenge.get(mStep);

        return null;
    }


    public boolean isFinished()
    {
        if(mStep>=mChallenge.size()) mDone=true;
        return mDone;
    }

    public Challenge getCurrentChallenge()
    {
        if(!isFinished()) return mChallenge.get(mStep);
        return null;
    }

    public ArrayList<Event> getStartEvent() {
        return mStartEvent;
    }

    public ArrayList<Event> getEndEvent() {
        return mEndEvent;
    }
}
