package com.andela.webservice;

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 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) {
        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");
        return true;
    }

    private void updateDisplay(String s) {
        output.append(s + "\n");
    }

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


}

@@ -25,16 +25,120 @@
 rvice;%0A%0A
+import android.content.Context;%0Aimport android.net.ConnectivityManager;%0Aimport android.net.NetworkInfo;%0A
 import a
@@ -158,16 +158,16 @@
 ncTask;%0A
-
 import a
@@ -433,16 +433,45 @@
 extView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -1325,24 +1325,336 @@
 tem item) %7B%0A
+        if(item.getItemId() == R.id.action_do_task) %7B%0A            if(isOnline()) %7B%0A                requestData();%0A            %7D else %7B%0A                Toast.makeText(this, %22Network isn't available%22, Toast.LENGTH_LONG).show();%0A            %7D%0A%0A        %7D%0A        return true;%0A    %7D%0A%0A    private void requestData() %7B%0A
         MyTa
@@ -1896,37 +1896,16 @@
 ram3%22);%0A
-        return true;%0A
     %7D%0A%0A 
@@ -1942,24 +1942,24 @@
 String s) %7B%0A
-
         outp
@@ -1978,32 +1978,379 @@
 + %22%5Cn%22);%0A    %7D%0A%0A
+    protected boolean isOnline() %7B%0A        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);%0A        NetworkInfo netInfo = cm.getActiveNetworkInfo();%0A        if(netInfo != null && netInfo.isConnectedOrConnecting()) %7B%0A            return true;%0A        %7D else %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A
     private clas

Implement method to check if internet connection is available before making request