package com.andela.webservice;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
    private TextView output;
    private ProgressBar progressBar;
    List<MyTask> tasks;

    @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/feeds/flowers.xml");
            } 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(String s) {
        output.append(s + "\n");
    }

    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]);
            return content;
        }

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

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


}

@@ -464,16 +464,113 @@
 Toast;%0A%0A
+import com.andela.webservice.model.Flower;%0Aimport com.andela.webservice.parser.FlowerXmlParser;%0A%0A
 import j
@@ -589,16 +589,16 @@
 ayList;%0A
-
 import j
@@ -737,16 +737,24 @@
 ar;%0A    
+private 
 List%3CMyT
@@ -764,16 +764,53 @@
 %3E tasks;
+%0A    private List%3CFlower%3E flowerList;
 %0A%0A    @O
@@ -1121,17 +1121,16 @@
 st%3C%3E();%0A
-%0A
         
@@ -2118,16 +2118,88 @@
 lay(
-String s
+) %7B%0A        if(flowerList != null) %7B%0A            for (Flower flower : flowerList
 ) %7B%0A
@@ -2202,24 +2202,32 @@
 ) %7B%0A        
+  
+      
 output.appen
@@ -2232,18 +2232,58 @@
 end(
-s + %22%5Cn%22);
+flower.getName() + %22%5Cn%22);%0A            %7D%0A%0A        %7D
 %0A   
@@ -2764,32 +2764,34 @@
 ) %7B%0A            
+//
 updateDisplay(%22S
@@ -3199,24 +3199,86 @@
 g result) %7B%0A
+%0A            flowerList = FlowerXmlParser.parseFeed(result);%0A%0A
             
@@ -3291,22 +3291,16 @@
 Display(
-result
 );%0A     
@@ -3520,24 +3520,24 @@
 . values) %7B%0A
-
             
@@ -3528,32 +3528,34 @@
 ) %7B%0A            
+//
 updateDisplay(va

Implement the FlowerXml in the Async task to get the xml data into the model objects