package com.base.scanqrclear.luma

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.graphics.Shader
import android.util.AttributeSet
import android.view.View
import com.base.scanqrclear.R

class CircleProgressBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val arcPaint: Paint
    private val bgPaint: Paint
    private val path: Path
    private val bounds: RectF
    private var progress = 0.0f // 进度
    private var colorArray = intArrayOf()

    init {
        arcPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            style = Paint.Style.STROKE
            strokeWidth = dpToPx(6f)
            strokeCap = Paint.Cap.ROUND
        }

        bgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            color = Color.TRANSPARENT
            style = Paint.Style.STROKE
            strokeWidth = dpToPx(6f)
            strokeCap = Paint.Cap.ROUND
        }

        path = Path().apply {

        }

        bounds = RectF()
        colorArray = intArrayOf(
            context.getColor(R.color.color_ffae00),
            context.getColor(R.color.color_fff5c3),
            context.getColor(R.color.color_ff6400),
        )
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 考虑Paint的strokeWidth，调整RectF的边界
        val strokeWidth: Float = bgPaint.getStrokeWidth()
        val halfStrokeWidth = strokeWidth / 2

        // 绘制完整圆的背景
        path.reset()
        path.addCircle(
            width / 2f,
            height / 2f,
            (Math.max(width, height) - strokeWidth) / 2,
            Path.Direction.CW
        )
        canvas.drawPath(path, bgPaint)

        // 绘制完整圆的进度
        path.reset()
        val sweepAngleForProgress: Float = 360 * progress

        // 设置渐变色
        val shader = LinearGradient(
            width / 2f, 0f, // 起点坐标
            width / 2f, height.toFloat(), // 终点坐标
            colorArray, // 渐变颜色
            null, // 颜色位置（null表示均匀分布）
            Shader.TileMode.CLAMP // 填充模式
        )
        arcPaint.shader = shader

        // 从圆的上方开始绘制
        path.addArc(
            RectF(
                halfStrokeWidth,
                halfStrokeWidth,
                width - halfStrokeWidth,
                height - halfStrokeWidth
            ), 270f, sweepAngleForProgress
        )
        canvas.drawPath(path, arcPaint)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val desiredWidth = dpToPx(144f)
        val desiredHeight = dpToPx(144f)
        setMeasuredDimension(resolveSize(desiredWidth, widthMeasureSpec), resolveSize(desiredHeight, heightMeasureSpec))
    }

    fun set(strokeWidth: Float, colorArray: IntArray) {
        val width = dpToPx(strokeWidth)
        arcPaint.strokeWidth = width
        bgPaint.strokeWidth = width
        this.colorArray = colorArray
    }

    fun setProgress(progress: Float) {
        this.progress = progress
        invalidate()
    }

    private fun dpToPx(dp: Float): Float {
        return dp * resources.displayMetrics.density
    }

    private fun resolveSize(desiredSize: Float, measureSpec: Int): Int {
        val mode = MeasureSpec.getMode(measureSpec)
        val size = MeasureSpec.getSize(measureSpec)
        return when (mode) {
            MeasureSpec.EXACTLY -> size
            MeasureSpec.AT_MOST -> Math.min(desiredSize.toInt(), size)
            else -> desiredSize.toInt()
        }
    }
}