package com.abbidot.tracker.widget.calender;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.abbidot.baselibrary.util.Utils;
import com.abbidot.tracker.R;
import com.hjq.toast.Toaster;

import java.util.List;


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

    private List<DateItem> datas;
    private Context context;
    private int dayTextColorSelected;
    private int dayTextColorNormal;
    private int daySelectionColor;
    private int daySelectionDrawable = 0;
    private int weekTextColor;
    private OnItemClick itemClick;
    private int pressShapeSelectorId;
    private int todayPointColor;
    private int todayBgDrawable = 0;
    private int todayTextColor = 0;

    public DayV2Adapter(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;
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        DateItem item = datas.get(position);

        String dayTime = item.getYear() + "-" + item.getMonth() + "-" + item.getDay();
        String week = Utils.Companion.stringToDate(dayTime, Utils.DATE_FORMAT_PATTERN_CN,
                Utils.WEEK_FORMAT_PATTERN);
        holder.tvWeek.setTextSize(12);
        holder.tvWeek.setText(week);
        // int week = item.getWeek();
        // 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.transparent));
        //     }
        // } else {
        // }
        if (item.isSelected()) {
            if (item.isCurrent()) {
                //不显示"今天"
                // holder.tvCurrentDay.setVisibility(View.VISIBLE);
                // holder.tvDay.setVisibility(View.INVISIBLE);
                // holder.tvWeek.setVisibility(View.INVISIBLE);
                holder.tvCurrentDay.setVisibility(View.GONE);
                holder.tvDay.setVisibility(View.VISIBLE);
                holder.tvWeek.setVisibility(View.VISIBLE);
            } else {
                holder.tvCurrentDay.setVisibility(View.GONE);
                holder.tvDay.setVisibility(View.VISIBLE);
                holder.tvWeek.setVisibility(View.VISIBLE);
            }
            if (daySelectionDrawable == 0) {
                holder.tvDay.setBackgroundResource(R.drawable.shape22_green_yellow_bg);
            } else
                holder.tvDay.setBackgroundResource(daySelectionDrawable);
        } else {
            //"日"默认的背景颜色
            holder.tvDay.setBackgroundResource(R.color.transparent);

            holder.tvCurrentDay.setVisibility(View.GONE);
            holder.tvDay.setVisibility(View.VISIBLE);
            holder.tvWeek.setVisibility(View.VISIBLE);
        }


        // 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(Utils.Companion.fill2Digits(item.getDay()));

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

        holder.rootView.setOnClickListener(v -> {
            if (item.isFuture()) {
                Toaster.show(R.string.txt_time_out_of_range);
            } else {
                if (null != itemClick) {
                    itemClick.onItemClick(item.getYear(), item.getMonth(), item.getDay(),
                            item.getWeek());
                }
            }
        });

        if (item.isFuture()) {
            holder.tvDay.setTextColor(ContextCompat.getColor(context,
                    R.color.sleep_time_gray_color2));
        } else {
            holder.tvDay.setTextColor(item.isSelected() ? dayTextColorSelected :
                    dayTextColorNormal);
        }

        if (todayBgDrawable > 0) {
            holder.tvCurrentDay.setBackgroundResource(todayBgDrawable);
        } else {

        }
        if (todayTextColor > 0) {
            holder.tvCurrentDay.setTextColor(ContextCompat.getColor(context, todayTextColor));
        } else {

        }
    }

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

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.list_item_root_layout_v2);
            tvWeek = itemView.findViewById(R.id.tv_item_calender_week);
            tvDay = itemView.findViewById(R.id.tv_item_calender_day);
            tvCurrentDay = itemView.findViewById(R.id.tv_item_calender_today);
            // tvPoint = itemView.findViewById(R.id.item_point);
        }
    }

    public void setDaySelectionDrawable(int selectionDrawable) {
        if (selectionDrawable > 0) {
            daySelectionDrawable = selectionDrawable;
            notifyDataSetChanged();
        }
    }

    public void setTodaySelectionBgTextColor(int selectionDrawable, int textColor) {
        todayBgDrawable = selectionDrawable;
        todayTextColor = textColor;
        notifyDataSetChanged();
    }
}
