package de.metalmatze.krautreporter.services;

import android.content.Context;
import android.widget.Toast;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.query.Select;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.List;
import java.util.concurrent.ExecutionException;

import de.metalmatze.krautreporter.KrautreporterRssParser;
import de.metalmatze.krautreporter.models.ArticleModel;

public class ArticleService {

    private static final String RSS_HOST = "https://krautreporter.de/";
    private static final String RSS_FILE = "rss.rss";

    protected Context context;

    public ArticleService(Context applicationContext) {

        this.context = applicationContext;
    }

    public List<ArticleModel> all()
    {
        return new Select().from(ArticleModel.class).orderBy("date DESC").execute();
    }

    public ArticleModel find(long id)
    {
        return new Select().from(ArticleModel.class).where("id = ?", id).executeSingle();
    }

    public void update()
    {
        RequestQueue requestQueue = Volley.newRequestQueue(this.context);
        StringRequest rssRequest = new StringRequest(
            Request.Method.GET,
            RSS_HOST + RSS_FILE,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    try {

                        List<ArticleModel> articleModels = new KrautreporterRssParser.KrautreporterRssParserTask().execute(s).get();
                        saveModels(articleModels);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    volleyError.printStackTrace();
                    Toast.makeText(context, "Error loading feed", Toast.LENGTH_SHORT).show();
                }
            }
        );

        requestQueue.add(rssRequest);
    }

    private void saveModels(List<ArticleModel> models)
    {
            ActiveAndroid.beginTransaction();
            try {
                for (ArticleModel article : models)
                {
                    article.save();
                }
                ActiveAndroid.setTransactionSuccessful();
            }
            finally {
                ActiveAndroid.endTransaction();
            }
    }

}

@@ -2430,36 +2430,32 @@
 )%0A    %7B%0A        
-    
 ActiveAndroid.be
@@ -2476,28 +2476,24 @@
 ();%0A        
-    
 try %7B%0A      
@@ -2498,20 +2498,16 @@
         
-   
- 
 for (Art
@@ -2546,26 +2546,18 @@
         
-    %7B%0A    
+%7B%0A
         
@@ -2588,34 +2588,26 @@
             
-  
-  %7D%0A    
+%7D%0A
             
@@ -2648,36 +2648,32 @@
 sful();%0A        
-    
 %7D%0A            fi
@@ -2666,20 +2666,16 @@
         
- 
-   
 finally 
@@ -2672,28 +2672,24 @@
   finally %7B%0A
-    
             
@@ -2716,28 +2716,24 @@
 nsaction();%0A
-    
         %7D%0A  

Fix indentation in ArticleService