package com.base.scanqrclear.luma

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.Shader
import android.util.AttributeSet
import android.view.View
import com.base.scanqrclear.R

class MyProgressBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var progress: Int = 0
    private var maxProgress: Int = 100
    private var cornerRadius: Float = 0f
    private var progressColor: Int = 0
    private var backgroundColor: Int = 0
    private var startColor: Int = 0
    private var endColor: Int = 0
    private var progressText: String = ""
    private var textColor: Int = 0
    private var progressDirection: Int = PROGRESS_DIRECTION_LEFT_TO_RIGHT

    private var shader: Shader? = null

    private val paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
        strokeWidth = 5f
    }

    private val rect by lazy { RectF(0f, 0f, width.toFloat(), height.toFloat()) }
    private val rectProgress by lazy { RectF() }
    private val textPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG).apply {
            style = Paint.Style.FILL
            textSize = 28f
            color = textColor
        }
    }

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyProgressBar)
        progress = typedArray.getInt(R.styleable.MyProgressBar_progress, 0)
        maxProgress = typedArray.getInt(R.styleable.MyProgressBar_max, 100)
        cornerRadius = typedArray.getDimension(R.styleable.MyProgressBar_cRadius, 0f)
        progressColor = typedArray.getColor(R.styleable.MyProgressBar_pColor, 0)
        backgroundColor = typedArray.getColor(R.styleable.MyProgressBar_pBgColor, 0)
        startColor = typedArray.getColor(R.styleable.MyProgressBar_pStartColor, 0)
        endColor = typedArray.getColor(R.styleable.MyProgressBar_pEndColor, 0)
        progressText = typedArray.getString(R.styleable.MyProgressBar_pText) ?: ""
        textColor = typedArray.getColor(R.styleable.MyProgressBar_pTextColor, 0)
        progressDirection = typedArray.getInt(
            R.styleable.MyProgressBar_progressDirection,
            PROGRESS_DIRECTION_LEFT_TO_RIGHT
        )
        typedArray.recycle()
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        paint.color = backgroundColor
        canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint)

        val progressWidth = (width * progress / maxProgress).toFloat()
        when (progressDirection) {
            PROGRESS_DIRECTION_LEFT_TO_RIGHT -> {
                rectProgress.set(0f, 0f, progressWidth, height.toFloat())
            }

            PROGRESS_DIRECTION_RIGHT_TO_LEFT -> {
                rectProgress.set(width - progressWidth, 0f, width.toFloat(), height.toFloat())
            }
        }

        paint.color = progressColor
        if (startColor != 0 && endColor != 0) {
            shader = LinearGradient(
                0f,
                0f,
                progressWidth,
                0f,
                startColor,
                endColor,
                Shader.TileMode.CLAMP
            )
            paint.shader = shader
            canvas.drawRoundRect(rectProgress, cornerRadius, cornerRadius, paint)
            paint.shader = null
        } else {
            canvas.drawRoundRect(rectProgress, cornerRadius, cornerRadius, paint)
        }

        if (progressText.isNotEmpty()) {
            val textWidth = textPaint.measureText(progressText)
            val textX = width - paddingRight - textWidth - 34
            canvas.drawText(progressText, textX, (height / 2).toFloat() + 10, textPaint)
        }
    }

    fun setProgress(progress: Int) {
        this.progress = progress.coerceAtMost(maxProgress)
        invalidate()
    }

    fun getProgress(): Int = progress

    fun setColor(backgroundColor: Int, progressColor: Int) {
        this.backgroundColor = backgroundColor
        this.progressColor = progressColor
        invalidate()
    }

    companion object {
        const val PROGRESS_DIRECTION_LEFT_TO_RIGHT = 0
        const val PROGRESS_DIRECTION_RIGHT_TO_LEFT = 1
    }
}