package com.cr.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.cr.common.CrColorManager
import com.cr.common.CrUnitManager
import com.cr.cruav.R

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/8/1 09:38
 * 描述：基础绘制组件 继承自View 共需要动态绘制内容的视图继承使用
 */
open class CrBaseDrawWidget @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    // todo: 2023/8/1 视图大小定义
    protected var viewWidth: Int = 0   // define: 2023/8/1 视图宽度
    protected var viewHeight: Int = 0  // define: 2023/8/1 视图高度
    protected var canvasSize: Int = 0  // define: 2023/8/1 视图大小

    // todo: 2023/8/1 画布
    protected var canvas: Canvas? = null

    /**
     * 生命周期函数
     * 当xml加载完成后会调用此方法
     */
    override fun onFinishInflate() {
        super.onFinishInflate()
        setBackgroundColor(Color.argb(0,70,71,81))
    }

    /**
     * 生命周期函数
     * 视图加入窗口
     */
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
    }

    /**
     * 重绘
     * @param widthMeasureSpec Int
     * @param heightMeasureSpec Int
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        // todo: 2023/7/31 获取视图宽度和高度 单位是像素
        this.viewWidth = MeasureSpec.getSize(widthMeasureSpec)
        this.viewHeight = MeasureSpec.getSize(heightMeasureSpec)
        // todo: 2023/7/31 取小的那个值作为画布大小
        canvasSize = if (viewWidth > viewHeight) viewHeight else viewWidth
    }

    /**
     * 重绘
     * @param canvas Canvas
     */
    override fun draw(canvas: Canvas?) {
        super.draw(canvas)
        this.canvas = canvas
    }

    /**
     * 初始化线类型画笔
     * @param paint Paint
     */
    protected fun crPaintInit(paint:Paint){
        paint?.let {
            it.isAntiAlias = true  // todo: 2023/8/1 抗锯齿
            it.color = CrColorManager.getColor(context, R.color.white)
            it.alpha = 100
            it.strokeJoin = Paint.Join.ROUND
            it.strokeCap = Paint.Cap.BUTT
            it.style = Paint.Style.STROKE  // define: 2023/8/1 画笔类型
        }
    }
}