package com.xty.common.weight

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.xty.common.R
import com.xty.common.SizeUtil

class CircleView(context: Context, attrs: AttributeSet?): View(context, attrs) {
    private  var showValue=0f  //扫描半径
    private  var unit="h"
    private var sleepInfo="0"

    private  var unitSize=0f
    private  var centerSize=0f
    private  var circleWith=0f
    private  var distance=0f

    init {

        var recycle=context.obtainStyledAttributes(attrs,R.styleable.CircleView)
        unitSize=recycle.getFloat(R.styleable.CircleView_unit_size,0f)
        centerSize=recycle.getFloat(R.styleable.CircleView_center_size,0f)
        circleWith=recycle.getFloat(R.styleable.CircleView_circle_with,0f)
        distance=recycle.getFloat(R.styleable.CircleView_distance,0f)
        recycle.recycle()
    }
        private val paintCircle by lazy {
        Paint().apply {
            this.isAntiAlias=true
            this.isDither=true
            this.strokeWidth= circleWith
            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=circleWith
            this.style=Paint.Style.STROKE
            this.strokeCap=Paint.Cap.ROUND
        }
    }

    private val paintH by lazy {
        Paint().apply {
            this.isAntiAlias = true
            this.isDither = true
            this.textSize =centerSize
            this.style = Paint.Style.FILL_AND_STROKE
        }
    }
    private val paintUnit by lazy {
        Paint().apply {
            this.isAntiAlias = true
            this.isDither = true
            this.textSize = unitSize
            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)-distance   //最大半径
            paintCircle.color=ContextCompat.getColor(context,R.color.col_d0d)
            it.drawCircle(width/2f,height/2f,r,paintCircle)
            it.save()
            it.rotate(270f,width/2f,height/2f)

            paintArC.color=ContextCompat.getColor(context,R.color.col_817)
            var retcf=RectF(width/2f-r,height/2f-r,width/2f+r,height/2f+r)
            it.drawArc(retcf,0f,showValue,false,paintArC)
            it.restore()
            txtSize(canvas)

        }


    }

    private fun txtSize(canvas: Canvas){
        var hourLength=paintH.measureText(sleepInfo)
        var unitLength=paintUnit.measureText(unit)
        var huourf=paintH.fontMetrics
        var hourBaseLIne=height/2f+( 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
        paintH.color=ContextCompat.getColor(context,R.color.col_564)
        canvas.drawText(sleepInfo,hourX,hourBaseLIne,paintH)
        //计算单位文字绘制的其实x坐标
        //文字的长度减去总体（文字+单位的长度）/2 的一般 等于文字绘制中间之后剩余绘制的距离
        paintUnit.color=ContextCompat.getColor(context,R.color.col_564)
       var unitStartX= width/2+(hourLength-allTxtLengthComm)
        canvas.drawText(unit,unitStartX,hourBaseLIne,paintUnit)
    }

    fun setData(value:String?){
        if(value==null){
            sleepInfo="无睡眠"
            unit=""
            showValue=0f
        }else{
            sleepInfo=value
            unit="h"
            //认为8小时为百分之百睡眠
            val toFloat = value.toFloat()
            showValue= 360*toFloat/8
        }

        postInvalidate()
    }



}