package com.example.hai.eventfinder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.ramotion.foldingcell.FoldingCell;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;


public class myFoldingCellListAdapter extends ArrayAdapter<EventItem> {
    private HashSet<Integer> unfoldedIndexes = new HashSet<>();
    Logger log = Logger.getAnonymousLogger();


    public myFoldingCellListAdapter(Context context, int resource) {
        super(context, resource);
    }

    public myFoldingCellListAdapter(Context context, int resource, List<EventItem> objects) {
        super(context, resource, objects);
    }



    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //View v =  super.getView(position, convertView, parent);
        FoldingCell v = (FoldingCell) convertView;
        ViewHolder viewHolder;

        /*
        Event testEvent = new Event();


        EventRequestHandler testHandler = new EventRequestHandler();

        testHandler.requestEvent(testEvent);
        */

        /*
        Event testEvent = new Event();

        testEvent.requestEvent(testEvent);
        */
        //this to set delegate/listener back to this class
        Event2 testEvent = new Event2();

        ArrayList testResultsArray = new ArrayList<String>();
        try {
            testResultsArray = testEvent.execute().get();
        }
        catch(InterruptedException ie){
            Log.d("Race" , "Condition");
        }
        catch (ExecutionException ee){
            Log.d("Condition" , "Race");
        }
        testEvent.eventName = testResultsArray.get(0).toString();
        //Log.d("y tho" , testEvent.eventName);

        //new Event2().execute();



        if(v == null) {
            log.info("cell isn't null");
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            v = (FoldingCell) inflater.inflate(R.layout.cell, parent, false);

            TextView blah = (TextView)  v.findViewById(R.id.content_title);
            blah.setText(testEvent.eventName);



            viewHolder.price = (TextView) v.findViewById(R.id.title_price);
            viewHolder.date = (TextView) v.findViewById(R.id.title_date);
            viewHolder.time = (TextView) v.findViewById(R.id.title_time);
            viewHolder.name = (TextView) v.findViewById(R.id.title_name);
            viewHolder.address = (TextView) v.findViewById(R.id.title_address);
            viewHolder.ratingLabel = (TextView) v.findViewById(R.id.title_ratinglabel);
            viewHolder.ratingBar = (RatingBar) v.findViewById(R.id.ratingBar);
            viewHolder.timeLabel = (TextView) v.findViewById(R.id.title_timeLabel2);
            viewHolder.eventTypeLabel = (TextView) v.findViewById(R.id.eventTypeLabel);
            viewHolder.eventType = (TextView) v.findViewById(R.id.eventType);
        } else {
            log.info("cell is null");
        }
        return v;
    }

    // simple methods for register cell state changes
    public void registerToggle(int position) {
        if (unfoldedIndexes.contains(position))
            registerFold(position);
        else
            registerUnfold(position);
    }

    public void registerFold(int position) {
        unfoldedIndexes.remove(position);
    }

    public void registerUnfold(int position) {
        unfoldedIndexes.add(position);
    }



    private static class ViewHolder{
        TextView price;
        TextView date;
        TextView time;
        TextView name;
        TextView address;
        TextView ratingLabel;
        RatingBar ratingBar;
        TextView timeLabel;
        TextView eventTypeLabel;
        TextView eventType;


    }
}

@@ -888,32 +888,70 @@
 source);%0A    %7D%0A%0A
+    Event2 testEvent = new Event2();%0A%0A
     public myFol
@@ -1067,24 +1067,678 @@
 , objects);%0A
+%0A%0A        ArrayList testResultsArray = new ArrayList%3CString%3E();%0A%0A        //String testEventName = testResultsArray.get(0).toString();%0A        String testEventName = %22Hello World%22;%0A%0A        //This launches the ASYNC task that calls the API%0A        try %7B%0A            testResultsArray = testEvent.execute().get();%0A            testEventName = testResultsArray.get(0).toString();%0A            Log.d(%22Done%22 , testEventName);%0A        %7D%0A        catch(InterruptedException ie)%7B%0A            Log.d(%22Race%22 , %22Condition%22);%0A        %7D%0A        catch (ExecutionException ee)%7B%0A            Log.d(%22Condition%22 , %22Race%22);%0A        %7D%0A        testEvent.eventName = testEventName;%0A
     %7D%0A%0A%0A%0A   
@@ -2270,16 +2270,19 @@
      */%0A
+%0A%0A%0A
         
@@ -2287,432 +2287,8 @@
   //
-this to set delegate/listener back to this class%0A        Event2 testEvent = new Event2();%0A%0A        ArrayList testResultsArray = new ArrayList%3CString%3E();%0A        try %7B%0A            testResultsArray = testEvent.execute().get();%0A        %7D%0A        catch(InterruptedException ie)%7B%0A            Log.d(%22Race%22 , %22Condition%22);%0A        %7D%0A        catch (ExecutionException ee)%7B%0A            Log.d(%22Condition%22 , %22Race%22);%0A        %7D%0A        
 test

Put the Async call outside of getview for better runtime. Going to clean everything up for next commit