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.TextView;

import com.revoapp.calendarapp.R;
import com.revoapp.calendar.helper.SimpleDialogItem;
import com.revoapp.calendar.helper.ThemeHelper;

import java.util.ArrayList;

public class DayNameAdapter extends ArrayAdapter<SimpleDialogItem> {
    private LayoutInflater inflater;
    private int resourceId;

    public DayNameAdapter(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;

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

            holder = new ViewHolder();

            holder.dayTV = (TextView) convertView
                    .findViewById(R.id.month_day_name);

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

        if ((position + 1) % 7 == 0 && position > 1) {
            holder.dayTV.setTextColor(convertView.getResources().getColor(ThemeHelper.get_cell_sunday_color()));
        }

        if (item.isCurrentMonth())
            holder.dayTV.setBackgroundResource(ThemeHelper.get_component_cell_on());
        else
            holder.dayTV.setBackgroundResource(ThemeHelper.get_component_cell_off());

        if (item.isCurrentDay())
            holder.dayTV.setTextColor(getContext().getResources().getColor(ThemeHelper.get_cell_today_color()));

        holder.dayTV.setText(item.getLabel());

        return convertView;
    }

    private class ViewHolder {
        TextView dayTV;
    }
}