package app.brest.utils.app.brest.game;

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

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

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

import app.brest.utils.SensorsManager;
import app.brest.utils.geometry.GPSPoint;
import app.brest.utils.geometry.Shape;

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

    protected String            mName;
    protected String            mResource;
    protected int               mStage;
    protected Shape             mShape = new Shape();
    protected ArrayList<Place>  mPlaces = new ArrayList<Place>();


    public Area(JSONObject root)
    {
        try {
            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)));
            }
            mName = root.getString("name");
            mResource = root.getString("resource");
            mStage = Integer.parseInt(root.getString("stage"));
            ja = root.getJSONArray("points");
            for(int i=0; i<ja.length(); i++) {
                mPlaces.add(new Place(ja.getJSONObject(i)));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public String getName() {
        return mName;
    }

    public String getResourceName() {
        return mResource;
    }

    public Resource getResource(Activity act) {
        return new Resource(mResource, mStage, act);
    }

    public int getStage() {
        return mStage;
    }

    public Shape getShape() {
        return mShape;
    }

    public ArrayList<Place> getPlaces() {
        return mPlaces;
    }

    public int placesCount()
    {
        return mPlaces.size();
    }

    public Place getPlace(int i)
    {
        return mPlaces.get(i);
    }

    public JSONObject getJson()
    {
        JSONObject jo = new JSONObject();
        JSONArray ja = new JSONArray();

        try {
            for(int i=0; i<mPlaces.size(); i++)
                ja.put(mPlaces.get(i).getJson());
            jo.put("points", ja);
            jo.put("coordinates", mShape.getJson());
            jo.put("name", mName);
            jo.put("resource", mResource);
            jo.put("stage", ""+mStage);
        }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)
    {
        for(int i=0; i<mPlaces.size(); i++)
            if(mPlaces.get(i).isPlayerOn(p))
                return true;
        return false;
    }

    public boolean isOnDirection(Player p)
    {

        for(int i=0; i<mPlaces.size(); i++)
            if(mPlaces.get(i).isOnDirection(p))
                return true;
        return false;
    }

    public double getDistanceToNextPlace(Player p)
    {
        double min = 32*1000*1000*1000;
        for(int i=0; i<mPlaces.size(); i++)
            if(mPlaces.get(i).getDistance(p)<min)
                min=mPlaces.get(i).getDistance(p);
        return min;
    }
}
