package com.xty.common.weight

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

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

    private var stepFir=true
    private var stepSec=false
    private var stepThr=false
    val line by lazy { width / 3 }  //线的长度
    var fonSize = 0f
    val paintLine by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            strokeWidth=SizeUtil.px2dp(getContext(),10f)
            style = Paint.Style.FILL_AND_STROKE
            color = ContextCompat.getColor(getContext(), R.color.col_d1d)
        }
    }

    val paintCircle by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL_AND_STROKE
            color = ContextCompat.getColor(getContext(), R.color.col_02c)
        }
    }

    val paintCircleNone by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL_AND_STROKE
            color = ContextCompat.getColor(getContext(), R.color.col_f1f)
        }
    }

    val paintText by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            textSize = fonSize
            style = Paint.Style.FILL_AND_STROKE
            color = ContextCompat.getColor(getContext(), R.color.white)
        }
    }

    init {
        val obtainStyledAttributes = context!!.obtainStyledAttributes(attrs, R.styleable.StepView)
        fonSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.StepView_step_size, 0)
            .toFloat()
        obtainStyledAttributes.recycle()


    }

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

        drawFistLine(canvas)
        drawSecLine(canvas)

        drawFirstCircle(canvas)
        drawSectCircle(canvas)
        drawThrCircle(canvas)

    }

    private fun drawFirstCircle(canvas: Canvas) {
        var str = "1"
        val measureText = paintText.measureText(str)
        var r = measureText / 2 + 10f

        paintText.color=ContextCompat.getColor(context, R.color.white)
        paintCircle.color=ContextCompat.getColor(context, R.color.col_02c)
        paintCircle.style=Paint.Style.FILL_AND_STROKE

        canvas.drawCircle(line.toFloat(), height / 2f, r, paintCircle)

        val fontMetrics = paintText.fontMetrics
        var fontHeight = fontMetrics.descent - fontMetrics.ascent
        var baseLines = height / 2f + fontHeight / 2 - fontMetrics.descent
        canvas.drawText(str, line - measureText / 2, baseLines, paintText)
    }

    private fun drawSectCircle(canvas: Canvas) {
        if(stepSec){
            paintText.color=ContextCompat.getColor(context, R.color.white)
            paintCircle.color=ContextCompat.getColor(context, R.color.col_02c)
            paintCircle.style=Paint.Style.FILL_AND_STROKE
        }else{
            paintText.color=ContextCompat.getColor(context, R.color.col_d1d)
            paintCircle.color=ContextCompat.getColor(context, R.color.col_d1d)
            paintCircle.style=Paint.Style.STROKE

        }
        var str = "2"
        val measureText = paintText.measureText(str)
        var r = measureText / 2 + 10f

        if(!stepSec){
            canvas.drawCircle(line+line/2.toFloat(), height / 2f, r, paintCircleNone)
        }
        canvas.drawCircle(line+line/2.toFloat(), height / 2f, r, paintCircle)

        val fontMetrics = paintText.fontMetrics
        var fontHeight = fontMetrics.descent - fontMetrics.ascent
        var baseLines = height / 2f + fontHeight / 2 - fontMetrics.descent
        canvas.drawText(str, line+line/2 - measureText / 2, baseLines, paintText)
    }

    private fun drawThrCircle(canvas: Canvas) {
        if(stepThr){
            paintText.color=ContextCompat.getColor(context, R.color.white)
            paintCircle.color=ContextCompat.getColor(context, R.color.col_02c)
            paintCircle.style=Paint.Style.FILL_AND_STROKE
        }else{
            paintText.color=ContextCompat.getColor(context, R.color.col_d1d)
            paintCircle.color=ContextCompat.getColor(context, R.color.col_d1d)
            paintCircle.style=Paint.Style.STROKE
        }
        var str = "3"
        val measureText = paintText.measureText(str)
        var r = measureText / 2 + 10f
        if(!stepThr){
            canvas.drawCircle(line*2f.toFloat(), height / 2f, r, paintCircleNone)
        }
        canvas.drawCircle(line*2f, height / 2f, r, paintCircle)

        val fontMetrics = paintText.fontMetrics
        var fontHeight = fontMetrics.descent - fontMetrics.ascent
        var baseLines = height / 2f + fontHeight / 2 - fontMetrics.descent
        canvas.drawText(str, line*2 - measureText / 2, baseLines, paintText)
    }

    private fun drawFistLine(canvas: Canvas) {
        if(stepSec){
            paintLine.color=ContextCompat.getColor(context, R.color.col_02c)
        }else{
            paintLine.color=ContextCompat.getColor(context, R.color.col_d1d)
        }
        canvas.drawLine(line.toFloat(), height / 2f, line + line / 2f, height / 2f, paintLine)
    }

    private fun drawSecLine(canvas: Canvas) {
        if(stepThr){
            paintLine.color=ContextCompat.getColor(context, R.color.col_02c)
        }else{
            paintLine.color=ContextCompat.getColor(context, R.color.col_d1d)
        }
        canvas.drawLine(line + line / 2f, height / 2f, line * 2f, height / 2f, paintLine)
    }


    fun setStep(sec:Boolean,thr:Boolean=false){
        stepSec=sec
        stepThr=thr
        invalidate()

    }
}
