package com.example.scores.utilities;

import java.util.ArrayList;

import android.content.Context;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.scores.R;

public class ScoreListAdapter extends BaseAdapter {
    private ArrayList<Parcelable> scores;
    private LayoutInflater inflater;

    public ScoreListAdapter(Context context,
            ArrayList<Parcelable> scores) {
        this.scores = scores;
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return scores.size();
    }

    public Object getItem(int position) {
        return scores.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView,
            ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row, null);
            holder = new ViewHolder();
            holder.score = (TextView) convertView
                    .findViewById(R.id.score);
            holder.details = (TextView) convertView
                    .findViewById(R.id.details);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        Score score = (Score)scores.get(position);
        holder.score.setText(score.getTeam1() + " " + 
          score.getTeam1Score() + ", " + score.getTeam2() + " " + 
          score.getTeam2Score());
        holder.details.setText(score.getDescription().trim());
        return convertView;
    }

    static class ViewHolder {
        TextView score;
        TextView details;
    }
}
