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.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 {

    public static final String PHOTOS_BASE_URL =
            "http://services.hanselandpetal.com/photos/";
    public static final String ENDPOINT = "http://services.hanselandpetal.com";
    //This is the base URL, we have described where the web service lives, the complete
    // url lives in the API, if we take the end point and the api defination and put the strings together, that's the complete location of the feed

    TextView output;
    ProgressBar pb;
    List<MyTask> tasks;

    List<Flower> flowerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pb = (ProgressBar) findViewById(R.id.progressBar1);
        pb.setVisibility(View.INVISIBLE);

        tasks = new ArrayList<>();
    }

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

    private void requestData(String uri) {
        MyTask task = new MyTask();
        task.execute(uri);
    }

    protected void updateDisplay() {
        //Use FlowerAdapter to display data
        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, List<Flower>> {

        @Override
        protected void onPreExecute() {
            if (tasks.size() == 0) {
                pb.setVisibility(View.VISIBLE);
            }
            tasks.add(this);
        }

        @Override
        protected List<Flower> doInBackground(String... params) {

            String content = HttpManager.getData(params[0]);
            flowerList = FlowerJsonParser.parseFeed(content);

            return flowerList;
        }

        @Override
        protected void onPostExecute(List<Flower> result) {

            tasks.remove(this);
            if (tasks.size() == 0) {
                pb.setVisibility(View.INVISIBLE);
            }

            if (result == null) {
                Toast.makeText(MainActivity.this, "Web service not available", Toast.LENGTH_LONG).show();
                return;
            }

            flowerList = result;
            updateDisplay();

        }

    }

}
@@ -546,16 +546,136 @@
 .List;%0A%0A
+import retrofit.Callback;%0Aimport retrofit.RestAdapter;%0Aimport retrofit.RetrofitError;%0Aimport retrofit.client.Response;%0A%0A
 public c
@@ -1184,32 +1184,8 @@
  pb;
-%0A    List%3CMyTask%3E tasks;
 %0A%0A  
@@ -1481,44 +1481,8 @@
 LE);
-%0A%0A        tasks = new ArrayList%3C%3E();
 %0A   
@@ -2096,62 +2096,850 @@
     
-MyTask task = new MyTask();%0A        task.execute(uri);
+RestAdapter adapter = new RestAdapter.Builder()%0A                .setEndpoint(ENDPOINT)%0A                .build(); // This screate the adapter object%0A        // we are ready to implement the API that we alread defined%0A        FlowersAPI api = adapter.create(FlowersAPI.class);%0A        //pass in the class property of the API interface%0A        //now we are ready to make a request%0A        api.getFeed(new Callback%3CList%3CFlower%3E%3E() %7B%0A            @Override%0A            public void success(List%3CFlower%3E flowers, Response response) %7B%0A                flowerList = flowers;%0A                updateDisplay();%0A            %7D%0A%0A            @Override%0A            public void failure(RetrofitError retrofitError) %7B%0A%0A            %7D%0A        %7D);%0A        //This argument ask for a response object, that will be what we declared that it will return a list of Flowers
 %0A   
@@ -3498,32 +3498,34 @@
    %7D%0A    %7D%0A%0A    
+/*
 private class My
@@ -4494,16 +4494,18 @@
      %7D%0A%0A
-
     %7D
+*/
 %0A%0A%7D

Implement retrofix in the MainActivity