package com.example.nuria.fesp_cyl;

import android.util.Log;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class RssParserPull {

    private static final String TAG = RssParserPull.class.getSimpleName();
    private URL rssUrl;

    public RssParserPull() {
    }

    public void RssParserPullSetURL(String url){

        try{

            this.rssUrl = new URL(url);

        }
        catch (MalformedURLException e){

            throw new RuntimeException(e);
        }

    }

    public List<Noticia> parse(){

        List<Noticia> noticias = new ArrayList<>();
        XmlPullParser parser = Xml.newPullParser();
        try{

            parser.setInput(this.getInputStream(), null);
            int evento = parser.getEventType();
            Noticia noticiaActual = null;

            while (evento != XmlPullParser.END_DOCUMENT){

                String etiqueta = null;

                if(evento == XmlPullParser.START_TAG){

                    etiqueta = parser.getName();


                    if (etiqueta.equals("item")){

                        noticiaActual = new Noticia();
                    }
                    else if (noticiaActual != null){

                        if (etiqueta.equals("link")){

                            noticiaActual.setLink(parser.nextText());
                        }
                        else if (etiqueta.equals("description")){

                                noticiaActual.setDescripcion(parser.nextText());

                        }
                        else if (etiqueta.equals("pubDate")){

                            noticiaActual.setFecha(parser.nextText());
                        }
                        else if (etiqueta.equals("title")){

                            noticiaActual.setTitulo(parser.nextText());

                        }else if(etiqueta.equals("category")){

                            noticiaActual.setCategoria(parser.nextText());
                        }
                    }
                } else if(evento == XmlPullParser.END_TAG){
                    etiqueta = parser.getName();
                    if (etiqueta.equals("item") && noticiaActual != null){
                        noticias.add(noticiaActual);
                    }
                }
               evento = parser.next();
                Log.d(TAG, "paso");
            }
        }
        catch (Exception ex){

            throw new RuntimeException(ex);
        }
        return noticias;

    }

    private InputStream getInputStream(){

        try{
            return rssUrl.openConnection().getInputStream();
        }
        catch (IOException e){
            throw new RuntimeException(e);
        }
    }


    public void onDestroy() {
        try {
            super.finalize();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}