package com.xty.common.weight

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.xty.common.R
import com.xty.common.SizeUtil
import com.xty.common.model.ReportBean
import java.lang.Math.cos
import java.lang.Math.sin

class MyProgressCircleView(context: Context, attrs: AttributeSet?) : View(context, attrs) {

    private var bean: ReportBean? = null
    private var progressWidth = 0f
    private var exampleWidth = 0f
    private var percentSize = 0f
    private var exampleLength = 0f
    private var count = 3

    private val paintProgress by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.STROKE
            strokeWidth = progressWidth
            color = ContextCompat.getColor(context, R.color.col_9BDd)
        }
    }

    private val paintFir by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.STROKE
            strokeWidth = progressWidth
            color = ContextCompat.getColor(context, R.color.col_9BDd)
        }
    }

    private val paintExample by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.STROKE
            strokeWidth = exampleWidth
            strokeCap = Paint.Cap.ROUND
            color = ContextCompat.getColor(context, R.color.col_FDA481)
        }
    }

    private val paintTxt by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL
            color = ContextCompat.getColor(context, R.color.col_313)
            textSize = percentSize
        }
    }

    init {
        val obtain = context.obtainStyledAttributes(attrs, R.styleable.MyProgressCircleView)
        progressWidth =
            obtain.getDimensionPixelSize(R.styleable.MyProgressCircleView_circle_stroke_width, 0)
                .toFloat()
        percentSize =
            obtain.getDimensionPixelSize(R.styleable.MyProgressCircleView_percent_size, 0).toFloat()
        exampleWidth =
            obtain.getDimensionPixelSize(R.styleable.MyProgressCircleView_example_size, 0).toFloat()
        exampleLength =
            obtain.getDimensionPixelSize(R.styleable.MyProgressCircleView_example_length, 0)
                .toFloat()
        count = obtain.getInt(R.styleable.MyProgressCircleView_count, 0)
        obtain.recycle()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //绘制基础的圆形
        drawMyCircle(canvas)
        //绘制尾部占比数量
        drawTxt(canvas)
        //次数
        drawNumTxt(canvas)
        //绘制 用例前面的占比名称
        drawStartTxt(canvas)
        //绘制用例颜色
        drawExampleLine(canvas)
    }

    /**
     * 绘制尾部占比数量
     */
    private fun drawTxt(canvas: Canvas) {
        val fontMetrics = paintTxt.fontMetrics
        val txtHeight = fontMetrics.descent - fontMetrics.ascent
        val minHeight = height / count
        val baseline = minHeight / 2 + txtHeight / 2 - fontMetrics.descent

        for (i in 0 until count) {
            val startY = minHeight * i + baseline
            val num = (bean!!.listNum[i]) / (bean!!.count.toFloat()) * 100
            var doubleNum = String.format("%.1f", num)
            if (doubleNum.equals("NaN")) {
                doubleNum = "0"
            }
            val measureText = paintTxt.measureText("$doubleNum%")
            canvas.drawText(
                "$doubleNum%",
                width.toFloat() - measureText - paddingRight,
                startY,
                paintTxt
            )
        }
    }

    /**
     * 次数
     */
    private fun drawNumTxt(canvas: Canvas) {
        val fontMetrics = paintTxt.fontMetrics
        val txtHeight = fontMetrics.descent - fontMetrics.ascent
        val minHeight = height / count
        val baseline = minHeight / 2 + txtHeight / 2 - fontMetrics.descent
        for (i in 0 until count) {
            val startY = minHeight * i + baseline
            val day = bean!!.listNum[i].toString() + "次"
            val measureText = paintTxt.measureText(day)
            canvas.drawText(
                day,
                width.toFloat() - measureText - paddingRight - SizeUtil.dp2px(context, 52f),
                startY,
                paintTxt
            )
        }
    }

    /**
     * 绘制 用例前面的占比名称
     */
    private fun drawStartTxt(canvas: Canvas) {
        val fontMetrics = paintTxt.fontMetrics
        val txtHeight = fontMetrics.descent - fontMetrics.ascent
        val minHeight = height / count
        val baseLine = minHeight / 2 + txtHeight / 2 - fontMetrics.descent
        for (i in 0 until count) {
            val startY = minHeight * i + baseLine
            canvas.drawText(
                bean!!.listName[i],
                width / 2f + exampleLength + 10 + (exampleLength / 3f),
                startY,
                paintTxt
            )
        }
    }

    /**
     * 绘制用例颜色
     */
    private fun drawExampleLine(canvas: Canvas) {
        var startAngle = 270f
        val i = width / 2
        val coerceAtMost = i.coerceAtMost(height)
        val r = coerceAtMost / 2f - progressWidth / 2 - paddingTop
        val startX = i / 2f - r
        val startY = height / 2f - r
        val endX = i / 2 + r
        val endY = height / 2f + r

        val rectF = RectF(startX, startY, endX, endY)

        val array = arrayOf(
            ContextCompat.getColor(context, R.color.col_9BDd),
            ContextCompat.getColor(context, R.color.col_9BD),
            ContextCompat.getColor(context, R.color.col_FDA481),
            ContextCompat.getColor(context, R.color.col_EFF),
            ContextCompat.getColor(context, R.color.col_BC8),
        )
        val x = width / 2f
        val y = height / count
        for (i in 0 until count) {
            val centerY = y * i + (y / 2f)
            paintExample.color = array[i]
            canvas.drawLine(x, centerY, x + exampleLength, centerY, paintExample)
            paintFir.color = array[i]
            val angle = if (bean!!.count == 0) {
                360 / count.toFloat()
            } else {
                360 * bean!!.listNum[i] / bean!!.count.toFloat()
            }

            paintFir.color = array[i]
            canvas.drawArc(rectF, startAngle, angle, false, paintFir)
            startAngle += angle
        }
    }

    //绘制基础的圆形
    private fun drawMyCircle(canvas: Canvas) {
        val i = width / 2
        val coerceAtMost = i.coerceAtMost(height)
        val r = coerceAtMost / 2f - progressWidth / 2 - paddingTop
        canvas.drawCircle(i / 2f, height / 2f, r, paintProgress)
    }

    fun setData(bean: ReportBean) {
        this.bean = bean
        count = bean.listName.size
        invalidate()
    }
}