package com.xty.common.weight

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import com.xty.common.R

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

    var text = "01-01"

    var progressFir = 0f  //百分比
    var progressSec = 0f  //百分比
    var progressThr = 0f  //百分比
    var progressFour = 0f  //百分比


    var progressWidth = 0f
    var dataTime = 0f

    private val paintProgress by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL
            strokeWidth = progressWidth
            color = ContextCompat.getColor(context, R.color.col_c3d)
            strokeCap = Paint.Cap.ROUND  //坑的一批   左右两边的半圆半径 等于线宽一半
        }
    }
    private val paintFir by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL
            strokeWidth = progressWidth
            color = ContextCompat.getColor(context, R.color.col_009)

        }
    }
    private val paintSec by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL
            strokeWidth = progressWidth
            color = ContextCompat.getColor(context, R.color.col_e0b)
        }
    }
    private val paintThr by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL
            strokeWidth = progressWidth
            color = ContextCompat.getColor(context, R.color.col_ea7)
        }
    }


    private val paintTxt by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            style = Paint.Style.FILL
            textSize = dataTime
        }
    }

    init {
        val obtain = context.obtainStyledAttributes(attrs, R.styleable.MyProgressView)

        progressWidth = obtain.getFloat(R.styleable.MyProgressView_progress_width, 0f)
        progressFir = obtain.getFloat(R.styleable.MyProgressView_progress_fir, 0f)
        progressSec = obtain.getFloat(R.styleable.MyProgressView_progress_sec, 0f)
        progressThr = obtain.getFloat(R.styleable.MyProgressView_progress_thr, 0f)
        progressFour = obtain.getFloat(R.styleable.MyProgressView_progress_four, 0f)
        dataTime = obtain.getFloat(R.styleable.MyProgressView_data_size, 0f)
        obtain.recycle()


    }

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

        val fontMetrics = paintTxt.fontMetrics
        val fl = fontMetrics.descent - fontMetrics.ascent



        var y = height / 2f - (fl+progressWidth) / 2
        //绘制总长度
        canvas.drawLine( progressWidth/2+paddingLeft, y, width.toFloat()-(progressWidth/2)-paddingRight, y, paintProgress)

       var allSize= progressFir+progressSec+progressThr+progressFour
        var allProgress=width-progressWidth -paddingRight-paddingLeft//总长度
        //头部半圆  按顺序 哪个不为零  则头部绘制哪个半圆
        var rect=RectF(paddingLeft.toFloat(),y-(progressWidth/2),progressWidth+paddingLeft,(progressWidth/2)+y)
        if(progressFir!=0f){
            canvas.drawArc(rect,90f,180f,true,paintFir)
        }else{
            if(progressSec!=0f){
                canvas.drawArc(rect,90f,180f,true,paintSec)
            }else{
                if(progressThr!=0f){
                    canvas.drawArc(rect,90f,180f,true,paintThr)
                }
            }
        }
        //第一个
        var first= progressFir/allSize *allProgress
        canvas.drawLine(progressWidth/2+paddingLeft, y, progressWidth/2+first+paddingLeft, y, paintFir)
        //第二个
        var sec= progressSec/allSize *allProgress
        canvas.drawLine(progressWidth/2+paddingLeft+first, y, progressWidth/2+first+sec+paddingLeft, y, paintSec)
        //第三
        var threed= progressThr/allSize *allProgress
        canvas.drawLine(progressWidth/2+paddingLeft+first+sec, y, progressWidth/2+first+sec+threed+paddingLeft, y, paintThr)


        //  尾部半圆 倒叙来如果为0  则不去绘制 直到最后有绘制的绘制不绘制
        var rectEnd=RectF(width.toFloat()-progressWidth-paddingRight,y-(progressWidth/2),width.toFloat()-paddingRight,(progressWidth/2)+y)
        if(progressFour==0f){
            if(progressThr==0f){
                if(progressSec==0f){
                    if(progressFir==0f){
                    }else{
                        canvas.drawArc(rectEnd,270f,180f,true,paintFir)
                    }
                }else{
                    canvas.drawArc(rectEnd,270f,180f,true,paintSec)
                }
            }else{
                canvas.drawArc(rectEnd,270f,180f,true,paintThr)
            }
        }

        //  绘制底下文字
        var baselineHeight = height / 2 + (fl+progressWidth) / 2 - fontMetrics.descent
        canvas.drawText(text, paddingLeft.toFloat(), baselineHeight, paintTxt)
    }

}