package com.thebluealliance.androidclient.fragments.event;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.thebluealliance.androidclient.Constants;
import com.thebluealliance.androidclient.R;
import com.thebluealliance.androidclient.activities.RefreshableHostActivity;
import com.thebluealliance.androidclient.activities.TeamAtEventActivity;
import com.thebluealliance.androidclient.adapters.EventStatsFragmentAdapter;
import com.thebluealliance.androidclient.adapters.ListViewAdapter;
import com.thebluealliance.androidclient.background.event.PopulateEventStats;
import com.thebluealliance.androidclient.helpers.TeamHelper;
import com.thebluealliance.androidclient.interfaces.RefreshListener;
import com.thebluealliance.androidclient.listitems.ListElement;

/**
 * Fragment that displays the team statistics for an FRC event.
 *
 * @author Phil Lopreiato
 * @author Bryce Matsuda
 * @author Nathan Walters
 *         <p/>
 *         File created by phil on 4/22/14.
 */
public class EventStatsFragment extends Fragment implements RefreshListener {

    private AlertDialog statsDialog;
    private String[] items;

    private Activity parent;

    private String mEventKey;
    private static final String KEY = "eventKey";

    private Parcelable mListState;
    private EventStatsFragmentAdapter mAdapter;
    private ListView mListView;
    private ProgressBar mProgressBar;
    private static String statSortCategory;

    private PopulateEventStats mTask;

    /**
     * Creates new event stats fragment for an event.
     *
     * @param eventKey key that represents an FRC event
     * @return new event stats fragment.
     */
    public static EventStatsFragment newInstance(String eventKey) {
        EventStatsFragment f = new EventStatsFragment();
        Bundle data = new Bundle();
        data.putString(KEY, eventKey);
        f.setArguments(data);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Reload key if returning from another fragment/activity
        if (getArguments() != null) {
            mEventKey = getArguments().getString(KEY, "");
        }
        parent = getActivity();
        if (parent instanceof RefreshableHostActivity) {
            ((RefreshableHostActivity) parent).registerRefreshableActivityListener(this);
        }

        // Setup stats sort dialog box
        items = getResources().getStringArray(R.array.statsDialogArray);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.dialog_stats_title)
               .setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                switch (items[i]) {
                    case "OPR":
                        statSortCategory = "opr";
                        break;
                    case "DPR":
                        statSortCategory = "dpr";
                        break;
                    case "CCWM":
                        statSortCategory = "ccwm";
                        break;
                    case "Team #":
                        statSortCategory = "team";
                        break;
                    default:
                        break;
                }

                mAdapter = (EventStatsFragmentAdapter) mListView.getAdapter();
                if (mAdapter != null && statSortCategory != null) {
                    mAdapter.sortStats(mAdapter, statSortCategory);
                }
            }
        }).setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
            }
        });

        statsDialog = builder.create();

        // Required for settings menu to be displayed.
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.stats_help_menu, menu);
        inflater.inflate(R.menu.stats_sort_menu, menu);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Setup views & listeners
        View view = inflater.inflate(R.layout.list_view_with_spinner, null);
        mListView = (ListView) view.findViewById(R.id.list);
        mProgressBar = (ProgressBar) view.findViewById(R.id.progress);

        // Either reload data if returning from another fragment/activity
        // Or get data if viewing fragment for the first time.
        if (mAdapter != null) {
            mListView.setAdapter(mAdapter);
            mListView.onRestoreInstanceState(mListState);
            mProgressBar.setVisibility(View.GONE);
        }

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String teamKey = ((ListElement) ((ListViewAdapter) adapterView.getAdapter()).getItem(position)).getKey();
                if (TeamHelper.validateTeamKey(teamKey) ^ TeamHelper.validateMultiTeamKey(teamKey)) {
                    if (TeamHelper.validateMultiTeamKey(teamKey)) {
                        // Take out extra letter at end to make team key valid.
                        teamKey = teamKey.substring(0, teamKey.length() - 1);
                    }
                    startActivity(TeamAtEventActivity.newInstance(getActivity(), mEventKey, teamKey));
                } else {
                    throw new IllegalArgumentException("OnItemClickListener must be attached to a view with a valid team key set as the tag!");
                }
            }
        });

        return view;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_sort_by){
            statsDialog.show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onPause() {
        // Save the data if moving away from fragment.
        super.onPause();
        if (mTask != null) {
            mTask.cancel(false);
        }
        if (mListView != null) {
            mAdapter = (EventStatsFragmentAdapter) mListView.getAdapter();
            mListState = mListView.onSaveInstanceState();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (parent instanceof RefreshableHostActivity) {
            ((RefreshableHostActivity) parent).startRefresh(this);
        }
    }

    @Override
    public void onRefreshStart() {
        Log.i(Constants.REFRESH_LOG, "Loading " + mEventKey + " stats");
        mTask = new PopulateEventStats(this, true, statSortCategory);
        mTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mEventKey);
    }

    @Override
    public void onRefreshStop() {
        if (mTask != null) {
            mTask.cancel(false);
        }
    }

    public void updateTask(PopulateEventStats newTask) {
        mTask = newTask;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((RefreshableHostActivity) parent).deregisterRefreshableActivityListener(this);
    }
}

@@ -1886,23 +1886,16 @@
 private 
-static 
 String s
@@ -1910,16 +1910,54 @@
 ategory;
+%0A    private int selectedStatSort = 0;
 %0A%0A    pr
@@ -3160,19 +3160,49 @@
 .set
-Items(items
+SingleChoiceItems(items, selectedStatSort
 , ne
@@ -3331,24 +3331,62 @@
 e, int i) %7B%0A
+                selectedStatSort = i;%0A
             
@@ -3938,32 +3938,76 @@
              %7D%0A%0A
+                dialogInterface.dismiss();%0A%0A
                 

make stats dialog single choice list (with radio buttons)