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 CircleInfoView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private  var showValue=0f
    private  var unit=""
    private  var center_txt=""
    private var top_txt=""
    private var bottom_txt=""
    private  var angle=""


    init {
        var obtain= context?.obtainStyledAttributes(attrs,R.styleable.XlCircleView)
        obtain?.let {
            unit= it.getString(R.styleable.XlCircleView_unit).toString()
            center_txt=it.getString(R.styleable.XlCircleView_center_txt).toString()
            top_txt=if(it.getString(R.styleable.XlCircleView_top_txt)==null) "" else it.getString(R.styleable.XlCircleView_top_txt).toString()
            bottom_txt=if(it.getString(R.styleable.XlCircleView_bottom_txt)==null) "" else it.getString(R.styleable.XlCircleView_bottom_txt).toString()
            showValue= it.getString(R.styleable.XlCircleView_showValue)?.toFloat() ?:0f
            it.recycle( )
        }
    }

    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
            this.strokeCap= Paint.Cap.ROUND
        }
    }

    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*3f/4f/2f   //半径
            var centerY=height/2f   //圆心位置  Y轴位置稍微向下便宜
            paintCircle.color= ContextCompat.getColor(context, R.color.col_2cc)
            it.drawCircle(width/2f,centerY,r,paintCircle)
            it.save()
            it.rotate(270f,width/2f,centerY)

            var retcf= RectF(width/2f-r,centerY-r,width/2f+r,centerY+r)
            it.drawArc(retcf,0f,showValue,false,paintArC)
            it.restore()
            txtCenterCan(canvas,centerY )
            setTopCan(canvas,centerY,r)
            setBottomCan(canvas,centerY,r)

        }


    }

    //setCenter txt
    private fun txtCenterCan(canvas: Canvas,centetY:Float){

        var hourLength=paintH.measureText(center_txt)
        var unitLength=paintUnit.measureText(unit)
        var huourf=paintH.fontMetrics
        var hourBaseLIne=centetY+( 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(center_txt,hourX,hourBaseLIne,paintH)
        //计算单位文字绘制的其实x坐标
        //文字的长度减去总体（文字+单位的长度）/2 的一般 等于文字绘制中间之后剩余绘制的距离
        var unitStartX= width/2+(hourLength-allTxtLengthComm)
        canvas.drawText(unit,unitStartX,hourBaseLIne,paintUnit)
    }
    //绘制上部分文字
    fun setTopCan(canvas: Canvas,centetY:Float,r:Float){
        if(top_txt==null||top_txt==""){
            return
        }
        var txtLength=paintUnit.measureText(top_txt)
        var txtf=paintUnit.fontMetrics

        var txtBaseLIne=(height)/4f+( txtf.descent-txtf.ascent)/2-txtf.descent
        var wX=width/2f-txtLength/2
        canvas.drawText(top_txt,wX,txtBaseLIne,paintUnit)
    }
    //绘制底部分文字
    fun setBottomCan(canvas: Canvas,centetY:Float,r:Float){
        if(bottom_txt==null||bottom_txt==""){
            return
        }
        var txtLength=paintTime.measureText(bottom_txt)
        var txtf=paintTime.fontMetrics

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

    fun setValue(value:Float){
        showValue=value
        invalidate()
    }



}