package com.andela.webservice;

import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.andela.webservice.model.Flower;
import com.andela.webservice.parser.FlowerJsonParser;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ListActivity {
    private TextView output;
    private ProgressBar progressBar;
    private List<MyTask> tasks;
    private List<Flower> flowerList;
    private static final String PHOTO_BASE_URL = "http://services.hanselandpetal.com/photos/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.INVISIBLE);
        tasks = new ArrayList<>();
        //output = (TextView) findViewById(R.id.textView);
        //output.setMovementMethod(new ScrollingMovementMethod());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_do_task) {
            if(isOnline()) {
                requestData("http://services.hanselandpetal.com/secure/flowers.json");
            } else {
                Toast.makeText(this, "Network isn't available", Toast.LENGTH_LONG).show();
            }

        }
        return true;
    }

    private void requestData(String uri) {
        MyTask task = new MyTask();
        //This makes serial request
        task.execute(uri);
        //For parrallel processing
        //task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "param1", "param2", "param3");
    }

    private void updateDisplay() {
        /*if(flowerList != null) {
            for (Flower flower : flowerList) {
                output.append(flower.getName() + "\n");
            }

        }*/

        FlowerAdapter adapter = new FlowerAdapter(this, R.layout.item_flower, flowerList);
        setListAdapter(adapter);
    }

    protected boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if(netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        } else {
            return false;
        }
    }

    private class MyTask extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            //updateDisplay("Starting task");
            if(tasks.size() == 0) {
                progressBar.setVisibility(View.VISIBLE);
            }
            tasks.add(this);
        }

        @Override
        protected String doInBackground(String... params) {
            String content = HttpManager.getData(params[0],"feeduser","feedpassword");
            return content;
        }

        @Override
        protected void onPostExecute(String result) {
            tasks.remove(this);
            if(tasks.size() == 0) {
                progressBar.setVisibility(View.INVISIBLE);
            }

            //flowerList = FlowerXmlParser.parseFeed(result);
            if(result == null) {
                Toast.makeText(MainActivity.this, "Can't connect to webservice", Toast.LENGTH_LONG).show();
                return;
            }
            flowerList = FlowerJsonParser.parseFeed(result);
            updateDisplay();

        }

        @Override
        protected void onProgressUpdate(String... values) {
            //updateDisplay(values[0]);
        }
    }


}

@@ -2886,17 +2886,81 @@
 ng, 
-String%3E %7B
+List%3CFlower%3E%3E %7B%0A        //Async task now returns a List of Flower objects
 %0A   
@@ -3199,32 +3199,121 @@
 is);%0A        %7D%0A%0A
+        /*%0A            Doin the background parsing in the background thread%0A         */%0A%0A
         @Overrid
@@ -3332,22 +3332,28 @@
 otected 
-String
+List%3CFlower%3E
  doInBac
@@ -3483,21 +3483,86 @@
     
-return conten
+flowerList = FlowerJsonParser.parseFeed(content);%0A            return flowerLis
 t;%0A 
@@ -3626,22 +3626,28 @@
 Execute(
-String
+List%3CFlower%3E
  result)
@@ -4061,42 +4061,14 @@
 t = 
-FlowerJsonParser.parseFeed(
 result
-)
 ;%0A  

Change the implementation of the Async task to return List of Flower object