package com.zxhy.fastfilemanagerpro.widget

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.zxhy.fastfilemanagerpro.R

class SS : View {

    private var widthSize = 0f
    private var heightSize = 0f
    private var outCircleRadius = 0f
    private var circlePaint: Paint = Paint()
    private val bitmapPaint = Paint()
    private val pointPaint = Paint()
    private var pointRadius = 0f
    private var bitmapRadius = 0f
    private var margin = 10f
    private lateinit var bitmap: Bitmap

    private var outArcMargin = 0f
    private var outArcRadius = 0f
    private val outArcPaint = Paint()
    private var outArcPercent = 0f

    private var inArcMargin = 0f
    private var inArcRadius = 0f
    private val inArcPaint = Paint()
    private var inArcPercent = 0f

    constructor(context: Context?) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        circlePaint.color = ContextCompat.getColor(context, R.color.color_ece8d9)
        pointPaint.color = ContextCompat.getColor(context, R.color.color_fffdf6)
        bitmap = BitmapFactory.decodeResource(resources, R.mipmap.d_897895)
        bitmapRadius = (bitmap.width / 2).toFloat()
        pointRadius = resources.getDimension(R.dimen.dp_6)

        outArcMargin = resources.getDimension(R.dimen.dp_20)
        outArcRadius = resources.getDimension(R.dimen.dp_64)
        outArcPaint.color = ContextCompat.getColor(context, R.color.color_494949)
        outArcPaint.style = Paint.Style.STROKE
        outArcPaint.strokeCap = Paint.Cap.ROUND
        outArcPaint.strokeWidth = resources.getDimension(R.dimen.dp_7)

        inArcMargin = resources.getDimension(R.dimen.dp_55)
        inArcRadius = resources.getDimension(R.dimen.dp_29)
        inArcPaint.color = ContextCompat.getColor(context, R.color.color_faf6e9)
        inArcPaint.style = Paint.Style.STROKE
        inArcPaint.strokeCap = Paint.Cap.ROUND
        inArcPaint.strokeWidth = resources.getDimension(R.dimen.dp_7)
    }

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

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//        val widthMode = MeasureSpec.getMode(widthMeasureSpec) //获取宽的模式
        widthSize = MeasureSpec.getSize(widthMeasureSpec).toFloat() //获取宽的尺寸
//        val heightMode = MeasureSpec.getMode(heightMeasureSpec) //获取高的模式
        heightSize = MeasureSpec.getSize(heightMeasureSpec).toFloat() //获取高的尺寸
        outCircleRadius = widthSize / 2 - margin
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawCircle(
            widthSize / 2, heightSize / 2,
            outCircleRadius, circlePaint
        )
        canvas.drawBitmap(
            bitmap,
            (outCircleRadius - bitmapRadius) + margin,
            (outCircleRadius - bitmapRadius) + margin,
            bitmapPaint
        )
        canvas.drawCircle(widthSize / 2, heightSize / 2, pointRadius, pointPaint)
        val left1 = outArcMargin
        val top1 = outArcMargin
        val right1 = left1 + outArcRadius * 2
        val bottom1 = top1 + outArcRadius * 2
        canvas.drawArc(
            left1,
            top1,
            right1,
            bottom1,
            -90f,
            -(360 * outArcPercent),
            false,
            outArcPaint
        )

        val left2 = inArcMargin
        val top2 = inArcMargin
        val right2 = left2 + inArcRadius * 2
        val bottom2 = top2 + inArcRadius * 2
        canvas.drawArc(left2, top2, right2, bottom2, -90f, (360 * inArcPercent), false, inArcPaint)
    }

    fun setPercent(outArcPercent: Float, inArcPercent: Float) {
        this.outArcPercent = outArcPercent
        this.inArcPercent = inArcPercent
        invalidate()
    }
}