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.utils.L;
import app.mar.utils.geometry.GPSPoint;
import app.mar.utils.geometry.Shape;

/**
 * Created by ptitcois on 19/08/16.
 */
public class Area  implements Serializable {

    protected String                mName;
    protected Shape                 mShape = new Shape();
    protected Place                 mPlaces;

    protected ArrayList<String>     mResourceName = new ArrayList<String>();


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

            JSONArray ja = root.getJSONArray("coordinates");
            for(int i=0; i<ja.length(); i++) {
                JSONArray jaa = ja.getJSONArray(i);
                mShape.add(new GPSPoint(jaa.getDouble(0), jaa.getDouble(1)));
            }

            mPlaces = new Place(root.getJSONObject("point"));

        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            String res []  = root.getString("resource").split(";");
            for(int i=0; i<res.length; i++)
            {
                g.registerResource(res[i], act);
                mResourceName.add(res[i]);
            }
        } catch (JSONException e) {
            L.info("In Area '"+mName+"' : resource field not found");
        }

    }

    public String getName() {
        return mName;
    }

    public ArrayList<String> getResourceNames() {
        return mResourceName;
    }


    public String getResourceName(int i) {
        return mResourceName.get(i);
    }

    public int countResource() {
        return mResourceName.size();
    }

    public Shape getShape() {
        return mShape;
    }

    public Place getPlace() {
        return mPlaces;
    }

    /*
    public JSONObject getJson()
    {
        JSONObject jo = new JSONObject();

        try {
            jo.put(mPlaces.getJson());
            jo.put("points", ja);
            jo.put("coordinates", mShape.getJson());
            jo.put("name", mName);
            jo.put("resource", mResourceName);
        }catch( Exception e)
        {
            e.printStackTrace();
        }
        return jo;
    }*/

    public boolean isOnArea(Player p)
    {
        GPSPoint player = p.getPosition();
        if(player == null)
            return false;

        return mShape.contains(player);

    }


    public boolean isOnPlace(Player p)
    {
        if(mPlaces.isPlayerOn(p))
                return true;
        return false;
    }

    public boolean isOnDirection(Player p)
    {

        if(mPlaces.isOnDirection(p))
                return true;
        return false;
    }

    public double getDistance(Player p)
    {
        return mPlaces.getDistance(p);
    }

    public String toString(Player p)
    {
        String str = "";
        str+="["+mName+" "+mPlaces.getDistance(p)+" m ]";
        return str;
    }
}
