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.calendarapp.R;
import com.revoapp.calendar.helper.SimpleDialogItem;
import com.revoapp.calendar.helper.ThemeHelper;

import java.util.ArrayList;

public class SimpleDialogAdapter extends ArrayAdapter<SimpleDialogItem> {
    private final int resourceId;
    private LayoutInflater inflater;
    private Context context;

    public SimpleDialogAdapter(Context context, int textViewResourceId,
                               ArrayList<SimpleDialogItem> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        resourceId = textViewResourceId;
        inflater = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SimpleDialogItem item = (SimpleDialogItem) getItem(position);
        if (item == null) {
            return null;
        }

        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(resourceId, null);

            convertView.setBackgroundResource(ThemeHelper.get_component_menu_item());

            holder = new ViewHolder();

            holder.label = (TextView) convertView
                    .findViewById(R.id.dialogTitle);

            holder.label.setTextColor(context.getResources().getColor(ThemeHelper.get_foreground_color()));

            holder.image = (ImageView) convertView
                    .findViewById(R.id.incommingCallPhotoIV);

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

        holder.label.setText(item.getLabel());
        if (item.getDrawable() != 0)
            holder.image.setImageResource(item.getDrawable());
        if (item.getShape() != 0 && item.getBackground() != 0) {
            holder.image = (ImageView) convertView
                    .findViewById(R.id.boxPillPhotoIV);
            holder.label.setTextSize(30);
            holder.image.setBackgroundResource(item.getBackground());
            holder.image.setImageResource(item.getShape());
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView image;
        TextView label;
    }
}