package com.virtual.tour.arquelogia.modelo;

/**
 * Created by Roger Gomez
 */

import android.os.AsyncTask;
import android.util.Log;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.FileInputStream;
import java.util.ArrayList;



public class RouteLoader extends AsyncTask<String,String, String> {

    @Override
    protected String doInBackground(String... params) {
        // This pattern takes more than one param but we'll just use the
        // first
        try {

            String fichero=params[0];
            FileInputStream in = new FileInputStream(fichero);
            XmlPullParserFactory parserCreator;

            parserCreator = XmlPullParserFactory.newInstance();

            XmlPullParser parser = parserCreator.newPullParser();

            parser.setInput(in, null);

            publishProgress("Parsing XML...");

            int parserEvent = parser.getEventType();
            int pointCounter = -1;
            int wptCounter = -1;
            int totalWaypoints = -1;
            int lat = -1;
            int lon = -1;
            String wptDescription = "";
            int grade = -1;


            ArrayList<PuntoGps> routePoints=new ArrayList<>();
            ArrayList<PuntoGps> wayPoints=new ArrayList<>();
            // Parse the XML returned on the network
            while (parserEvent != XmlPullParser.END_DOCUMENT) {
                switch (parserEvent) {
                    case XmlPullParser.START_TAG:
                        String tag = parser.getName();
                        if (tag.compareTo("trkpt") == 0) {
                            pointCounter++;
                            lat = Integer.parseInt(parser.getAttributeValue(
                                    null, "lat"));
                            lon = Integer.parseInt(parser.getAttributeValue(
                                    null, "lon"));
                            grade = Integer.parseInt(parser.getAttributeValue(
                                    null, "grade"));
                            routePoints.add(pointCounter, new PuntoGps(1,lat, lon, grade, ""));

                        } else if (tag.compareTo("wpt") == 0) {
                            wptCounter++;
                            lat = Integer.parseInt(parser.getAttributeValue(
                                    null, "lat"));
                            lon = Integer.parseInt(parser.getAttributeValue(
                                    null, "lon"));
                            grade = Integer.parseInt(parser.getAttributeValue(
                                    null, "grade"));
                            wptDescription = parser.getAttributeValue(null,
                                    "description");
                            routePoints.add(wptCounter,new PuntoGps(1,lat, lon,grade,wptDescription));
                        }
                        break;
                }

                parserEvent = parser.next();
            }

        } catch (Exception e) {
            Log.i("RouteLoader", "Failed in parsing XML", e);
            return "Finished with failure.";
        }

        return "Done...";
    }

    protected void onCancelled() {
        Log.i("RouteLoader", "GetRoute task Cancelled");
    }

    // Now that route data are loaded, execute the method to overlay the
    // route on the map
    protected void onPostExecute(String result) {

        //overlayRoute();
    }



    protected void onPreExecute() {

    }

    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }

}
