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

import android.util.Log;

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

import java.io.Serializable;

import app.brest.utils.geometry.GPSPoint;

/**
 * Created by ptitcois on 19/08/16.
 */
public class Place  implements Serializable {
    public Place(JSONObject root)
    {

        try {
            JSONArray arr = root.getJSONArray("coordinates");
            mLocation = new GPSPoint(arr.getDouble(0), arr.getDouble(1));
            mRadius = root.getDouble("radius");
            mField = root.getDouble("field");
            mAngle = root.getDouble("angle");
            mUseAngle = root.getBoolean("useAngle");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    protected GPSPoint  mLocation;
    protected double    mRadius;
    protected double    mField;
    protected double    mAngle;
    protected boolean   mUseAngle;
    public GPSPoint getLocation() {
        return mLocation;
    }

    public double getRadius() {
        return mRadius;
    }

    public double getField() {
        return mField;
    }

    public double getAngle() {
        return mAngle;
    }

    public JSONObject getJson()
    {
        JSONObject js = new JSONObject();
        JSONArray ja = new JSONArray();
        try {
            ja.put(mLocation.getY());
            ja.put(mLocation.getY());
            js.put("coordinates", ja);
            js.put("radius", ""+mRadius);
            js.put("field", ""+mField);
            js.put("angle", ""+mAngle);
            js.put("useAngle", ""+mUseAngle);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return js;
    }

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

        return mLocation.getDistanceWith(pp)<=mRadius;
    }

    public boolean isOnDirection(Player p)
    {
        boolean ret;
        //si utilisation de angle et field
        if(mUseAngle)
        {
            float angle = p.getOrientation()%360;
            while (angle < 0) angle += 360;
            double min = mAngle - mField / 2;
            double max = mAngle + mField / 2;


            if (min < 0) {
                min += 360;
                ret = angle >= min || angle <= max;
            } else if (max >= 360) {
                max -= 360;
                ret = angle <= max || (angle >= min);
            } else {
                ret = (angle <= max && angle >= min);
            }
        }
        else
        //si par rapport au coordonnées gps
        {
            float pangle=p.getOrientation()%360;
            while (pangle < 0) pangle += 360;
            float dir = (float)p.getPosition().getAngleWith(mLocation);
            double min = dir - mField / 2;
            double max = dir + mField / 2;
            if (min < 0) {
                min += 360;
                ret = pangle >= min || pangle <= max;
            } else if (max >= 360) {
                max -= 360;
                ret = pangle <= max || (pangle >= min);
            } else {
                ret = (pangle <= max && pangle >= min);
            }
        }
        return ret;
    }

    public boolean isUseAngle()
    {
        return mUseAngle;
    }

    public double getDistance(Player p)
    {
        return mLocation.getDistanceWith(p.getPosition());
    }

}
