package app.brest.utils;

import android.util.Log;

import java.util.ArrayList;
import java.util.StringTokenizer;

import app.brest.utils.geometry.GPSPoint;

/**
 * Created by ptitcois on 24/10/16.
 */
public class GpsStub {
    protected ArrayList<GPSPoint> mStubs = new ArrayList<GPSPoint>();
    protected boolean mIsActive;
    protected int mI = 0;

    public GpsStub(boolean act)
    {
        mIsActive=act;
    }

    public GpsStub(String r)
    {
        mIsActive=true;
        add(r);
    }


    public void add(GPSPoint p)
    {
        mStubs.add(p);
    }

    public void add(String p)
    {
        String r[] = p.split(";");
        for(int i=0; i<r.length; i++)
            addLine(r[i]);
    }

    private void addLine(String str)
    {
        GPSPoint p =  null;
        double lon=0, lat=0, acc=0;
        String res[] = str.split("\\s+");
        try {
            if (res.length > 0) lon = (double) Double.parseDouble(res[0]);
            if (res.length > 1) lat = (double) Double.parseDouble(res[1]);
        }catch (Exception e)
        {
            Log.e("GPSStub", "Stub reading error (first) on '"+str+"'");
        }

        try {
            if (res.length > 2) acc = (double) Double.parseDouble(res[2]);
        }catch (Exception e)
        {
            Log.e("GPSStub", "Stub reading error (second) on '"+str+"'");
        }

        add(new GPSPoint(lon, lat,(float) acc));
    }

    public GPSPoint next()
    {
        if(mStubs.size()>0)
        {
            GPSPoint p = mStubs.get(mI);
            mI++;
            if(mI==mStubs.size())
                mI=0;
            return p;
        }
        else return null;
    }


}
