package com.xty.common.weight

import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.core.content.ContextCompat
import com.xty.common.R
import com.xty.common.SizeUtil

class FullCircleInfo (context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private  var showValue=0f
    var unit=""
    var center_txt=""
    var top_txt=""
    var bottom_txt=""
    private  var angle=""

    private var centerSize =0f
    private var unitSize = 0
    private var topSize =0f
    private var bottomSize =0f
    private var drawables:Drawable?=null
    var click:MyClick?=null


    var r=0f //半径

        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

            centerSize= it.getDimensionPixelSize(R.styleable.XlCircleView_value_size,0).toFloat()
            unitSize = it.getDimensionPixelSize(R.styleable.XlCircleView_circle_unit_size, 0)
            topSize=it.getDimensionPixelSize(R.styleable.XlCircleView_top_size,0).toFloat()
            bottomSize=it.getDimensionPixelSize(R.styleable.XlCircleView_bottom_size,0).toFloat()
            drawables=it.getDrawable(R.styleable.XlCircleView_bmp)
            it.recycle( )
        }
    }

    private val paintCircle by lazy {
        Paint().apply {
            this.isAntiAlias=true
            this.isDither=true
            this.color= ContextCompat.getColor(context!!, R.color.col_62d)
            this.strokeWidth= SizeUtil.px2dp(context,16f)
            this.typeface= Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)
            this.style= Paint.Style.STROKE
        }
    }


    private val paintFullCircle by lazy {
        Paint().apply {
            this.isAntiAlias=true
            this.isDither=true
            this.strokeWidth= SizeUtil.px2dp(context!!,20f)
            this.color= ContextCompat.getColor(context!!, R.color.col_25c)
            this.style= Paint.Style.FILL_AND_STROKE
            this.strokeCap= Paint.Cap.ROUND
        }
    }

    //绘制中心文字
    private val paintH by lazy {
        Paint().apply {
            this.isAntiAlias = true
            this.isDither = true
            this.color= Color.WHITE
            this.typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)
            this.textSize =  centerSize
            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 = if (unitSize == 0) topSize else unitSize.toFloat()
            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 = bottomSize
            this.style = Paint.Style.FILL_AND_STROKE
        }
    }
    private val paintBitmap by lazy{
        Paint() .apply {
            isAntiAlias=true
            isDither=true
            color=Color.WHITE
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.let {
            var distance= SizeUtil.px2dp(context!!,40f)
            //var r= Math.min(width/2f,height/2f)-10f  //最大半径
              r=  height/4f*3/2
            var centerY=height/2f //圆心位置

            it.drawCircle(width/2f,centerY,r,paintCircle) // 绘制外部圆环


            it.drawCircle(width/2f,centerY,r-distance,paintFullCircle)  //绘制内部实心圆

            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)
        if(drawables!=null){
            val bd = drawables as BitmapDrawable
            val bm: Bitmap = bd.getBitmap()
            canvas.drawBitmap(bm,width/2+allTxtLengthComm+(drawables!!.minimumWidth/2),hourBaseLIne-drawables!!.minimumHeight,paintBitmap)
        }
    }
    //绘制上部分文字
    fun setTopCan(canvas: Canvas, centetY:Float, r:Float){
        if(top_txt==null||top_txt==""){
            return
        }
        var txtLength=paintTime.measureText(top_txt)
        var txtf=paintTime.fontMetrics
        var txtBaseLIne=(height)/4f+( txtf.descent-txtf.ascent)/2-txtf.descent
        var wX=width/2f-txtLength/2
        canvas.drawText(top_txt,wX,txtBaseLIne,paintTime)
    }
    //绘制底部分文字
    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=(height)/4f*3+( txtf.descent-txtf.ascent)/2-txtf.descent
        var wX=width/2f-txtLength/2
        canvas.drawText(bottom_txt,wX,txtBaseLIne,paintTime)
    }

    fun setCenterBottom(center:String,bottom:String){
        center_txt=center
        bottom_txt=bottom
        invalidate()
    }


    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        when(event.action){
            MotionEvent.ACTION_DOWN->{
                var clickX=event.x
                var clickY=event.y
                if(clickX<=width/2+r&&clickX>=width/2-r&&clickY>height/2-r&&clickY<=height/2+r){
                    setClickListener(click)
                    return true
                }

            }
        }
        return super.dispatchTouchEvent(event)
    }

    interface MyClick{
        fun clickListener()
    }

    fun setClickListener(click:MyClick?){
        click?.let {
            it.clickListener()
        }


    }
}

