package com.abbidot.tracker.widget.calender;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.abbidot.tracker.R;

import java.util.List;


/**
 * Created by user999 on 2016/6/7.
 */
public class DayAdapter extends RecyclerView.Adapter<DayAdapter.ViewHolder> {

    private List<DateItem> datas;
    private Context context;
    private int dayTextColorSelected;
    private int dayTextColorNormal;
    private int daySelectionColor;
    private int weekTextColor;
    private OnItemClick itemClick;
    private int pressShapeSelectorId;
    private int todayPointColor;

    public DayAdapter(Context context, List<DateItem> datas, int dayTextColorSelected,
                      int dayTextColorNormal, int daySelectionColor, int weekTextColor,
                      int pressShapeSelectorId, int todayPointColor) {
        this.datas = datas;
        this.context = context;
        this.dayTextColorSelected = dayTextColorSelected;
        this.dayTextColorNormal = dayTextColorNormal;
        this.daySelectionColor = daySelectionColor;
        this.weekTextColor = weekTextColor;
        this.pressShapeSelectorId = pressShapeSelectorId;
        this.todayPointColor = todayPointColor;
    }

    public void setDatas(List<DateItem> datas) {
        this.datas = datas;
    }

    public List<DateItem> getData() {
        return datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.list_item_day, null);
        ViewUtil.scaleContentView(view.findViewById(R.id.list_item_root_layout));
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        DateItem item = datas.get(position);
        int week = item.getWeek();
        holder.tvWeek.setTextSize(12);
        switch (week) {
            case 0:
                holder.tvWeek.setText("SUN");
                break;
            case 1:
                holder.tvWeek.setText("MON");
                break;
            case 2:
                holder.tvWeek.setText("TUE");
                break;
            case 3:
                holder.tvWeek.setText("WED");
                break;
            case 4:
                holder.tvWeek.setText("THU");
                break;
            case 5:
                holder.tvWeek.setText("FRI");
                break;
            case 6:
                holder.tvWeek.setText("SAT");
                break;
        }
        GradientDrawable p = (GradientDrawable) holder.tvDay.getBackground();
        if (p != null) {
            if (item.isSelected()) {
                p.setColor(daySelectionColor);
            } else {
                p.setColor(ContextCompat.getColor(context, R.color.sleep_time_gray_color));
            }
        } else {
        }
        GradientDrawable t = (GradientDrawable) holder.tvPoint.getBackground();
        if (t != null) {
            if (item.isCurrent()) {
                t.setColor(todayPointColor);
            } else {
                t.setColor(0x00000000);
            }
        } else {
        }

        holder.rootView.setBackgroundResource(pressShapeSelectorId);
        holder.tvWeek.setTextColor(weekTextColor);
        holder.tvDay.setText(item.getDay() + "");

        holder.tvPoint.setVisibility(item.isCurrent() ? View.VISIBLE : View.INVISIBLE);

        if (item.isFuture()) {
            holder.tvDay.setTextColor(ContextCompat.getColor(context,
                    R.color.sleep_time_gray_color2));
            holder.rootView.setOnClickListener(null);
        } else {
            holder.tvDay.setTextColor(item.isSelected() ? dayTextColorSelected :
                    dayTextColorNormal);
            holder.rootView.setOnClickListener(v -> {
                if (itemClick != null) {
                    itemClick.onItemClick(datas.get(position).getYear(),
                            datas.get(position).getMonth(), datas.get(position).getDay(),
                            datas.get(position).getWeek());
                }
            });
        }
    }

    public void setPressShapeSelectorId(int pressShapeSelectorId) {
        this.pressShapeSelectorId = pressShapeSelectorId;
        notifyDataSetChanged();
    }

    public void setDayTextColorSelected(int dayTextColorSelected) {
        this.dayTextColorSelected = dayTextColorSelected;
        notifyDataSetChanged();
    }

    public void setDayTextColorNormal(int dayTextColorNormal) {
        this.dayTextColorNormal = dayTextColorNormal;
        notifyDataSetChanged();
    }

    public void setDaySelectionColor(int daySelectionColor) {
        this.daySelectionColor = daySelectionColor;
        notifyDataSetChanged();
    }

    public void setWeekTextColor(int weekTextColor) {
        this.weekTextColor = weekTextColor;
        notifyDataSetChanged();
    }

    public void setTodayPointColor(int todayPointColor) {
        this.todayPointColor = todayPointColor;
        notifyDataSetChanged();
    }

    public void setItemClick(OnItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface OnItemClick {
        void onItemClick(int year, int month, int day, int week);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public TextView tvWeek;
        public TextView tvDay;
        public View tvPoint;

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.list_item_root_layout);
            tvWeek = itemView.findViewById(R.id.item_week);
            tvDay = itemView.findViewById(R.id.item_day);
            tvPoint = itemView.findViewById(R.id.item_point);
        }
    }
}
