package com.sheep.gamegroup.view.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;
import com.sheep.gamegroup.util.LogUtil;

import java.util.Locale;

/**
 * Created by realicing on 2018/9/12.
 * realicing@sina.com
 * 不要删除这个文件，否则不能正常运行，其在布局文件中有隐性引用
 */
public class CalendarMonthView2 extends MonthView {

    private Paint mTextPaint = new Paint();
    private Paint mCurDayPaint = new Paint();
    private Paint mSolarTermTextPaint = new Paint();
    private Paint mSchemeBasicPaint = new Paint();
    private float mPadding;
//    private float mRadio;
//    private float mSchemeBaseLine;

    public CalendarMonthView2(Context context) {
        super(context);
        this.mTextPaint.setTextSize((float) dipToPx(context, 8.0F));
        this.mTextPaint.setColor(-1);
        this.mTextPaint.setAntiAlias(true);
        this.mTextPaint.setFakeBoldText(true);
        this.mSchemeBasicPaint.setAntiAlias(true);
        this.mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        this.mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        this.mSchemeBasicPaint.setFakeBoldText(true);
        this.mCurDayPaint.setAntiAlias(true);
        this.mCurDayPaint.setStyle(Paint.Style.FILL);
        this.mCurDayPaint.setTextAlign(Paint.Align.CENTER);
        this.mCurDayPaint.setColor(Color.parseColor("#50CFCFCF"));
        this.mCurDayPaint.setFakeBoldText(true);
        this.mSolarTermTextPaint.setTextAlign(Paint.Align.CENTER);
        this.mSolarTermTextPaint.setColor(Color.parseColor("#4889FF"));
        this.mSolarTermTextPaint.setTextSize(mCurMonthTextPaint.getTextSize());
        this.mPadding = dipToPx(this.getContext(), 4.0F);
//        this.mRadio = (float) dipToPx(this.getContext(), 7.0F);
//        Paint.FontMetrics metrics = this.mSchemeBasicPaint.getFontMetrics();
//        this.mSchemeBaseLine = this.mRadio - metrics.descent + (metrics.bottom - metrics.top) / 2.0F + (float) dipToPx(this.getContext(), 1.0F);
    }

    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        this.mSelectedPaint.setStyle(Paint.Style.FILL);
//        canvas.drawRect((float) (x + this.mPadding), (float) (y + this.mPadding), (float) (x + this.mItemWidth - this.mPadding), (float) (y + this.mItemHeight - this.mPadding), this.mSelectedPaint);
        canvas.drawCircle(x + this.mItemWidth / 2.0F, y + this.mItemHeight / 2.0F, (Math.min(this.mItemHeight, this.mItemWidth) - this.mPadding) / 2.0F, this.mSelectedPaint);
        return true;
    }

    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        this.mSchemeBasicPaint.setColor(calendar.getSchemeColor());
//        canvas.drawRect((float) (x + this.mPadding), (float) (y + this.mPadding), (float) (x + this.mItemWidth - this.mPadding), (float) (y + this.mItemHeight - this.mPadding), this.mSchemeBasicPaint);
        canvas.drawRect((float) (x), y - this.mPadding / 2.0F, x + this.mItemWidth + (calendar.getWeek() == 0 ? this.mPadding : 0), y + this.mItemHeight - this.mPadding / 2.0F, this.mSchemeBasicPaint);
//        canvas.drawRoundRect((float) (x + this.mPadding), (float) (y + this.mPadding), (float) (x + this.mItemWidth - this.mPadding), (float) (y + this.mItemHeight - this.mPadding), this.mSchemeBasicPaint);
//        canvas.drawText(calendar.getScheme(), (float) (x + this.mItemWidth - this.mPadding) - this.mRadio / 2.0F - this.getTextWidth(calendar.getScheme()) / 2.0F, (float) (y + this.mPadding) + this.mSchemeBaseLine, this.mTextPaint);
    }

    private float getTextWidth(String text) {
        return this.mTextPaint.measureText(text);
    }

    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        int cx = x + this.mItemWidth / 2;
        int top = y - this.mItemHeight / 6;
        boolean isCurrentDay = calendar.isCurrentDay();
        if (isCurrentDay) {
            onDrawCurDay(canvas, calendar, x, y, hasScheme);
        }
        /*if (isSelected && !isCurrentDay) {
            canvas.drawText(String.valueOf(calendar.getDay()), (float) cx, this.mTextBaseLine + (float) top, this.mSelectTextPaint);
            canvas.drawText(calendar.getLunar(), (float) cx, this.mTextBaseLine + (float) y + (float) (this.mItemHeight / 10), this.mSelectedLunarTextPaint);
        } else */
        Paint textPaint = this.mCurMonthTextPaint;
        Paint lunarTextPaint = this.mCurMonthLunarTextPaint;
        if(isSelected) {
            textPaint = this.mSelectTextPaint;
            lunarTextPaint = this.mSelectedLunarTextPaint;
        } else if(calendar.isCurrentDay()){//当天
            textPaint = this.mCurDayTextPaint;
            lunarTextPaint = this.mCurDayLunarTextPaint;
        } else if(hasScheme){//标记的
            textPaint = this.mSchemeTextPaint;
            lunarTextPaint = this.mSchemeLunarTextPaint;
        } else if(!TextUtils.isEmpty(calendar.getSolarTerm())){//农历节庆
            textPaint = this.mSolarTermTextPaint;
            lunarTextPaint = this.mSolarTermTextPaint;
        }
        if(calendar.getDay() == 1){
            canvas.drawText(String.format(Locale.CHINA, "%d月", calendar.getMonth()), (float) cx, this.mTextBaseLine + (float) top, textPaint);
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), (float) cx, this.mTextBaseLine + (float) top, textPaint);
        }
        canvas.drawText(calendar.isCurrentDay() ? "今天" : calendar.getLunar(), (float) cx, this.mTextBaseLine + (float) y + (float) (this.mItemHeight / 10), lunarTextPaint);

    }

    //画当天背景
    protected void onDrawCurDay(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
//        this.mCurDayPaint.setStyle(Paint.Style.STROKE);
//        canvas.drawCircle(x + this.mItemWidth / 2.0F, y + this.mItemHeight / 2.0F, (Math.min(this.mItemHeight, this.mItemWidth) - this.mPadding) / 2.0F, this.mCurDayPaint);
//        canvas.drawRect((float) (x + this.mPadding), (float) (y + this.mPadding), (float) (x + this.mItemWidth - this.mPadding), (float) (y + this.mItemHeight - this.mPadding), this.mCurDayPaint);
    }

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