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.RelativeLayout;
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 MonthAdapter extends ArrayAdapter<SimpleDialogItem> {
    private LayoutInflater inflater;
    private int resourceId;
    private ArrayList<ImageView> ar;

    public MonthAdapter(Context context, int textViewResourceId,
                        ArrayList<SimpleDialogItem> objects) {
        super(context, textViewResourceId, objects);

        resourceId = textViewResourceId;
        inflater = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
    }

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

        ViewHolder holder;
        int dayTVHeigh = parent.getHeight()/6 - 2;

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

            holder = new ViewHolder();

            holder.dayTV = (TextView) convertView
                    .findViewById(R.id.month_day);
            holder.rl = (RelativeLayout) convertView.findViewById(R.id.month_rl);

            ar = new ArrayList<ImageView>();
            ar.add(holder.alarmIV = (ImageView) convertView
                    .findViewById(R.id.alarm));
            ar.add(holder.medicineIV = (ImageView) convertView
                    .findViewById(R.id.medicine));
            ar.add(holder.medicineIV = (ImageView) convertView
                    .findViewById(R.id.meal));

            holder.alarmIV.setMaxHeight(10);
            holder.alarmIV.setMaxWidth(10);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        for (ImageView iv : ar) {
            iv.setBackgroundDrawable(null);
        }

        if (item.getIcons() != null) {
            int number = 0;

            for (int i : item.getIcons()) {
                ar.get(number).setBackgroundResource(i);
                number++;
            }
        }

        if (!item.isCurrentMonth()) {
            holder.rl.setBackgroundResource(ThemeHelper.get_component_cell_prevmonth());
            holder.dayTV.setTextColor(convertView.getResources().getColor(R.color.gray));
        } else {
            holder.rl.setBackgroundResource(ThemeHelper.get_component_cell());
        }
        if ((position + 1) % 7 == 0 && position > 1 && item.isCurrentMonth()) {
            holder.dayTV.setTextColor(convertView.getResources().getColor(ThemeHelper.get_cell_sunday_color()));
        }
        if (item.isCurrentDay()) {
            holder.rl.setBackgroundResource(ThemeHelper.get_component_cell_today());
        }
        holder.dayTV.setText(item.getLabel());
        holder.dayTV.setHeight(dayTVHeigh);

        return convertView;
    }

    private class ViewHolder {
        TextView dayTV;
        RelativeLayout rl;
        ImageView alarmIV;
        ImageView medicineIV;
    }
}