package com.cr.widget

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.util.AttributeSet
import android.view.View
import com.cr.common.CrColorManager
import com.cr.common.CrFontManager
import com.cr.common.CrUnitManager
import com.cr.cruav.R

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/7/31 09:06
 * 描述：飞行器速度View
 */
class CrSpeed @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAtr: Int = 0
) : View(context, attrs, defStyleAtr) {
    // TODO: 2023/7/31 视图的尺寸及大小相关定义
    private var viewWidth: Int = 0   // define: 2023/7/31 视图宽度
    private var viewHeight: Int = 0  // define: 2023/7/31 视图高度
    private var canvasSize: Int = 0  // define: 2023/7/31 视图大小

    // TODO: 2023/7/31 画笔定义
    private var mArcBasePaint: Paint? = null  // define: 2023/7/31 基础弧画笔
    private var mArcProgressPaint: Paint? = null  // define: 2023/7/31 进度条画笔
    private var mTilePaint: Paint? = null // define: 2023/7/31 标题画笔
    private var mStrokeWith: Int = 20 // define: 2023/7/31 圆弧画笔的宽度 单位是dp
    private var mProgressPaint: Paint? = null // define: 2023/7/31 速度画笔

    // todo: 2023/7/31 画布
    private var canvas: Canvas? = null

    // todo: 2023/7/31 值相关
    private var progressAngle: Int = 30  // define: 2023/7/31 进度条
    private var progressValue: Int = 0  // define: 2023/7/31 当前进度条
    private var progressMax: Int = 100 // define: 2023/7/31 最大值
    private var title: String? = "水平速度" // define: 2023/7/31 标题

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

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

    /**
     * 初始化
     */
    init {
        this.mStrokeWith = CrUnitManager.getDimens(context, R.dimen.cr_5_dp).toInt()
        // todo: 2023/7/31 属性初始化
        val attributes = context.obtainStyledAttributes(attrs, R.styleable.CrSpeed)
        if(attributes != null){
            // todo: 2023/7/31 设置标题
            val strTitle = attributes.getString(R.styleable.CrSpeed_speedTitle)
            title = if(strTitle != null && strTitle != ""){
                strTitle
            }else{
                "**速度"
            }
            val floatProgress = attributes.getInt(R.styleable.CrSpeed_speedProgress,0)
            progressAngle = floatProgress
            progressValue = floatProgress
        }
    }

    /**
     * 重置
     * @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 onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        this.canvas = canvas
        // todo: 2023/7/31 画笔初始化
        initPaint()
        // todo: 2023/7/31 绘制圆弧
        drawArc()
        // todo: 2023/7/31 绘制文字
        drawText()
    }

    /**
     * 绘制弧
     */
    private fun drawArc() {
        var paddingPixel: Float = CrUnitManager.dp2px(0f)
        var strokeWidthPixel = CrUnitManager.dp2px(mStrokeWith)/2
        // todo: 2023/7/31 绘制圆弧背景
        var mRectF: RectF = RectF(
            strokeWidthPixel + paddingPixel,
            strokeWidthPixel + paddingPixel,
            canvasSize - strokeWidthPixel - paddingPixel,
            canvasSize - strokeWidthPixel - paddingPixel
        )

        // todo: 2023/7/31 绘制一个矩形看看 这是测试用的
//        var rectPaint = Paint()
//        rectPaint.color = Color.rgb(0,0,255)
//        canvas?.drawRect(mRectF,rectPaint)

        canvas?.drawArc(mRectF, 90f, 270f, false, mArcBasePaint!!)

        // todo: 2023/7/31 绘制进度条
        canvas?.drawArc(mRectF, 90f, progressAngle * 1f, false, mArcProgressPaint!!)
    }

    /**
     * 绘制文字
     */
    private fun drawText() {
        var speed: String = "$progressValue m/s"
        // todo: 2023/7/31 定义绘制范围
        var titleRect = Rect()
        var speedRect = Rect()
        // todo: 2023/7/31 获取绘制范围
        mTilePaint?.getTextBounds(title, 0, title!!.length, titleRect)
        mProgressPaint?.getTextBounds(speed, 0, speed!!.length, speedRect)
        // todo: 2023/7/31 绘制速度
        canvas?.drawText(
            speed,
            canvasSize / 2f + CrUnitManager.dp2px(1f),
            canvasSize*1f - CrUnitManager.dp2px(mStrokeWith) + titleRect.height(),
            mProgressPaint!!
        )
        // todo: 2023/7/31 绘制标题
        canvas?.drawText(
            title!!,
            viewWidth - titleRect.width()*1f,
            canvasSize - canvasSize/4f - CrUnitManager.dp2px(mStrokeWith) +  titleRect.height(),
            mTilePaint!!
        )
    }

    /**
     * 画笔初始化
     */
    private fun initPaint() {
        // todo: 2023/7/31 基础画笔初始化
        mArcBasePaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mArcBasePaint?.let {
            it.isAntiAlias = true  // todo: 2023/7/31 抗锯齿
            it.color = Color.argb(255, 144, 131, 112)
            it.alpha = 100
            it.strokeJoin = Paint.Join.ROUND
            it.strokeCap = Paint.Cap.BUTT
            it.style = Paint.Style.STROKE  // define: 2023/7/31 画笔类型
            it.strokeWidth = CrUnitManager.dp2px(mStrokeWith).toFloat()
        }

        // todo: 2023/7/31 进度条画笔初始化
        mArcProgressPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mArcProgressPaint?.let {
            it.isAntiAlias = true  // todo: 2023/7/31 抗锯齿
            it.color = CrColorManager.getColor(context, R.color.lawngreen)
            it.alpha = 100
            it.strokeJoin = Paint.Join.ROUND
            it.strokeCap = Paint.Cap.BUTT
            it.style = Paint.Style.STROKE  // define: 2023/7/31 画笔类型
            it.strokeWidth = CrUnitManager.dp2px(mStrokeWith).toFloat()
        }

        // todo: 2023/7/31 标题画笔初始化
        mTilePaint = Paint()
        mTilePaint?.let {
            it.isAntiAlias = true  // todo: 2023/7/31 抗锯齿
            it.textAlign = Paint.Align.LEFT  // define: 2023/7/31 文字对齐方式
            it.color = CrColorManager.getColor(context, R.color.white)  // define: 2023/7/31 文字颜色
            it.textSize =
                CrUnitManager.dp2px(CrUnitManager.getDimens(context, R.dimen.cr_4_dp)).toFloat()
            it.isFakeBoldText = true  // define: 2023/7/31 加粗字体
            it.typeface = CrFontManager.getTTGBTypeface(context)
        }

        // todo: 2023/7/31 速度画笔初始化
        mProgressPaint = Paint()
        mProgressPaint?.let {
            it.isAntiAlias = true  // todo: 2023/7/31 抗锯齿
            it.textAlign = Paint.Align.LEFT  // define: 2023/7/31 文字对齐方式
            it.color = CrColorManager.getColor(context, R.color.progress)  // define: 2023/7/31 文字颜色
            it.textSize =
                CrUnitManager.dp2px(CrUnitManager.getDimens(context, R.dimen.cr_3_dp)).toFloat()
            it.isFakeBoldText = true  // define: 2023/7/31 加粗字体
            it.typeface = CrFontManager.getTTGBTypeface(context)
        }
    }
}