package com.xty.common.weight

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.xty.common.R

class XyaCirCleView  : View {
        private  var showValue=90f
        constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)


    private val paintCircle by lazy {
        Paint().apply {
            this.isAntiAlias=true
            this.isDither=true
            this.strokeWidth=10f
            this.typeface= Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)
            this.style= Paint.Style.STROKE
        }
    }

    private val paintArC by lazy {
        Paint().apply {
            this.isAntiAlias=true
            this.isDither=true
            this.strokeWidth=10f
            this.color= Color.WHITE
            this.style= Paint.Style.STROKE
        }
    }

    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
        }
    }

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

            //var r= Math.min(width/2f,height/2f)-10f  //最大半径
            var r=  height/4f*3/2  //圆心y轴位置
            var centerY=height/2f  //圆心位置  Y轴位置稍微向下便宜
            paintCircle.color= ContextCompat.getColor(context, R.color.col_2cc)

            it.save()
            it.rotate(135f,width/2f,centerY)

            var retcf= RectF(width/2f-r,centerY-r,width/2f+r,centerY+r)

            paintArC.color=ContextCompat.getColor(context,R.color.col_12e)
            it.drawArc(retcf,0f,90f,false,paintArC)
            paintArC.color=ContextCompat.getColor(context,R.color.col_1b7)
            it.drawArc(retcf,90f,90f,false,paintArC)
            paintArC.color=ContextCompat.getColor(context,R.color.col_ffc)
            it.drawArc(retcf,180f,90f,false,paintArC)
            it.restore()

            txtCenterCan(canvas  ,centerY)
            setTopCan(canvas,centerY,r)
            setBottomCan(canvas,centerY,r)

        }


    }

    //setCenter txt
    private fun txtCenterCan(canvas: Canvas,centerY:Float){
        var txt="8.8"
        var hourLength=paintH.measureText(txt)
        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/2  //总字体长度的一般
        var hourX= width/2-allTxtLengthComm
        canvas.drawText(txt,hourX,hourBaseLIne,paintH)

    }
    //绘制上部分文字
    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 setValue(value:Float){

        showValue=value
        invalidate()
    }

}