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.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.blocksolid.okrello.api.ServiceGenerator;
import com.blocksolid.okrello.api.TrelloApi;
import com.blocksolid.okrello.model.TrelloCard;
import com.blocksolid.okrello.model.TrelloCheckItem;
import com.blocksolid.okrello.model.TrelloChecklist;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

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

    public static TrelloApi trelloApi;
    public static ArrayList<TrelloChecklist> trelloChecklists;
    public static ArrayList<TrelloCheckItem> keyResults;
    public static TrelloCard trelloCard;
    public static ListView listView;
    public static ProgressBar keyresProgressBar;
    public String cardId;
    public String objective;
    public String checklistId;
    KeyResultAdapter keyResultAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_results);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        trelloApi = ServiceGenerator.createService(TrelloApi.class);

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

        keyresProgressBar = (ProgressBar) findViewById(R.id.keyres_progress);
        keyresProgressBar.setVisibility(View.INVISIBLE);

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

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

        keyResultAdapter = new KeyResultAdapter(this, getLayoutInflater());
        listView.setAdapter(keyResultAdapter);

        getKeyResults();
    }

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

        // Define the request
        String fields = "name";
        final Call<ArrayList<TrelloChecklist>> call = trelloApi.getChecklists(cardId, TrelloApi.KEY, fields);

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

            @Override
            public void onResponse(Call<ArrayList<TrelloChecklist>> cardCall, Response<ArrayList<TrelloChecklist>> response) {
                trelloChecklists = response.body();
                // Update data in custom view adapter
                // TODO decide which checklist is the correct one to use
                // TODO extract this method out to somewhere else - not sure where

                // Populate an ArrayList of checkItems called keyResults
                keyResults = getKeyResultsCheckitems(trelloChecklists);
                // Update data in custom view adapter
                keyResultAdapter.updateData(keyResults);
                // Hide progress indicator when done
                keyresProgressBar.setVisibility(View.INVISIBLE);
            }

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

    public ArrayList<TrelloCheckItem> getKeyResultsCheckitems(ArrayList<TrelloChecklist> trelloChecklists) {
        ArrayList<TrelloCheckItem> keyResults = null;
        int position = getKeyResultsChecklistPosition(trelloChecklists);

        if (position >= 0) {
            keyResults = trelloChecklists.get(position).getTrelloCheckItems();
        }
        return keyResults;
    }

    public int getKeyResultsChecklistPosition(ArrayList<TrelloChecklist> trelloChecklists) {
        if (trelloChecklists.size() == 1) {
            // If there's only one checklist, use that checklist as Key Results
            return 0;
        } else if (trelloChecklists.size() > 1) {
            // When card has more than one checklist belonging to a card
            // only checkItems belonging to the checklist called "Key Results" are displayed.
            for (int i = 0; i < trelloChecklists.size(); i++) {
                String checklistName = trelloChecklists.get(i).getName();
                if (checklistName.equals("Key Results")) {
                    return i;
                }
            }
        }
        // When there is no checklist called "Key Results" belonging to a card with
        // multiple checklists, then no Key Results are displayed.
        return -1;
    }
}

@@ -762,71 +762,8 @@
 pi;%0A
-    public static ArrayList%3CTrelloChecklist%3E trelloChecklists;%0A
     
@@ -2006,24 +2006,64 @@
 tAdapter);%0A%0A
+        trelloCard = new TrelloCard();%0A%0A
         getK
@@ -2654,32 +2654,40 @@
          trelloC
+ard.setC
 hecklists = resp
@@ -2679,19 +2679,17 @@
 ecklists
- = 
+(
 response
@@ -2699,220 +2699,10 @@
 dy()
+)
 ;
-%0A                // Update data in custom view adapter%0A                // TODO decide which checklist is the correct one to use%0A                // TODO extract this method out to somewhere else - not sure where%0A
 %0A   
@@ -2831,25 +2831,19 @@
 (trelloC
-hecklists
+ard
 );%0A     
@@ -3564,315 +3564,160 @@
 ems(
-ArrayList%3CTrelloChecklist%3E trelloChecklists) %7B%0A        ArrayList%3CTrelloCheckItem%3E keyResults = null;%0A        int position = getKeyResultsChecklistPosition(trelloChecklists);%0A%0A        if (position %3E= 0) %7B%0A            keyResults = trelloChecklists.get(position).getTrelloCheckItems();%0A        %7D%0A        return
+TrelloCard trelloCard) %7B%0A        // Initialise keyResults ArrayList as null to be returned if there are no keyResults%0A        ArrayList%3CTrelloCheckItem%3E
  key
@@ -3727,32 +3727,51 @@
 ults
+ = null
 ;%0A    
-%7D%0A%0A
     
-public int 
+int position = trelloCard.
 getK
@@ -3801,322 +3801,86 @@
 ion(
-ArrayList%3CTrelloChecklist%3E trelloChecklists) %7B%0A        if (trelloChecklists.size() == 1) %7B%0A            // If there's only one checklist, use that checklist as Key Results%0A            return 0;%0A        %7D else if (trelloChecklists.size() %3E 1) %7B%0A            // When card has more than one checklist belonging to a car
+);%0A        if (position %3E= 0) %7B // If a suitable Key Results checklist is foun
 d%0A  
@@ -3896,12 +3896,11 @@
  // 
-only
+Get
  che
@@ -3911,171 +3911,72 @@
 ems 
-belonging to the checklist called %22Key Results%22 are displayed.%0A            for (int i = 0; i %3C trelloChecklists.size(); i++) %7B%0A                String checklistName
+to use as Key Results from that checklist%0A            keyResults
  = t
@@ -3977,24 +3977,32 @@
 ts = trelloC
+ard.getC
 hecklists.ge
@@ -4002,326 +4002,83 @@
 ists
+()
 .get(
-i).getName();%0A                if (checklistName.equals(%22Key Results%22)) %7B%0A                    return i;%0A                %7D%0A            %7D%0A        %7D%0A        // When there is no checklist called %22Key Results%22 belonging to a card with%0A        // multiple checklists, then no K
+position).getTrelloCheckItems();%0A        %7D%0A        return k
 ey
- 
 Results
- are displayed.%0A        return -1
 ;%0A  

Use correct getKeyResultsChecklistPosition method