package de.metalmatze.krautreporter;

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

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.metalmatze.krautreporter.entities.Article;

public class KrautreporterRssParser {

    private ArrayList<Article> articles;
    private Article article;
    private String text;
    private static final String ns = null;
    protected XmlPullParser parser;

    public KrautreporterRssParser() throws XmlPullParserException {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        this.parser = factory.newPullParser();
        this.articles = new ArrayList<Article>();
        this.article = new Article();
    }

    public ArrayList<Article> parse(BufferedInputStream bufferedInputStream) throws XmlPullParserException, IOException {
        this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        this.parser.setInput(bufferedInputStream, null);

        int eventType = this.parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            String tagName = this.parser.getName();

            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagName.equalsIgnoreCase("item")) {
                        this.article = new Article();
                    }
                    break;
                case XmlPullParser.TEXT:
                    this.text = this.parser.getText();
                    break;
                case XmlPullParser.END_TAG:
                    if (tagName.equalsIgnoreCase("item"))
                    {
                        this.articles.add(this.article);
                    }
                    else if (tagName.equalsIgnoreCase("guid"))
                    {
                        this.article.setUuid(this.text);
                    }
                    else if (tagName.equalsIgnoreCase("title"))
                    {
                        this.article.setTitle(this.text.trim());
                    }
                    else if (tagName.equalsIgnoreCase("pubDate"))
                    {
                        this.article.setDate(this.parseDate(this.text));
                    }
                    else if (tagName.equalsIgnoreCase("link"))
                    {
                        this.article.setLink(new URL(this.text));
                    }
                    else if (tagName.equalsIgnoreCase("description"))
                    {
                        String content = this.parseContent(this.text);
                        this.article.setContent(content);
                    }
                    break;
                default:
                    break;
            }

            eventType = this.parser.next();
        }

        return this.articles;
    }

    private Date parseDate(String text) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);

        Date date = null;
        try {
            date = dateFormat.parse(text);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    private String parseContent(String text) {

        this.parseTeaserImage(text);

        return text;
    }

    private void parseTeaserImage(String text) {
        Pattern pattern = Pattern.compile("(<img src=')(.*(teaser_image).*)('\\/>)");
        Matcher matcher = pattern.matcher(text);

        while (matcher.find())
        {
            try {
                this.article.setTeaserImage(new URL("http://krautreporter.de" + matcher.group(2)));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }

}

@@ -531,15 +531,13 @@
 ter.
-entitie
+model
 s.Ar
@@ -541,16 +541,21 @@
 .Article
+Model
 ;%0A%0Apubli
@@ -617,16 +617,21 @@
 %3CArticle
+Model
 %3E articl
@@ -653,16 +653,21 @@
  Article
+Model
  article
@@ -1049,16 +1049,21 @@
 %3CArticle
+Model
 %3E();%0A   
@@ -1085,32 +1085,37 @@
 le = new Article
+Model
 ();%0A    %7D%0A%0A    p
@@ -1137,16 +1137,21 @@
 %3CArticle
+Model
 %3E parse(
@@ -1729,16 +1729,21 @@
  Article
+Model
 ();%0A    
@@ -2239,16 +2239,15 @@
 cle.
-setUuid(
+uuid = 
 this
@@ -2243,33 +2243,32 @@
 uuid = this.text
-)
 ;%0A              
@@ -2402,17 +2402,16 @@
 cle.
-setT
+t
 itle
-(
+ = 
 this
@@ -2419,16 +2419,8 @@
 text
-.trim())
 ;%0A  
@@ -2568,16 +2568,15 @@
 cle.
-setDate(
+date = 
 this
@@ -2588,33 +2588,32 @@
 eDate(this.text)
-)
 ;%0A              
@@ -2746,16 +2746,15 @@
 cle.
-setLink(
+link = 
 new 
@@ -2767,17 +2767,16 @@
 is.text)
-)
 ;%0A      
@@ -2995,27 +2995,25 @@
 cle.
-setC
+c
 ontent
-(
+ = 
 content
-)
 ;%0A  
@@ -3946,23 +3946,16 @@
 cle.
-setTeaserI
+i
 mage
-(
+ = 
 new 
@@ -4003,17 +4003,16 @@
 roup(2))
-)
 ;%0A      
@@ -4110,21 +4110,22 @@
 %7D%0A        %7D%0A
+%0A
     %7D%0A%0A%7D%0A

Return list of ArticleModel from KrautreporterRssParser