package com.abbidot.tracker.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.qmuiteam.qmui.util.QMUIDisplayHelper

/**
 *Created  by .yzq on 2024/1/22/022.
 * @link
 * @description:小圆的进度条
 */
class SmallDotProgressView : View {

    private var mWidth = 0
    private var mHeight = 0

    private val mRadius = AppUtils.dpToPx(2.2f)
    private val mBorderSize = AppUtils.dpToPx(1f)
    private val mCircleSpace = QMUIDisplayHelper.dpToPx(6)
    private val mMaxProgress = 4
    private var mProgress = 1

    private var mDefColor = 0
    private var mBorderColor = 0
    private var mProgressColor = 0

    private lateinit var mDefPaint: Paint
    private lateinit var mProgressPaint: Paint

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        //禁用硬件加速,防止返回到界面多次执行onDraw
        setLayerType(LAYER_TYPE_SOFTWARE, null)

        initAttrs(context, attrs)

        mDefPaint = Paint()
        mDefPaint.color = mDefColor

        mProgressPaint = Paint()
        mProgressPaint.color = mBorderColor
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        mDefColor = ContextCompat.getColor(context, R.color.line_color1)
        mBorderColor = ContextCompat.getColor(context, R.color.select_color)
        mProgressColor = ContextCompat.getColor(context, R.color.select_color2)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.apply {
            startDraw(this)
        }
    }

    private fun startDraw(canvas: Canvas) {
        for (i in 0 until mMaxProgress) {
            val cx = mRadius + mRadius * 2 * i + mCircleSpace * i
            val cy = mHeight / 2f
            if (i == mProgress - 1) {
                mDefPaint.color = mProgressColor
                canvas.drawCircle(cx, cy, mRadius, mProgressPaint)
                canvas.drawCircle(cx, cy, mRadius - mBorderSize, mDefPaint)
            } else {
                mDefPaint.color = mDefColor
                canvas.drawCircle(cx, cy, mRadius, mDefPaint)
            }
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mHeight = (mRadius * 2).toInt()
        mWidth = (mRadius * 2 * mMaxProgress + mCircleSpace * (mMaxProgress - 1)).toInt()
        setMeasuredDimension(mWidth, mHeight)

//        // 获取- 网规则的模式和大小
//        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
//        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
//        // 获取高- 测量规则的模式和大小
//        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
//        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
//        LogUtil.e("$widthMode,$widthSize,,,$heightMode,$heightSize,----${MeasureSpec.AT_MOST},${MeasureSpec.EXACTLY},${MeasureSpec.UNSPECIFIED}")
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        //处理布局高度变大(如RelativeLayout，强制变宽),居中
        val newHeight = bottom - top
        if (newHeight > mHeight) {
            mHeight = newHeight
            invalidate()
        }
    }

    fun setProgress(progress: Int) {
        if (progress != mProgress && progress >= 0) {
            mProgress = progress
            invalidate()
        }
    }
}