package com.blocksolid.okrello;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.blocksolid.okrello.api.TrelloApi;
import com.blocksolid.okrello.model.TrelloCard;

import java.util.ArrayList;

import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;

/**
 * Created by Dan Buckland on 06/12/2015.
 */
public class ObjectivesActivity extends AppCompatActivity {

    public static ArrayList<TrelloCard> trelloCards;
    public static ListView listView;
    public static Button refreshCardsBtn;
    public static ProgressBar objsProgressBar;
    public String listId;
    public String listName;
    ObjectiveAdapter objectiveAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_objectives);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // Set Activity title to the selected list name
        listName = this.getIntent().getExtras().getString("listName");
        setTitle(listName);

        objsProgressBar = (ProgressBar) findViewById(R.id.objs_progress);
        objsProgressBar.setVisibility(View.INVISIBLE);

        listView = (ListView) findViewById(R.id.objs_list_objectives);

        listId = this.getIntent().getExtras().getString("listId");

        objectiveAdapter = new ObjectiveAdapter(this, getLayoutInflater());
        listView.setAdapter(objectiveAdapter);

        getCards();
    }

    private void getCards() {
        // Show progress indicator while working
        objsProgressBar.setVisibility(View.VISIBLE);

        // Retrofit stuff starts here
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(TrelloApi.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        TrelloApi trelloApi = retrofit.create(TrelloApi.class);

        // Define the request
        String fields = "name";
        final Call<ArrayList<TrelloCard>> call = trelloApi.getCards(listId, TrelloApi.KEY, fields);

        // Make the request
        call.enqueue(new Callback<ArrayList<TrelloCard>>() {

            @Override
            public void onResponse(Response<ArrayList<TrelloCard>> response, Retrofit retrofit) {
                trelloCards = response.body();
                // Update data in custom view adapter
                objectiveAdapter.updateData(trelloCards);
                // Hide progress indicator when done
                objsProgressBar.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onFailure(Throwable t) {
                // Log error here since request failed
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
                Log.d("Retrofit", t.getMessage());
                objsProgressBar.setVisibility(View.INVISIBLE);
            }
        });
    }
}

@@ -26,16 +26,47 @@
 rello;%0A%0A
+import android.content.Intent;%0A
 import a
@@ -83,16 +83,16 @@
 Bundle;%0A
-
 import a
@@ -179,24 +179,59 @@
 .view.View;%0A
+import android.widget.AdapterView;%0A
 import andro
@@ -437,16 +437,64 @@
 lloCard;
+%0Aimport com.blocksolid.okrello.model.TrelloList;
 %0A%0Aimport
@@ -763,16 +763,59 @@
 ctivity 
+implements AdapterView.OnItemClickListener 
 %7B%0A%0A    p
@@ -903,50 +903,8 @@
 ew;%0A
-    public static Button refreshCardsBtn;%0A
     
@@ -1821,16 +1821,63 @@
 dapter);
+%0A        listView.setOnItemClickListener(this);
 %0A%0A      
@@ -3340,16 +3340,16 @@
       %7D%0A
-
         
@@ -3357,11 +3357,733 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void onItemClick(AdapterView%3C?%3E parent, View view, int position, long id) %7B%0A        // Grab the ID of the selected Trello List (Quarter)%0A        TrelloCard trelloCard = trelloCards.get(position);%0A        String cardId = trelloCard.getId();%0A        String cardName = trelloCard.getName();%0A%0A        // Intent to take the user to a new KeyResultsActivity%0A        Intent keyResultsIntent = new Intent(this, KeyResultsActivity.class);%0A%0A        // Pass across the list ID in the intent%0A        keyResultsIntent.putExtra(%22cardId%22, cardId);%0A        keyResultsIntent.putExtra(%22cardName%22, cardName);%0A%0A        // start the next Activity using the above intent%0A        startActivity(keyResultsIntent);%0A    %7D
 %0A%7D%0A

Update Objectives activity with onItemClick method to view Key Results