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

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


    private var centerTxtSize=0f
    private var circleCol:Int=0
    private var arcCol=0
    private var strokeWidthMy=0f
    private var selectArc=120f //扫过的角度
    private var strText="--天"

    private  val paintCenter by lazy{
        Paint().apply {
            isDither=true
            isAntiAlias=true
            style=Paint.Style.FILL
            color= ContextCompat.getColor(context, R.color.col_455)
            textSize=centerTxtSize
        }
    }
    private  val paintCircle by lazy{
        Paint().apply {
            isDither=true
            isAntiAlias=true
            style=Paint.Style.STROKE
            color=circleCol
            strokeWidth=strokeWidthMy
        }
    }

    private  val paintArc by lazy{
        Paint().apply {
            isDither=true
            isAntiAlias=true
            style=Paint.Style.STROKE
            color=arcCol
            strokeWidth=strokeWidthMy
        }
    }

    init {

        val obtain  = context.obtainStyledAttributes(attrs, R.styleable.CircleDayShow)

        centerTxtSize=obtain.getDimensionPixelSize(R.styleable.CircleDayShow_center_txt_size,0).toFloat()
        strokeWidthMy=obtain.getDimensionPixelSize(R.styleable.CircleDayShow_stroke_width,0).toFloat()
        circleCol=obtain.getColor(R.styleable.CircleDayShow_circle_col,0)
        arcCol=  obtain.getColor(R.styleable.CircleDayShow_arc_col,0)
        obtain.recycle()

    }


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

        var length=Math.min(width,height)


        canvas.drawCircle(width/2f,height/2f,length/2f-strokeWidthMy,paintCircle)

        var rectf=RectF(width/2f-length/2f+strokeWidthMy,height/2f-length/2f+strokeWidthMy,width/2f-strokeWidthMy+length/2f,height/2f+length/2f-strokeWidthMy)

        canvas.save()
        canvas.rotate(270f,width/2f,height/2f)
        canvas.drawArc(rectf,0f,selectArc,false,paintArc)
        canvas.restore()


        val measureText = paintCenter.measureText(strText)

        val fontMetrics = paintCenter.fontMetrics
        var txth=fontMetrics.descent-fontMetrics.ascent

        var baselineHeight= txth/2-fontMetrics.descent+   height/2

        canvas.drawText(strText,width/2- measureText/2,baselineHeight,paintCenter)
    }

    /**
     *
     *  非总进度
     *
     */
    fun setOtherData(all:Int,now:Int){
        selectArc= now*1f/all*360
        strText="${now}天"
        invalidate()

    }
    //总进度
    fun setAllData(all:Int){
        strText="${all}天"
        selectArc=0f
        invalidate()

    }
}