package com.example.providers;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.provider.CallLog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CallLogCursorAdapter extends CursorAdapter {
  private final LayoutInflater inflater;
  private Drawable symIncoming;
  private Drawable symOutgoing;
  private Drawable symMissed;
  private int dateCol;
  private int durCol;
  private int numCol;
  private int typeCol;
  private SimpleDateFormat formatter = new SimpleDateFormat("EE HH:mm", Locale.US);
  private GregorianCalendar cal;
  
  public CallLogCursorAdapter(Context context, Cursor c) {
    super(context, c, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    inflater = LayoutInflater.from(context);
    symIncoming = context.getResources().getDrawable(android.R.drawable.sym_call_incoming);
    symOutgoing = context.getResources().getDrawable(android.R.drawable.sym_call_outgoing);
    symMissed = context.getResources().getDrawable(android.R.drawable.sym_call_missed);
    cal = new GregorianCalendar(TimeZone.getDefault());
  }

  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view = inflater.inflate(R.layout.call_history_item, null);
    return view;
  }

  @Override
  public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder)view.getTag();
    if (holder == null) {
      dateCol = cursor.getColumnIndex(CallLog.Calls.DATE);
      durCol = cursor.getColumnIndex(CallLog.Calls.DURATION);
      numCol = cursor.getColumnIndex(CallLog.Calls.NUMBER);
      typeCol = cursor.getColumnIndex(CallLog.Calls.TYPE);
      holder = new ViewHolder();
      holder.callDate = (TextView)view.findViewById(R.id.callDate);
      holder.callDuration = (TextView)view.findViewById(R.id.callDuration);
      holder.callNumber = (TextView)view.findViewById(R.id.callNumber);
      holder.callInOut = (ImageView)view.findViewById(R.id.callInOut);
      view.setTag(holder);
    }
    cal.setTimeInMillis(cursor.getLong(dateCol));
    holder.callDate.setText(formatter.format(cal.getTime()));
    holder.callDuration.setText(cursor.getString(durCol)+"s");
    switch (cursor.getInt(typeCol)) {
      case CallLog.Calls.INCOMING_TYPE:
        holder.callInOut.setImageDrawable(symIncoming);
      break;
      case CallLog.Calls.OUTGOING_TYPE:
        holder.callInOut.setImageDrawable(symOutgoing);
      break;
      case CallLog.Calls.MISSED_TYPE:
        holder.callInOut.setImageDrawable(symMissed);
      break;     
    }
    holder.callNumber.setText(cursor.getString(numCol));
  }

  private static final class ViewHolder {
    public TextView callDate;
    public TextView callDuration;
    public TextView callNumber;
    public ImageView callInOut;
  }
}
