/*
 * Copyright 2013 Benjamin Linskey
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.benlinskey.greekreference.lexicon;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.benlinskey.greekreference.R;
import com.benlinskey.greekreference.data.lexicon.LexiconContract;

/**
 * A list fragment representing a list of Items. This fragment
 * also supports tablet devices by allowing list items to be given an
 * 'activated' state upon selection. This helps indicate which item is
 * currently being viewed in a {@link LexiconDetailFragment}.
 * <p>
 * Activities containing this fragment MUST implement the {@link Callbacks}
 * interface.
 */
public class LexiconBrowseListFragment extends LexiconListFragment
        implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final String TAG = "LexiconBrowseListFragment";
    public final static String NAME = "lexicon_browse";
    SimpleCursorAdapter mAdapter;
    static final String[] PROJECTION
            = new String[] {LexiconContract._ID, LexiconContract.COLUMN_GREEK_FULL_WORD};
    static final String SELECTION = "";
    static final String[] SELECTION_ARGS = {};

    /**
     * The fragment's current callback object, which is notified of list item
     * clicks.
     */
    private Callbacks mCallbacks = sDummyCallbacks;

    /**
     * The current activated item position. Only used on tablets.
     */
    private int mActivatedPosition = ListView.INVALID_POSITION;

    /**
     * A dummy implementation of the {@link Callbacks} interface that does
     * nothing. Used only when this fragment is not attached to an activity.
     */
    private static Callbacks sDummyCallbacks = new Callbacks() {
        @Override
        public void onItemSelected(String fragmentName, int id) {
        }
    };

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public LexiconBrowseListFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // TODO: Add progress indicator.

        // Create and set list adapter.
        // TODO: Replace this with a more efficient adapter.
        String[] fromColumns = {LexiconContract.COLUMN_GREEK_FULL_WORD};
        int[] toViews = {android.R.id.text1};
        mAdapter = new SimpleCursorAdapter(getActivity(),
                R.layout.greek_simple_list_item_activated_1, null, fromColumns, toViews, 0);
        setListAdapter(mAdapter);
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(getActivity(), LexiconContract.CONTENT_URI, PROJECTION, SELECTION,
                SELECTION_ARGS, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getListView().setFastScrollEnabled(true);
        getListView().setFastScrollAlwaysVisible(true);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // Activities containing this fragment must implement its callbacks.
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        mCallbacks = (Callbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();

        // Reset the active callbacks interface to the dummy implementation.
        mCallbacks = sDummyCallbacks;
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);
        setSelectedLexiconItemId(position);
        mCallbacks.onItemSelected(NAME, position + 1); // Positions are off by one from database ID.
    }

    private void setSelectedLexiconItemId(int id) {
        mSelectedLexiconId = id + 1;
    }

    public void selectItem(int id) {
        Log.w(TAG, "Item selected: " + id);
        mSelectedLexiconId = id;
        int position = id - 1;
        setActivatedPosition(position);
        getListView().setSelection(position);
        mCallbacks.onItemSelected(NAME, position);

    }
}

@@ -880,44 +880,210 @@
 iew.
-View;%0Aimport android.widget.ListView
+Gravity;%0Aimport android.view.View;%0Aimport android.view.ViewGroup;%0Aimport android.widget.FrameLayout;%0Aimport android.widget.LinearLayout;%0Aimport android.widget.ListView;%0Aimport android.widget.ProgressBar
 ;%0Aim
@@ -3062,508 +3062,8 @@
 );%0A%0A
-        // TODO: Add progress indicator.%0A%0A        // Create and set list adapter.%0A        // TODO: Replace this with a more efficient adapter.%0A        String%5B%5D fromColumns = %7BLexiconContract.COLUMN_GREEK_FULL_WORD%7D;%0A        int%5B%5D toViews = %7Bandroid.R.id.text1%7D;%0A        mAdapter = new SimpleCursorAdapter(getActivity(),%0A                R.layout.greek_simple_list_item_activated_1, null, fromColumns, toViews, 0);%0A        setListAdapter(mAdapter);%0A        getLoaderManager().initLoader(0, null, this);
 %0A   
@@ -3661,24 +3661,1001 @@
 nceState);%0A%0A
+        // Create a progress bar to display while the list loads%0A        ProgressBar progressBar = new ProgressBar(getActivity());%0A        FrameLayout.LayoutParams params%0A                = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,%0A                    ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);%0A        progressBar.setLayoutParams(params);%0A        progressBar.setIndeterminate(true);%0A        getListView().setEmptyView(progressBar);%0A        ((ViewGroup) view).addView(progressBar);%0A%0A        // Create and set list adapter.%0A        // TODO: Replace this with a more efficient adapter.%0A        String%5B%5D fromColumns = %7BLexiconContract.COLUMN_GREEK_FULL_WORD%7D;%0A        int%5B%5D toViews = %7Bandroid.R.id.text1%7D;%0A        mAdapter = new SimpleCursorAdapter(getActivity(),%0A                R.layout.greek_simple_list_item_activated_1, null, fromColumns, toViews, 0);%0A        setListAdapter(mAdapter);%0A        getLoaderManager().initLoader(0, null, this);%0A%0A
         getL

Add progress indicator to lexicon browse list