package app.mar.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 app.mar.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=0;

    public Stage(Game g, JSONObject root, Activity act)
    {
        try {
            mName = root.getString("name");
            mDone = false;
            JSONArray 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());
        }

        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) {
        }
    }


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

    public Challenge nextChallenge()
    {
        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;
    }

}
