package de.metalmatze.krautreporter;

import android.os.AsyncTask;

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

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.metalmatze.krautreporter.models.ArticleModel;

public class KrautreporterRssParser {

    private ArrayList<ArticleModel> articles;
    private ArticleModel 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<ArticleModel>();
        this.article = new ArticleModel();
    }

    public ArrayList<ArticleModel> parse(String rss) throws XmlPullParserException, IOException {
        this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        this.parser.setInput(new ByteArrayInputStream(rss.getBytes("UTF-8")), 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 ArticleModel();
                    }
                    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.uuid = this.text;
                    }
                    else if (tagName.equalsIgnoreCase("title"))
                    {
                        this.article.title = this.text;
                    }
                    else if (tagName.equalsIgnoreCase("pubDate"))
                    {
                        this.article.date = this.parseDate(this.text);
                    }
                    else if (tagName.equalsIgnoreCase("link"))
                    {
                        this.article.link = this.text;
                    }
                    else if (tagName.equalsIgnoreCase("description"))
                    {
                        String content = this.parseContent(this.text);
                        this.article.content = 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())
        {
            this.article.image = "https://krautreporter.de" + matcher.group(2);
        }
    }

    public static class KrautreporterRssParserTask extends AsyncTask<String, Void, List<ArticleModel>>
    {
        @Override
        protected List<ArticleModel> doInBackground(String... params) {
            String rss = params[0];

            try
            {
                KrautreporterRssParser rssParser = new KrautreporterRssParser();
                return rssParser.parse(rss);

            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }

}

@@ -35,38 +35,8 @@
 r;%0A%0A
-import android.os.AsyncTask;%0A%0A
 impo
@@ -174,36 +174,27 @@
 java.io.
-ByteArrayInputStream
+IOException
 ;%0Aimport
@@ -199,34 +199,34 @@
 rt java.io.I
-OException
+nputStream
 ;%0Aimport jav
@@ -338,31 +338,8 @@
 te;%0A
-import java.util.List;%0A
 impo
@@ -1082,26 +1082,39 @@
 %3E parse(
-String rss
+InputStream inputStream
 ) throws
@@ -1266,61 +1266,28 @@
 put(
-new ByteArrayInputStream(rss.getBytes(%22UTF-8%22)), null
+inputStream, %22UTF-8%22
 );%0A%0A
@@ -3898,617 +3898,6 @@
  %7D%0A%0A
-    public static class KrautreporterRssParserTask extends AsyncTask%3CString, Void, List%3CArticleModel%3E%3E%0A    %7B%0A        @Override%0A        protected List%3CArticleModel%3E doInBackground(String... params) %7B%0A            String rss = params%5B0%5D;%0A%0A            try%0A            %7B%0A                KrautreporterRssParser rssParser = new KrautreporterRssParser();%0A                return rssParser.parse(rss);%0A%0A            %7D catch (XmlPullParserException e) %7B%0A                e.printStackTrace();%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A%0A            return null;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Remove async task for KrautreporterParser