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();
            } else {
                Toast.makeText(this, "Network isn't available", Toast.LENGTH_LONG).show();
            }

        }
        return true;
    }

    private void requestData() {
        MyTask task = new MyTask();
        //This makes serial request
        //task.execute("param1", "param2", "param3");
        //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) {
            for(int i = 0; i< params.length; i++) {
                publishProgress("working with " + params[i]);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            return "Task Complete";
        }

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


}

@@ -1440,19 +1440,73 @@
 estData(
+%22http://services.hanselandpetal.com/feeds/flowers.xml%22
 );%0A
-
         
@@ -1691,16 +1691,26 @@
 estData(
+String uri
 ) %7B%0A    
@@ -1785,18 +1785,16 @@
         
-//
 task.exe
@@ -1802,36 +1802,11 @@
 ute(
-%22param1%22, %22param2%22, %22param3%22
+uri
 );%0A 
@@ -1839,24 +1839,24 @@
 cessing%0A
-
         
 task.exe
@@ -1839,32 +1839,34 @@
 cessing%0A        
+//
 task.executeOnEx
@@ -2784,111 +2784,57 @@
     
-for(int i = 0; i%3C params.length; i++) %7B%0A                publishProgress(%22working with %22 + 
+String content = HttpManager.getData(
 params%5B
-i
+0
 %5D);%0A
-%0A
     
@@ -2845,217 +2845,22 @@
     
-    try %7B%0A                    Thread.sleep(1000);%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A%0A            return %22Task Complete%22
+return content
 ;%0A  

Call the new implemented HttpManager in the AsyncTask subclass of the MainActivity