package com.revoapp.calendar.adapter;

import android.content.Context;
import android.text.format.Time;
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 WeekAdapter extends ArrayAdapter<SimpleDialogItem> {
    private LayoutInflater inflater;
    private int resourceId;

    public WeekAdapter(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) {
        SimpleDialogItem item = getItem(position);
        if (item == null) {
            return null;
        }

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

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

            holder = new ViewHolder();
            holder.rl = (RelativeLayout) convertView
                    .findViewById(R.id.week_day_item_layout);
            holder.dayTV = (TextView) convertView
                    .findViewById(R.id.week_day);

            holder.dayTimeIV = (ImageView) convertView
                    .findViewById(R.id.week_day_time);

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

        holder.rl.setBackgroundResource(ThemeHelper.get_component_cell());

        if (item.getDrawable() != 0) {
            Time time = new Time();
            time.setToNow();
            int t = time.hour % 6;
            t = t * 60 + time.minute;
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(85, 7);
            lp.setMargins(0, t / 2, 0, 0);
            holder.dayTV.setBackgroundResource(ThemeHelper.get_component_cell_today());
        }

        int childs = holder.rl.getChildCount();
        holder.rl.removeViews(2, childs - 2);
        if (item.getIcons() != null && !item.getIcons().isEmpty()) {
            for (int i = 0; i < item.getIcons().size(); i++) {
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(30, 30);
                lp.setMargins(7 + i % 2 * 35, 7 + i / 2 * 35, 0, 0);
                ImageView iv = new ImageView(getContext());
                iv.setBackgroundResource(item.getIcons().get(i));
                holder.rl.addView(iv, lp);
            }
        }

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

        return convertView;
    }

    private class ViewHolder {
        TextView dayTV;
        ImageView dayTimeIV;
        RelativeLayout rl;
    }
}