package com.thebluealliance.androidclient.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.thebluealliance.androidclient.R;
import com.thebluealliance.androidclient.activities.ViewEventActivity;
import com.thebluealliance.androidclient.adapters.ListViewAdapter;
import com.thebluealliance.androidclient.background.PopulateEventList;
import com.thebluealliance.androidclient.datatypes.ListElement;

/**
 * File created by phil on 4/20/14.
 */
public class EventListFragment extends Fragment {

    public static final String YEAR = "YEAR";
    public static final String WEEK = "WEEK";
    public static final String TEAM_KEY = "TEAM_KEY";

    private int mYear;
    private int mWeek;
    private String mTeamKey;

    private Parcelable mListState;
    private ListViewAdapter mAdapter;
    private ListView mListView;
    private ProgressBar mProgressBar;

    private PopulateEventList mTask;

    public static EventListFragment newInstance(int year, int week, String teamKey) {
        EventListFragment f = new EventListFragment();
        Bundle args = new Bundle();
        args.putInt(YEAR, year);
        args.putInt(WEEK, week);
        args.putString(TEAM_KEY, teamKey);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mYear = getArguments().getInt(YEAR, -1);
        mWeek = getArguments().getInt(WEEK, -1);
        mTeamKey = getArguments().getString(TEAM_KEY);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.list_fragment_with_spinner, null);
        mListView = (ListView) v.findViewById(R.id.list);
        mProgressBar = (ProgressBar) v.findViewById(R.id.progress);
        if (mAdapter != null) {
            mListView.setAdapter(mAdapter);
            mListView.onRestoreInstanceState(mListState);
            mProgressBar.setVisibility(View.GONE);
        } else {
            mTask = new PopulateEventList(this, mYear, mWeek, mTeamKey);
            mTask.execute();
        }
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ViewEventActivity.class);
                String eventKey = ((ListElement) ((ListViewAdapter) parent.getAdapter()).getItem(position)).getKey();
                intent.putExtra("eventKey", eventKey);
                startActivity(intent);
            }
        });
        return v;
    }

    @Override
    public void onPause() {
        super.onPause();
        mTask.cancel(false);
        if (mListView != null) {
            mAdapter = (ListViewAdapter) mListView.getAdapter();
            mListState = mListView.onSaveInstanceState();
        }
    }
}

@@ -170,24 +170,49 @@
 p.Fragment;%0A
+import android.util.Log;%0A
 import andro
@@ -395,16 +395,68 @@
 ssBar;%0A%0A
+import com.thebluealliance.androidclient.Constants;%0A
 import c
@@ -2732,182 +2732,871 @@
     
-Intent intent = new Intent(getActivity(), ViewEventActivity.class);%0A                String eventKey = ((ListElement) ((ListViewAdapter) parent.getAdapter()).getItem(position)
+if(!(parent.getAdapter() instanceof ListViewAdapter))%7B%0A                    //safety check. Shouldn't ever be tripped unless someone messed up in code somewhere%0A                    Log.w(Constants.LOG_TAG, %22Someone done goofed. A ListView adapter doesn't extend ListViewAdapter. Try again...%22);%0A                    return;%0A                %7D%0A                Object item = ((ListViewAdapter) parent.getAdapter()).getItem(position);%0A                if(item != null && item instanceof ListElement) %7B%0A                    // only open up the view event activity if the user actually clicks on a ListElement%0A                    // (as opposed to something inheriting from ListHeader, which shouldn't do anything on user click%0A                    Intent intent = new Intent(getActivity(), ViewEventActivity.class);%0A                    String eventKey = ((ListElement) item
 ).ge
@@ -3599,24 +3599,28 @@
 ).getKey();%0A
+    
             
@@ -3678,16 +3678,20 @@
         
+    
 startAct
@@ -3701,24 +3701,144 @@
 ty(intent);%0A
+                %7Delse%7B%0A                    Log.d(Constants.LOG_TAG, %22ListHeader clicked. Ignore...%22);%0A                %7D%0A
             

fix #66