package com.cooleshow.teacher.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;

import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.teacher.R;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;

/**
 * 默认日历
 * Created by huanghaibin on 2017/11/15.
 */

public class SimpleMonthView extends MonthView {

    private int mRadius;
    //    private Paint mPaint = new Paint();
    Context context;
    private int maxSelectBgWidth;
    private int spaceMarginBottom;
    private int spaceMarginTop;
    private int schemeTextSize;
    private int schemeTextColor;
    private int schemeTextColorSelect;
    private Paint schemeBgPaint = new Paint();

    public SimpleMonthView(Context context) {
        super(context);
        this.context = context;
        //兼容硬件加速无效的代码
        setLayerType(View.LAYER_TYPE_SOFTWARE, mSelectedPaint);
        setBackgroundColor(Color.WHITE);
        maxSelectBgWidth = SizeUtils.dp2px(32);
        mRadius = SizeUtils.dp2px(2);
        spaceMarginBottom = SizeUtils.dp2px(5);
        spaceMarginTop = SizeUtils.dp2px(10);
        schemeTextSize = SizeUtils.sp2px(10);
        schemeTextColor = context.getResources().getColor(com.cooleshow.base.R.color.color_ff6363);
        schemeTextColorSelect = context.getResources().getColor(R.color.white);
        schemeBgPaint.setColor(context.getResources().getColor(com.cooleshow.base.R.color.color_30ffd7a6));
        schemeBgPaint.setAntiAlias(true);
        schemeBgPaint.setStyle(Paint.Style.FILL);
        //4.0以上硬件加速会导致无效
//        mSelectedPaint.setMaskFilter(new BlurMaskFilter(25, BlurMaskFilter.Blur.SOLID));
//
//        mPaint.setColor(0xFF01C1B5);
//        mPaint.setAntiAlias(true);
//        mPaint.setStrokeWidth(dipToPx(context,1));
//        mPaint.setFakeBoldText(true);
//        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onPreviewHook() {
//        mRadius = Math.min(mItemWidth, mItemHeight) / 2;
        mRadius = SizeUtils.dp2px(2);
        mSchemePaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onLoopStart(int x, int y) {

    }

    /**
     * 绘制选中的日子
     *
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param y         日历Card y起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return 返回true 则会继续绘制onDrawScheme，因为这里背景色不是是互斥的，所以返回true，返回false，则点击scheme标记的日子，则不继续绘制onDrawScheme，自行选择即可
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        RectF rect = buildRect(x, y);
        canvas.drawRoundRect(rect, mRadius, mRadius, mSelectedPaint);
        return false;
    }

    private RectF buildRect(int x, int y) {
        RectF rect = new RectF();
        rect.left = x + Math.abs(mItemWidth - maxSelectBgWidth) / 2;
        rect.right = x + mItemWidth - Math.abs(mItemWidth - maxSelectBgWidth) / 2;
        rect.top = y + spaceMarginTop;
        rect.bottom = y + mItemHeight;
        return rect;
    }

    /**
     * 绘制标记的事件日子
     *
     * @param canvas   canvas
     * @param calendar 日历calendar
     * @param x        日历Card x起点坐标
     * @param y        日历Card y起点坐标
     */

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        RectF rect = buildRect(x, y);
        canvas.drawRoundRect(rect, mRadius, mRadius, schemeBgPaint);
    }

    /**
     * 绘制文本
     *
     * @param canvas     canvas
     * @param calendar   日历calendar
     * @param x          日历Card x起点坐标
     * @param y          日历Card y起点坐标
     * @param hasScheme  是否是标记的日期
     * @param isSelected 是否选中
     */

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;

        if (isSelected) {//优先绘制选择的
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    mSelectTextPaint);
            if (hasScheme) {
                mSchemeTextPaint.setTextSize(schemeTextSize);
                mSchemeTextPaint.setColor(schemeTextColorSelect);
                canvas.drawText("有课", cx, y + mItemHeight - spaceMarginBottom, mSchemeTextPaint);
            }
        } else if (hasScheme) {//否则绘制具有标记的
            canvas.drawText(calendar.isCurrentDay() ? "今" : String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    getPaint(calendar));
            mSchemeTextPaint.setTextSize(schemeTextSize);
            mSchemeTextPaint.setColor(schemeTextColor);
            canvas.drawText("有课", cx, y + mItemHeight - spaceMarginBottom, mSchemeTextPaint);
        } else {//最好绘制普通文本
            canvas.drawText(calendar.isCurrentDay() ? "今" : String.valueOf(calendar.getDay()), cx, baselineY,
                    getPaint(calendar));
        }

    }

    private Paint getPaint(Calendar calendar) {
        if (calendar.isCurrentDay()) {
            //当前日期
            return mCurDayTextPaint;
        }
        if (calendar.isWeekend()) {
            return mCurDayTextPaint;
        }
        if (calendar.isCurrentMonth()) {
            return mCurMonthTextPaint;
        }
        return mOtherMonthTextPaint;
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
