package com.example.sleep.calendarInfo;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sleep.R;
import com.ldf.calendar.Utils;
import com.ldf.calendar.component.State;
import com.ldf.calendar.interf.IDayRenderer;
import com.ldf.calendar.model.CalendarDate;
import com.ldf.calendar.view.DayView;

import java.util.Objects;

public class CustomDayView extends DayView {

    private final CalendarDate today = new CalendarDate();//当前日期
    private TextView dateTv;//日期文本显示
    private ImageView marker;//标记图标
    private View selectedBackground;//选中背景
    private View todayBackground;//今天的背景


    public CustomDayView(Context context, int layoutResource) {
        super(context, layoutResource);
        dateTv = findViewById(R.id.date);//找到日期文本视图
        marker = findViewById(R.id.maker);// 找到标记图标视图
        selectedBackground = findViewById(R.id.selected_background); // 找到选中背景视图
        todayBackground = findViewById(R.id.today_background); // 找到今天的背景视图
    }

    @Override
    public void refreshContent() {
        super.refreshContent();// 刷新日历内容
        renderToday(day.getDate()); // 渲染今天的日期
        renderSelect(day.getState());// 渲染选中状态
        renderMarker(day.getDate(), day.getState());// 渲染标记
        dateTv.setTextSize(19);// 设置日期文本大小
        super.refreshContent();// 再次刷新日历内容
    }

    // 渲染标记的显示
    private void renderMarker(CalendarDate date, State state) {
        if (Utils.loadMarkData().containsKey(date.toString())) {
            if (state == State.SELECT || date.toString().equals(today.toString())) {
                marker.setVisibility(GONE);
            } else {
                marker.setVisibility(VISIBLE);
                if (Objects.equals(Utils.loadMarkData().get(date.toString()), "0")) {
                    marker.setEnabled(true);
                } else {
                    marker.setEnabled(false);
                }
            }
        } else {
            marker.setVisibility(GONE);
        }
    }

    // 渲染选中状态
    private void renderSelect(State state) {
        if (state == State.SELECT) {
            selectedBackground.setVisibility(View.VISIBLE);
            dateTv.setTextColor(Color.BLACK);
        } else if (state == State.NEXT_MONTH || state == State.PAST_MONTH) {
            selectedBackground.setVisibility(GONE);
            dateTv.setTextColor(Color.parseColor("#3666a2"));
        } else {
            selectedBackground.setVisibility(GONE);
            dateTv.setTextColor(Color.WHITE);
        }
    }

    // 渲染今天的日期显示
    private void renderToday(CalendarDate date) {
        if (date != null) {
            if (date.equals(today)) {
                dateTv.setText("今");
                dateTv.setTextColor(Color.BLACK);
                //todayBackground.setVisibility(VISIBLE);
            } else {
                dateTv.setText(date.day + "");
                todayBackground.setVisibility(GONE);
            }
        }
    }

    @Override
    // 复制当前的日历视图
    public IDayRenderer copy() {
        return new CustomDayView(context, layoutResource);
    }
}