package com.xty.common.weight

import android.animation.ObjectAnimator
import android.animation.ValueAnimator.INFINITE
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.core.content.ContextCompat
import com.xty.common.R

@SuppressLint("ObjectAnimatorBinding")
class XyCircleView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    val percentValue  by lazy { 50 }//进度  百分比 分子  50/100
    private  var unit="%"

    val bgOffsetX by lazy {  width/4}  //背景偏移量
    val paintCilp by lazy {
        Paint().apply {
            style = Paint.Style.STROKE
            strokeWidth=10f
            isAntiAlias=true
            color=Color.WHITE
            isDither = true;//设定是否使用图像抖动处理，会使绘制出来的图片颜色更加平滑和饱满，图像更加清晰
        }
    }
    val paint by lazy {
        Paint().apply {
            style = Paint.Style.FILL
            strokeWidth=10f
            color= ContextCompat.getColor(context!!, R.color.col_1da)
            isAntiAlias=true
            isDither = true;//设定是否使用图像抖动处理，会使绘制出来的图片颜色更加平滑和饱满，图像更加清晰
        }
    }
    val paintBg by lazy {
        Paint().apply {
            style = Paint.Style.FILL
            color=ContextCompat.getColor(context!!,R.color.col_27b)
            strokeWidth=10f
            isAntiAlias=true
            isDither = true
        }
    }

    private val paintH by lazy {
        Paint().apply {
            this.isAntiAlias = true
            this.isDither = true
            this.color=Color.WHITE
            this.textSize = 60f
            this.style = Paint.Style.FILL_AND_STROKE
        }
    }
    private val paintUnit by lazy {
        Paint().apply {
            this.isAntiAlias = true
            this.isDither = true
            this.color=Color.WHITE
            this.textSize = 30f
            this.style = Paint.Style.FILL_AND_STROKE
        }
    }
    private val paintTime by lazy {
        Paint().apply {
            this.isAntiAlias = true
            this.isDither = true
            this.color=Color.WHITE
            this.textSize = 20f
            this.style = Paint.Style.FILL_AND_STROKE
        }
    }

    private  var offsetX=0f
    val annotations by lazy { ObjectAnimator.ofFloat(this,"offsetX",width.toFloat()).apply {
        this.repeatCount= INFINITE
        this.interpolator= LinearInterpolator()  //差值器  很关键
        this.duration=1500
    }}

    //开始动画
    fun startAnnotations(){
        annotations.setFloatValues(width.toFloat(), 0f)
        annotations.addUpdateListener {
            offsetX=it.animatedValue as Float
            postInvalidate()
        }
        annotations.start()
    }
    //停止
    fun  stopAnnotations(){
        annotations.end()
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

         var dir= Math.min(width,height)  //计算最小值
        var r=  dir/4f*3/2  //半径大小
        var centerY=height/2f //圆心位置  Y轴位置稍微向下便宜
        var mFu = 80f //水波纹的振幅
       // var offsetFu=560*(percentValue/100) //振幅偏移
        /**
         * 绘制偏移水波纹
         */
        canvas.save()
        var pathCilp= Path()
        pathCilp.addCircle(width/2f,centerY,r,Path.Direction.CCW)
        canvas.clipPath(pathCilp)

        var zero=centerY+r //水波纹起始点
        var offsetY=zero-(2*r*(percentValue/100f)) //水波纹y轴的偏移量


        var pathBg=Path()
        pathBg.moveTo(width.toFloat()+offsetX ,offsetY)
        pathBg.lineTo(width.toFloat()+offsetX ,zero)
        pathBg.lineTo(-width.toFloat()+offsetX ,zero)
        pathBg.lineTo(-width.toFloat()+offsetX ,offsetY)
        //先画屏幕外面的
        pathBg.quadTo((-width*3/4f)+offsetX+bgOffsetX,offsetY+mFu,(-width/2f)+offsetX+bgOffsetX,offsetY)
        pathBg.quadTo((-width/4f)+offsetX+bgOffsetX,offsetY-mFu,0f+offsetX+bgOffsetX,offsetY)
        pathBg.quadTo((width/4f)+offsetX+bgOffsetX,offsetY+mFu,(width/2f+offsetX+bgOffsetX),offsetY)
        pathBg.quadTo((width*3/4f)+offsetX+bgOffsetX,offsetY-mFu,width.toFloat()+offsetX+bgOffsetX,offsetY)
        canvas.drawPath(pathBg,paintBg)

        /**
         * 绘制前置水波纹
         */
        var path=Path()
        path.moveTo(width.toFloat()+offsetX,offsetY)
        path.lineTo(width.toFloat()+offsetX,zero)
        path.lineTo(-width.toFloat()+offsetX,zero)
        path.lineTo(-width.toFloat()+offsetX,offsetY)

        //先画屏幕外面的
        path.quadTo((-width*3/4f)+offsetX,offsetY+mFu,(-width/2f)+offsetX,offsetY)
        path.quadTo((-width/4f)+offsetX,offsetY-mFu,0f+offsetX,offsetY)
        path.quadTo((width/4f)+offsetX,offsetY+mFu,(width/2f+offsetX),offsetY)
        path.quadTo((width*3/4f)+offsetX,offsetY-mFu,width.toFloat()+offsetX,offsetY)
        canvas.drawPath(path,paint)

        canvas.restore()
        //裁剪
        canvas.drawCircle(width/2f,centerY,r,paintCilp)
        txtCenterCan(canvas,centerY)
        setTopCan(canvas,centerY,r)
        setBottomCan(canvas,centerY,r)
    }
    //setCenter txt
    private fun txtCenterCan(canvas: Canvas,centerY:Float){
        var txt="90"
        var hourLength=paintH.measureText(txt)
        var unitLength=paintUnit.measureText(unit)
        var huourf=paintH.fontMetrics
        var hourBaseLIne=centerY+( huourf.descent-huourf.ascent)/2-huourf.descent

        /* var unitF=paintUnit.fontMetrics
         var unitBaseLine=height/2f+( unitF.ascent-unitF.descent)/2+unitF.descent*/
        var allTxtLengthComm= (hourLength+unitLength)/2  //总字体长度的一般
        var hourX= width/2-allTxtLengthComm
        canvas.drawText(txt,hourX,hourBaseLIne,paintH)
        //计算单位文字绘制的其实x坐标
        //文字的长度减去总体（文字+单位的长度）/2 的一般 等于文字绘制中间之后剩余绘制的距离
        var unitStartX= width/2+(hourLength-allTxtLengthComm)
        canvas.drawText(unit,unitStartX,hourBaseLIne,paintUnit)
    }
    //绘制上部分文字
    fun setTopCan(canvas: Canvas,centerY:Float,r:Float){
        var txt="当前心率"
        var txtLength=paintUnit.measureText(txt)
        var txtf=paintUnit.fontMetrics

        var txtBaseLIne=(centerY+r)/4f+( txtf.descent-txtf.ascent)/2-txtf.descent
        var wX=width/2f-txtLength/2
        canvas.drawText(txt,wX,txtBaseLIne,paintUnit)
    }
    //绘制底部分文字
    fun setBottomCan(canvas: Canvas,centerY:Float,r:Float){
        var txt="22:22"
        var txtLength=paintTime.measureText(txt)
        var txtf=paintTime.fontMetrics

        var txtBaseLIne=(centerY+r)/4f*3+( txtf.descent-txtf.ascent)/2-txtf.descent
        var wX=width/2f-txtLength/2
        canvas.drawText(txt,wX,txtBaseLIne,paintTime)
    }

    //设置百分比
    fun setPercent(percent:Float){
        //percentValue=percent


    }
}