package com.example.checkregister;

import com.example.checkregister.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class CheckRegisterCursorAdapter extends CursorAdapter {
  private final LayoutInflater inflater;
  private static SharedPreferences prefs;
  private static String currencySymbol = "$";

  private final class ViewHolder {
    public TextView checkNum;
    public TextView date;
    public TextView payee;
    public TextView amount;
    public TextView balance;
    public TextView memo;
    public int checkNumIndex;
    public int dateIndex;
    public int payeeIndex;
    public int amountIndex;
    public int balanceIndex;
    public int memoIndex;
  }

  public CheckRegisterCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
    inflater = LayoutInflater.from(context);
    prefs = PreferenceManager.getDefaultSharedPreferences(context);
  }

  @Override
  public void bindView(View view, Context context, Cursor c) {
    currencySymbol = prefs.getString("currencySymbol", "$");

    /**
     * Without ViewHolder: TextView checkNum =
     * (TextView)view.findViewById(R.id.checkNumView); TextView date =
     * (TextView)view.findViewById(R.id.dateView); TextView payee =
     * (TextView)view.findViewById(R.id.payeeView); TextView amount =
     * (TextView)view.findViewById(R.id.amountView); TextView balance =
     * (TextView)view.findViewById(R.id.balanceView); TextView memo =
     * (TextView)view.findViewById(R.id.memoView);
     * checkNum.setText(c.getString(c
     * .getColumnIndex(CheckRegisterDBAdapter.KEY_CHECKNUM)));
     * date.setText(c.getString
     * (c.getColumnIndex(CheckRegisterDBAdapter.KEY_DATE)));
     * payee.setText(c.getString
     * (c.getColumnIndex(CheckRegisterDBAdapter.KEY_PAYEE)));
     * amount.setText(currencySymbol +
     * c.getString(c.getColumnIndex(CheckRegisterDBAdapter.KEY_AMOUNT)));
     * balance.setText(currencySymbol +
     * c.getString(c.getColumnIndex(CheckRegisterDBAdapter.KEY_BALANCE)));
     * memo.setText
     * (c.getString(c.getColumnIndex(CheckRegisterDBAdapter.KEY_MEMO)));
     **/

    /** With ViewHolder: **/
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder == null) {
      holder = new ViewHolder();
      holder.checkNum = (TextView) view.findViewById(R.id.checkNumView);
      holder.date = (TextView) view.findViewById(R.id.dateView);
      holder.payee = (TextView) view.findViewById(R.id.payeeView);
      holder.amount = (TextView) view.findViewById(R.id.amountView);
      holder.balance = (TextView) view.findViewById(R.id.balanceView);
      holder.memo = (TextView) view.findViewById(R.id.memoView);
      holder.checkNumIndex = c
          .getColumnIndex(CheckRegisterDBAdapter.KEY_CHECKNUM);
      holder.dateIndex = c.getColumnIndex(CheckRegisterDBAdapter.KEY_DATE);
      holder.payeeIndex = c.getColumnIndex(CheckRegisterDBAdapter.KEY_PAYEE);
      holder.amountIndex = c.getColumnIndex(CheckRegisterDBAdapter.KEY_AMOUNT);
      holder.balanceIndex = c
          .getColumnIndex(CheckRegisterDBAdapter.KEY_BALANCE);
      holder.memoIndex = c.getColumnIndex(CheckRegisterDBAdapter.KEY_MEMO);
      view.setTag(holder);
    }
    holder.checkNum.setText(c.getString(holder.checkNumIndex));
    holder.date.setText(c.getString(holder.dateIndex));
    holder.payee.setText(c.getString(holder.payeeIndex));
    holder.amount.setText(currencySymbol + c.getString(holder.amountIndex));
    holder.balance.setText(currencySymbol + c.getString(holder.balanceIndex));
    holder.memo.setText(c.getString(holder.memoIndex));
  }

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

}
