package com.gh.gamecenter.common.view

import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.util.AttributeSet
import android.view.View
import android.widget.ProgressBar
import androidx.core.content.ContextCompat
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.utils.dip2px

class ThumbProgressBar : ProgressBar {
    private var mThumbDrawable: Drawable? = null

    //Thumb的宽度
    private var mThumbDrawableWidth = 0

    //Thumb的高度
    private var mThumbDrawableHeight = 0

    //进度条高度
    private var mProgressBarHeight = 12f.dip2px()

    //Thumb偏移量
    private val mDefaultThumbOffset = 13f.dip2px()

    //ProgressBar左右偏移量
    private val mProgressBarPadding = 12f.dip2px()

    private var mPaint: Paint

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        mThumbDrawable = ContextCompat.getDrawable(context, R.drawable.icon_package_check_rocket)
        mThumbDrawableWidth = mThumbDrawable?.intrinsicWidth ?: 0
        mThumbDrawableHeight = mThumbDrawable?.intrinsicHeight ?: 0
        mPaint = Paint()
        mPaint.isAntiAlias = true
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val dh = if (mThumbDrawableHeight > mProgressBarHeight) mThumbDrawableHeight else mProgressBarHeight
        setMeasuredDimension(
            View.getDefaultSize(0, widthMeasureSpec),
            dh
        )
        //LayerDrawable使用setBounds可能无效
        if (progressDrawable is LayerDrawable) {
            (progressDrawable as LayerDrawable).setLayerInset(
                0,
                mProgressBarPadding,
                (mThumbDrawableHeight - mProgressBarHeight) / 2,
                mProgressBarPadding,
                (mThumbDrawableHeight - mProgressBarHeight) / 2
            )
            (progressDrawable as LayerDrawable).setLayerInset(
                1,
                mProgressBarPadding,
                (mThumbDrawableHeight - mProgressBarHeight) / 2,
                mProgressBarPadding,
                (mThumbDrawableHeight - mProgressBarHeight) / 2
            )
        } else {
            progressDrawable.setBounds(
                mProgressBarPadding,
                (mThumbDrawableHeight - mProgressBarHeight) / 2,
                measuredWidth - mProgressBarPadding,
                measuredHeight - (mThumbDrawableHeight - mProgressBarHeight) / 2
            )
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val percent = progress.toFloat() / max
        val progressWidth = measuredWidth - paddingLeft - paddingRight - mProgressBarPadding * 2
        var offsetX =
            progressWidth * percent + paddingLeft - mDefaultThumbOffset + mProgressBarPadding

        if (offsetX < 0) {
            offsetX = 0f
        }
        if (offsetX > measuredWidth - mThumbDrawableWidth) {
            offsetX = (measuredWidth - mThumbDrawableWidth).toFloat()
        }

        val bitmapDrawable = mThumbDrawable as BitmapDrawable
        val bitmap = bitmapDrawable.bitmap
        canvas.drawBitmap(bitmap, offsetX, 0f, mPaint)
    }
}