package com.lutka.weatherstation;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;
import com.weather.lutka.weatherstation.R;

import java.util.List;

/**
 * Created by Paulina on 28/11/2014.
 */
public abstract class GraphActivity extends Activity implements Response.ErrorListener
{
    RequestQueue requestQueue;
    static final String READINGS_URL = "http://weather.cs.nuim.ie/output.php";

    protected abstract void onResponse(ReadingsFeed response);

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.graphs);

        requestQueue = Volley.newRequestQueue(getApplicationContext());

        GsonRequest<ReadingsFeed> readingRequest = new GsonRequest<ReadingsFeed>(READINGS_URL, this, ReadingsFeed.class)
        {
            @Override
            protected void deliverResponse(ReadingsFeed response)
            {
                super.deliverResponse(response);
                onResponse(response);
            }
        };
        requestQueue.add(readingRequest);

        // int num = 100;
        //  GraphView.GraphViewData[] data = new GraphView.GraphViewData[];
    }

    public void drawGraph(List<Reading> readings, String measurementType)
    {
        int num = 100;
        int readingSize = readings.size();
        //obsluzyc przypadek gdy liczba elementow jest mniejsza niz 100
        GraphView.GraphViewData[] data = new GraphView.GraphViewData[num];
        int j =0;
        for (int i = readingSize - num; i < readingSize; i++)
        {
            int time = readings.get(j).getTime();
            int value = readings.get(j).getValue();

            data[j] = new GraphView.GraphViewData(time, value);
            j++;
        }
        GraphViewSeries graphViewReadings =  new GraphViewSeries("Real data readings",
                new GraphViewSeries.GraphViewSeriesStyle(Color.rgb(90, 250, 00),3), data);

        // graph with dynamically genereated horizontal and vertical labels
        GraphView graphView =  new LineGraphView(this, measurementType);

        // add data
        graphView.addSeries(graphViewReadings);
        // set view port, start=a, size=b
        graphView.setViewPort(readings.get(readingSize-num).getTime(), 10*(60*60));
        graphView.setScrollable(true);
        graphView.setScalable(true);//zooming

        graphView.setShowLegend(true);
        graphView.setLegendAlign(GraphView.LegendAlign.BOTTOM);


        graphView.setLegendWidth(200);

        LinearLayout layout = (LinearLayout) findViewById(R.id.dataGraph);

        layout.addView(graphView);

    }


    @Override
    public void onErrorResponse(VolleyError error)
    {
        new AlertDialog.Builder(this)
                .setMessage(error.getMessage())
                .show();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        requestQueue.stop();
    }
}

@@ -1625,16 +1625,77 @@
 eadings,
+int colorReadings, List%3CReading%3E forecast, int colorForecast,
  String 
@@ -1762,24 +1762,30 @@
 adingSize = 
+100;//
 readings.siz
@@ -1951,16 +1951,17 @@
  int j =
+ 
 0;%0A     
@@ -2059,25 +2059,25 @@
 eadings.get(
-j
+i
 ).getTime();
@@ -2114,17 +2114,17 @@
 ngs.get(
-j
+i
 ).getVal
@@ -2129,17 +2129,66 @@
 alue();%0A
-%0A
+            if(j %3C data.length)%0A            %7B%0A    
         
@@ -2259,13 +2259,31 @@
     
+    
 j++;%0A
+            %7D%0A
     
@@ -2361,23 +2361,905 @@
 %22Rea
-l data readings
+dings%22,%0A                new GraphViewSeries.GraphViewSeriesStyle(colorReadings, 3), data);%0A%0A        // graph with dynamically genereated horizontal and vertical labels%0A        GraphView graphView =  new LineGraphView(this, measurementType);%0A%0A        // add data%0A        graphView.addSeries(graphViewReadings);%0A%0A        //to include second series%0A        int forecastSize = 100;//forecast.size();%0A        GraphView.GraphViewData%5B%5D data1 = new GraphView.GraphViewData%5Bnum%5D;%0A        j = 0;%0A        for (int i = forecastSize - num; i %3C forecastSize; i++)%0A        %7B%0A            int time = forecast.get(i).getTime();%0A            int value = forecast.get(i).getValue();%0A            if(j %3C data.length)%0A            %7B%0A                data1%5Bj%5D = new GraphView.GraphViewData(time, value);%0A                j++;%0A            %7D%0A        %7D%0A        GraphViewSeries graphViewForecast =  new GraphViewSeries(%22Forecast
 %22,%0A 
@@ -3318,39 +3318,31 @@
 yle(
-C
+c
 olor
-.rgb(90, 250, 00),
+Forecast, 
 3), data
 );%0A%0A
@@ -3329,32 +3329,33 @@
 recast, 3), data
+1
 );%0A%0A        // g
@@ -3418,32 +3418,34 @@
  labels%0A        
+//
 GraphView graphV
@@ -3439,32 +3439,33 @@
 phView graphView
+1
  =  new LineGrap
@@ -3552,26 +3552,62 @@
 raphView
-Readings);
+Forecast);%0A        /**end of second series*/%0A%0A
 %0A       

add functionality to drawGraph to handle two series instead of one