package com.cooleshow.teacher.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
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.WeekView;

/**
 * 简单周视图
 * Created by huanghaibin on 2017/11/29.
 */

public class SimpleWeekView extends WeekView {
    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 SimpleWeekView(Context context) {
        super(context);
        this.context = context;
        setLayerType(View.LAYER_TYPE_SOFTWARE, mSelectedPaint);
        setBackgroundColor(Color.WHITE);
        maxSelectBgWidth = SizeUtils.dp2px(32);
        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 = SizeUtils.dp2px(2);
        mSchemePaint.setStyle(Paint.Style.STROKE);
    }

    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;
    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;
        RectF rect = buildRect(x, 0);
        canvas.drawRoundRect(rect, mRadius, mRadius, mSelectedPaint);
        return false;
    }

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

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

        if (isSelected) {
            canvas.drawText(calendar.isCurrentDay() ? "今" : String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    mSelectTextPaint);

            if (hasScheme) {
                mSchemeTextPaint.setTextSize(schemeTextSize);
                mSchemeTextPaint.setColor(schemeTextColorSelect);
                canvas.drawText("有课", cx, 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, mItemHeight - spaceMarginBottom, mSchemeTextPaint);
        } else {//最好绘制普通文本
            canvas.drawText(calendar.isCurrentDay() ? "今" : String.valueOf(calendar.getDay()), cx, baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }

    }

    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);
    }
}
