package com.example.providers;

import android.content.ContentUris;
import android.content.Context;
import android.content.res.Configuration;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class CallLogActivity extends FragmentActivity {
  private static final Uri uri = CallLog.Calls.CONTENT_URI;
  private static final int CALLLOG_LOADER = 1;
  private static final String[] columns = new String[] {
      CallLog.Calls._ID,
      CallLog.Calls.DATE,
      CallLog.Calls.NEW,
      CallLog.Calls.TYPE,
      CallLog.Calls.NUMBER,
      CallLog.Calls.DURATION,
      CallLog.Calls.CACHED_NAME,
      CallLog.Calls.CACHED_NUMBER_LABEL,
      CallLog.Calls.CACHED_NUMBER_TYPE
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
      finish();
      return;
    }

    if (savedInstanceState == null) {
      CallLogListFragment callLog = new CallLogListFragment();
      callLog.setArguments(getIntent().getExtras());
      getSupportFragmentManager().beginTransaction()
          .add(android.R.id.content, callLog).commit();
    }
  }

  public static class CallLogListFragment extends ListFragment implements
      LoaderManager.LoaderCallbacks<Cursor> {

    private CallLogCursorAdapter clca;

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      getLoaderManager().initLoader(CALLLOG_LOADER, null, this);
      clca = new CallLogCursorAdapter((Context) getActivity(), null);
      setListAdapter(clca);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
      CursorLoader loader = new CursorLoader(getActivity(), uri, columns, null,
          null, CallLog.Calls.DATE + " DESC");
      return loader;
    }

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

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

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
      super.onListItemClick(l, v, position, id);
      Uri itemUri = ContentUris.withAppendedId(uri, id);
      Cursor c = getActivity().getContentResolver().query(itemUri, columns,
          null, null, null);
      if (c.moveToFirst()) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < c.getColumnCount(); i++) {
          builder.append(c.getColumnName(i)).append(": ")
              .append(c.getString(i)).append("\n");
        }
        Toast.makeText(getActivity(), builder.toString(), Toast.LENGTH_LONG)
            .show();
      }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      super.onOptionsItemSelected(item);
      switch (item.getItemId()) {
        default:
          return false;
      }
    }
  }

}