package com.revoapp.calendar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.revoapp.api.database.calendar.pojo.Pills;
import com.revoapp.api.utils.DateTimeConvert;
import com.revoapp.calendarapp.R;
import com.revoapp.calendar.helper.DataHelper;

import java.util.ArrayList;

public class DayMedicineAdapter extends ArrayAdapter<Pills> {
    private LayoutInflater layoutInflater;
    public ArrayList<Integer> shapes;
    public ArrayList<Integer> colors;

    public DayMedicineAdapter(Context context, int textViewResourceId, ArrayList<Pills> pills) {
        super(context, textViewResourceId, pills);

        colors = new ArrayList<Integer>();
        colors = DataHelper.getCollection(DataHelper.get_color_array());
        shapes = new ArrayList<Integer>();
        shapes = DataHelper.getCollection(DataHelper.get_shape_array());
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = layoutInflater.inflate(
                    R.layout.day_medicine_row, null);
            //convertView.setBackgroundResource(R.drawable.b0_day_medicine_row);

            holder = new ViewHolder();

            holder.nameTV = (TextView) convertView
                    .findViewById(R.id.plii_name);
            holder.photoIV = (ImageView) convertView
                    .findViewById(R.id.pill_image);
            holder.pillTimeTV = (TextView) convertView.findViewById(R.id.pill_time);

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

        Pills item = getItem(position);
        if (item != null) {
            holder.photoIV.setBackgroundResource(colors.get(item.getmPillColor()));
            holder.photoIV.setImageResource(shapes.get(item.getmPillShape()));
            holder.nameTV.setText(item.getmQuantity() + "x " + item.getmPillName());

            holder.pillTimeTV.setText(DateTimeConvert.getTime(getContext(),
                    item.getmDateTime()));
        }

        return convertView;
    }

    private static class ViewHolder {
        private ImageView photoIV;
        private TextView nameTV, pillTimeTV;
    }
}