package com.xty.common.weight

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

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

    var firStr="1"
    var secStr="2"

    var title_1="信息填写"
    var title_2="设备设置"

    var circleR=0f
    var circleCentersize=0f
    var circleWidth=0f
    var titleSize=0f

    var nextStep=false

    private val paintFir by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL_AND_STROKE
            strokeWidth = circleWidth
            color = ContextCompat.getColor(context, R.color.col_009)

        }
    }
    private val paintSec by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.STROKE
            strokeWidth = circleWidth
            color = ContextCompat.getColor(context, R.color.col_009)
        }
    }
    private val paintTxt by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL
            color=ContextCompat.getColor(context, R.color.col_e0b)
            textSize = circleCentersize
        }
    }
    private val paintTitle by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL
            color=ContextCompat.getColor(context, R.color.col_009)
            textSize = titleSize
        }
    }

    init {
        var obtain =context.obtainStyledAttributes(attrs,R.styleable.ProcessView)
        circleR=obtain.getDimensionPixelSize(R.styleable.ProcessView_circle_r,0).toFloat()
        circleCentersize=obtain.getDimensionPixelSize(R.styleable.ProcessView_circle_center_size,0).toFloat()
        circleWidth=obtain.getDimensionPixelSize(R.styleable.ProcessView_circle_width,0).toFloat()
        titleSize= obtain.getDimensionPixelSize(R.styleable.ProcessView_title_size,0).toFloat()
        obtain.recycle()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        var fontH=paintTitle.fontMetrics


       var startH=  height/2-(circleR*2+  fontH.descent-fontH.ascent)/2+circleR

        val wMin = width / 3f
        canvas.drawLine(wMin,startH,wMin*2,startH,paintFir)


        canvas.drawCircle(wMin,startH,circleR,paintFir)


        val measureText = paintTxt.measureText(firStr)
        val fontMetrics = paintTxt.fontMetrics
        val fl = fontMetrics.descent - fontMetrics.ascent

         var base =startH+(fl/2f-fontMetrics.descent)

        paintTxt.color=Color.WHITE
        canvas.drawText(firStr,   wMin-measureText/2f,base,paintTxt)
        //第二个

        if(nextStep){
            drawAble(canvas,startH)
        }else{

            drawEnable(canvas,startH)
        }
        //底部分文字

        initTitle(canvas,startH)


    }

    /**
     * 流程名称
     */
    private fun initTitle(canvas: Canvas,startH:Float) {
        val wMin = width / 3f

        val measureText = paintTitle.measureText(title_1)
        val fontMetrics = paintTitle.fontMetrics
        val fl = fontMetrics.descent - fontMetrics.ascent

        var baesLins= fl/2+(  fl/2-fontMetrics.descent)

        var base =startH+circleR+ circleR+baesLins

        canvas.drawText(title_1,wMin-(measureText/2),base,paintTitle)

        canvas.drawText(title_2,(wMin*2)-(measureText/2),base,paintTitle)

    }

    private fun drawEnable(canvas: Canvas,startH: Float) {
        val wMin = width / 3f
        val measureText = paintTxt.measureText(firStr)
        val fontMetrics = paintTxt.fontMetrics
        val fl = fontMetrics.descent - fontMetrics.ascent

        var base =startH+(fl/2f-fontMetrics.descent)

        paintSec.color=Color.WHITE
        paintSec.style=Paint.Style.FILL_AND_STROKE
        canvas.drawCircle(wMin*2,startH,circleR,paintSec)
        paintSec.color= ContextCompat.getColor(context, R.color.col_009)
        paintSec.style= Paint.Style.STROKE
        canvas.drawCircle(wMin*2,startH,circleR,paintSec)

        paintTxt.color=ContextCompat.getColor(context, R.color.col_009)
        canvas.drawText(secStr,   wMin*2-measureText/2f,base,paintTxt)
    }

    private fun drawAble(canvas: Canvas,startH: Float) {
        val wMin = width / 3f
        val measureText = paintTxt.measureText(firStr)
        val fontMetrics = paintTxt.fontMetrics
        val fl = fontMetrics.descent - fontMetrics.ascent

        var base =startH+(fl/2f-fontMetrics.descent)

        paintSec.color=ContextCompat.getColor(context, R.color.col_009)
        paintSec.style=Paint.Style.FILL_AND_STROKE
        canvas.drawCircle(wMin*2,startH,circleR,paintSec)

        paintTxt.color=ContextCompat.getColor(context, R.color.white)
        canvas.drawText(secStr,   wMin*2-measureText/2f,base,paintTxt)
    }
}