package com.ama.core.architecture.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.core.graphics.withSave
import com.ama.core.architecture.R
import com.ama.core.architecture.util.ResUtil

class CustomProgressBar(context: Context, attrs: AttributeSet) : View(context, attrs) {

    private var progress = 0
    private var max = 100

    private var mBgColor = ResUtil.getColor(R.color.gray_d3)
    private var mForColor = ResUtil.getColor(R.color.green_09)
    private var mEnableTouch: Boolean = false



    private val paint = Paint().apply {
        isAntiAlias = true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val width = measuredWidth.toFloat()
        val height = measuredHeight.toFloat()



        canvas.withSave {
            paint.color = mBgColor
            canvas.drawRoundRect(0f, 0f, width, height, height/2, height/2, paint)
            val completeProgressWidth = (width) * progress / max
            if (completeProgressWidth > 0) {
                paint.color = mForColor
                canvas.drawRoundRect(0f, 0f, completeProgressWidth, height, height/2, height/2, paint)
            }
        }
    }


    fun setProgress(progress: Int) {
        this.progress = progress.coerceIn(0, max)
        invalidate()
    }

    fun setMax(max: Int) {
        this.max = max
        invalidate()
    }

    fun enableTouch(enableTouch: Boolean) {
        mEnableTouch = enableTouch
    }

    fun setBarColor(bgColor: Int=R.color.gray_d3, forColor: Int=R.color.green_09) {
        mBgColor = ResUtil.getColor(bgColor)
        mForColor = ResUtil.getColor(forColor)
        invalidate()
    }





    //--------------------------- New added ----------------------------
    private var onProgressChangeListener: OnProgressChangeListener? = null

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (mEnableTouch) {
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    parent.requestDisallowInterceptTouchEvent(true)
                    updateProgressFromTouch(event.x)
                    return true
                }
                MotionEvent.ACTION_MOVE -> {
                    updateProgressFromTouch(event.x)
                    return true
                }
                MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                    parent.requestDisallowInterceptTouchEvent(false)
                    updateProgressFromTouch(event.x, true)
                    return true
                }
            }
        }

        return super.onTouchEvent(event)
    }

    private fun updateProgressFromTouch(touchX: Float, notifyNewProgress: Boolean = false) {
        val width = width.toFloat()
        if (width > 0) {
            var newProgress = (touchX / width * max).toInt()
            newProgress = newProgress.coerceIn(0, max)

            if (newProgress != progress) {
                setProgress(newProgress)

                if (notifyNewProgress) {
                    onProgressChangeListener?.onProgressChanged(newProgress)
                }
            }
        }
    }

    fun setOnProgressChangeListener(listener: OnProgressChangeListener) {
        this.onProgressChangeListener = listener
    }

    interface OnProgressChangeListener {
        fun onProgressChanged(progress: Int)
    }


}



