package com.zxhy.hfilemanagermaster.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import com.example.hfilemanagermaster.R
import com.zxhy.hfilemanagermaster.knife.setImageSize
import java.math.BigDecimal
import java.math.RoundingMode
import kotlin.math.min

class IndicatorView : View {

    private var strokeWidth = 40f//圆弧线宽
    private var radius = 425f//圆弧半径(外圆弧)
    private var hMargin = 75f//圆弧水平间距
    private var topMargin = 50f//圆弧顶部距离
    private var sweepAngle = 0f

    private lateinit var bgBitmap: Bitmap
    private var scale = 1f//指针图像缩放比例

    private var rotationDegrees = 0f // 旋转的角度
    private var indicatorY = 0.95f//绘制指针画布Y移动的距离
    private var indicatorX = 0f//绘制指针画布X移动的修补
    private var rotationDegreesOffset = 0f//指针旋转误差修补

    // 画原始刻度的画笔
    private var arcPaint: Paint = Paint()

    constructor(context: Context?) : super(context) {
        setResource()
    }

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

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

    private fun setResource() {

        bgBitmap = BitmapFactory.decodeResource(context.resources, R.mipmap.dad_58978)

        arcPaint.strokeWidth = strokeWidth
        arcPaint.color = ContextCompat.getColor(context, R.color.color_DCFE86)
        arcPaint.isAntiAlias = true
        arcPaint.style = Paint.Style.STROKE
        arcPaint.strokeCap = Paint.Cap.ROUND
    }

    @SuppressLint("DrawAllocation")
    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)

        scale = BigDecimal(measuredWidth).divide(
            BigDecimal(bgBitmap.width), 3, RoundingMode.HALF_UP
        ).toFloat()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        // 通过测量规则获得宽和高,这里宽高一致
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
//        Log.e("IndicatorView", "width=$width height=$height")
        // 取出最小值
        val len = min(width, height)

        hMargin = len * 0.125f
        topMargin = len * 0.075f

        //获取半径
        radius = len.toFloat() - topMargin
//        Log.e("IndicatorView", "radius=$radius")
        setMeasuredDimension(width, height)
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)


//        Log.e("IndicatorView", "radius=$radius")
        val left = hMargin + strokeWidth / 2
        val top = topMargin + strokeWidth / 2
        val right = radius * 2 + left
        val bottom = radius * 2 + top

//        Log.e("IndicatorView", "left=$left top=$top right=$right bottom=$bottom")
        //绘制半圆
        val arcRect = RectF(left, top, right, bottom)
        canvas.drawArc(arcRect, -180f, sweepAngle, false, arcPaint)


        canvas.translate(
            (measuredWidth / 2).toFloat() - (bgBitmap.width / 2) * indicatorX,
            measuredHeight.toFloat() * indicatorY
        )
        // 绘制旋转后的图片
//        val srcRect = Rect(0, 0, 100, 100)
//        val dstRect = Rect(0, 0, 100, 100)
        //绘制指针
        canvas.rotate(rotationDegrees)
        canvas.save()
        canvas.drawBitmap(bgBitmap, 0f, -bgBitmap.height.toFloat(), Paint())

    }

    //绘制百分比
    fun setPercent(percent: Float) {
        sweepAngle = 180f * percent
        rotationDegrees = 180f * percent - 90f
//        sweepAngle = 180f
//        rotationDegrees = 180f - 90f
        //针对指针角度进行误差调整
        when (sweepAngle) {
            in 0f..29f -> {
                indicatorY = 0.95f
                indicatorX = 0f
                rotationDegreesOffset = -8f
            }

            in 30f..59f -> {
                indicatorY = 0.98f
                indicatorX = 0f
                rotationDegreesOffset = -5f
            }

            in 60f..89f -> {
                indicatorY = 0.95f
                indicatorX = 1f
                rotationDegreesOffset = 0f
                rotationDegrees += rotationDegreesOffset
            }

            in 90f..99f -> {
                indicatorY = 0.95f
                indicatorX = 1f
                rotationDegreesOffset = 0f
            }

            in 100f..119f -> {
                indicatorY = 0.95f
                indicatorX = 1f
                rotationDegreesOffset = 2f
            }

            in 120f..149f -> {
                indicatorY = 0.85f
                indicatorX = 1.05f
                rotationDegreesOffset = 8f
            }

            in 150f..180f -> {
                indicatorY = 0.85f
                indicatorX = 1.08f
                rotationDegreesOffset = 8f
            }
        }
        rotationDegrees += rotationDegreesOffset
//        Log.e("IndicatorView", "rotationDegrees=$rotationDegrees")
        invalidate()
    }
}