package com.virtual.pollentia.WalkingTour.modelo;

import android.os.AsyncTask;
import android.util.Log;

import com.virtual.pollentia.WalkingTour.controlador.Controlador;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.FileInputStream;
import java.util.ArrayList;

/**
 * Created by Roger Gomez
 */

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

    private ArrayList<PuntoGps> routePoints;
    private ArrayList<PuntoGps> wayPoints;
    private Controlador controlador;

    public RouteLoader(Controlador con) {
        controlador = con;
    }

    @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;
            Double lat = -1.0;
            Double lon = -1.0;
            String wptDescription = "";
            int grade = -1;


            routePoints = new ArrayList<>();
            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 = Double.parseDouble(parser.getAttributeValue(
                                    null, "lat"));
                            lon = Double.parseDouble(parser.getAttributeValue(
                                    null, "lon"));
                            //grade = Integer.parseInt(parser.getAttributeValue(
                            //null, "grade"));
                            routePoints.add(pointCounter, new PuntoGps(0, lat, lon, 0, ""));

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

                parserEvent = parser.next();
            }

            //guardarBD();

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

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


    protected void onPostExecute() {

        //overlayRoute();
        //controlador.guardarPuntos();
    }


    protected void onPreExecute() {

    }

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

    /*private void guardarBD() {
        controlador.alta(routePoints,wayPoints);


    }*/

}
