package org.deke.risk.riskahead.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.deke.risk.riskahead.R;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class Top10ListAdapter extends BaseAdapter {

    Context context;
    List<JSONObject> data;

    private static LayoutInflater inflater = null;

    public Top10ListAdapter(Context context, List<JSONObject> data) {
        this.context = context;
        this.data = data;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null) vi = inflater.inflate(R.layout.fragment_top10list_row, null);

        TextView rank = (TextView) vi.findViewById(R.id.txt_top10_rank);
        TextView name = (TextView) vi.findViewById(R.id.txt_top10_name);
        TextView numberOfIncidents = (TextView) vi.findViewById(R.id.txt_top10_numberOfIncidents);
        TextView memberSince = (TextView) vi.findViewById(R.id.txt_top10_membersince);
        TextView points = (TextView) vi.findViewById(R.id.txt_top10_points);

        try {
            rank.setText("#"+data.get(position).getString("rank"));
            name.setText(data.get(position).getString("username"));
            name.setTypeface(null, Typeface.BOLD);
            points.setText(data.get(position).getString("points"));

            numberOfIncidents.setText(data.get(position).getString("numberOfPosts")+" reported incidents");
            memberSince.setText("Member since "+data.get(position).getString("created_at").split(" ")[0]);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return vi;
    }
}