package app.mar.game;

import android.app.Activity;

import java.io.Serializable;

import app.mar.utils.SensorsManager;
import app.mar.utils.geometry.GPSPoint;

/**
 * Created by ptitcois on 20/08/16.
 */
public class Player implements Serializable{
    protected transient SensorsManager mSensors;

    private static final int STATE_SEARCHING=0;
    private static final int STATE_FOUND=1;
    private int mState=STATE_FOUND;
    private GPSPoint mFreezedPosition;

    public Player(Activity sm)
    {
        mSensors=new SensorsManager(sm);
    }

    public void setSensorsManager(SensorsManager sm)
    {
        mSensors=sm;
    }

    public SensorsManager getSensorsManager()
    {
        return mSensors;
    }

    public void onPause()
    {
        mSensors.onPause();
    }

    public boolean hasMagneticField() {
        return mSensors.hasMagneticField();
    }

    public boolean hasAcceleromter() {
        return mSensors.hasAcceleromter();
    }

    public void onResume()
    {
        if(mSensors!=null)
            mSensors.onResume();
    }


    public float getOrientation()
    {
        return mSensors.getOrientation();
    }

    public float getOrientationX()
    {

        return mSensors.getAzimuth();
    }

    public float getOrientationY()
    {

        return mSensors.getAngleY();
    }


    public float getOrientationZ()
    {
        return mSensors.getAngleZ();
    }

    public double getAngleWith(Place p)
    {
        double d=getPosition().getAngleWith(p.getLocation());;
        while(d<0) d+=360;
        return d;
    }

    public double getXWith(Place p)
    {
        return getPosition().getDistanceXWith(p.getLocation());
    }

    public double getYWith(Place p)
    {
        return getPosition().getDistanceYWith(p.getLocation());
    }

    public double getRelXWith(Place p)
    {
        return getPosition().getRelXWith(p.getLocation());
    }

    public double getRelYWith(Place p)
    {
        return getPosition().getRelYWith(p.getLocation());
    }

    public void newSensorManager(Activity act)
    {
        mSensors = new SensorsManager(act);
    }

    public void nextStub()
    {
        mSensors.nextStub();
    }

    public GPSPoint getPosition()
    {
        return mSensors.getPosition();
        /*
        if(mState==STATE_SEARCHING || mSensors.useGpsStub())
            return mSensors.getPosition();
        else
            return mFreezedPosition;
            */
    }

    public void freezePosition()
    {
        return ;
        /*mState=STATE_FOUND;
        mFreezedPosition=mSensors.getPosition().copy();*/
    }

    public void freezePosition(GPSPoint target)
    {
        return ;
        /*if( mState==STATE_SEARCHING
                || (mSensors.getPosition()!=null &&  mFreezedPosition!=null &&
                mSensors.getPosition().getDistanceWith(target) < mFreezedPosition.getDistanceWith(target)))
            mFreezedPosition=mSensors.getPosition().copy();
        mState=STATE_FOUND;*/
    }

    public void releasePosition()
    {
        mState=STATE_SEARCHING;
    }
}
