package com.remax.visualnovel.widget.uitoken.view

import android.content.Context
import android.graphics.drawable.ClipDrawable
import android.graphics.drawable.LayerDrawable
import android.util.AttributeSet
import android.view.Gravity
import android.widget.ProgressBar
import com.remax.visualnovel.R
import com.remax.visualnovel.widget.uitoken.bean.CustomViewToken
import com.remax.visualnovel.widget.uitoken.getGradientDrawable

/**
 * Created by HJW on 2022/8/31
 *
 */
class UITokenProgressBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    ProgressBar(context, attrs, defStyleAttr), UIView {

    val customViewToken = CustomViewToken()

    init {
        /**
         * 自定义控件属性
         */
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.UITokenProgressBar)
        customViewToken.run {
            backgroundUIColorToken = typedArray.getString(R.styleable.UITokenProgressBar_backgroundColorToken) ?: ""
            backgroundUIPressedColorToken = typedArray.getString(R.styleable.UITokenProgressBar_backgroundPressedColorToken) ?: ""
            val radius = typedArray.getString(R.styleable.UITokenProgressBar_radiusToken) ?: ""
            radiusToken = radius
            topLeftRadiusToken = (typedArray.getString(R.styleable.UITokenProgressBar_topLeftRadiusToken) ?: "").ifEmpty { radius }
            topRightRadiusToken = (typedArray.getString(R.styleable.UITokenProgressBar_topRightRadiusToken) ?: "").ifEmpty { radius }
            bottomLeftRadiusToken = (typedArray.getString(R.styleable.UITokenProgressBar_bottomLeftRadiusToken) ?: "").ifEmpty { radius }
            bottomRightRadiusToken = (typedArray.getString(R.styleable.UITokenProgressBar_bottomRightRadiusToken) ?: "").ifEmpty { radius }
        }
        typedArray.recycle()

        val backDrawable = getGradientDrawable(
            customViewToken.backgroundUIColorToken,
            customViewToken.topLeftRadiusToken,
            customViewToken.topRightRadiusToken,
            customViewToken.bottomRightRadiusToken,
            customViewToken.bottomLeftRadiusToken,
        )

        val gradientDrawable = getGradientDrawable(
            customViewToken.backgroundUIPressedColorToken,
            customViewToken.topLeftRadiusToken,
            customViewToken.topRightRadiusToken,
            customViewToken.bottomRightRadiusToken,
            customViewToken.bottomLeftRadiusToken,
        )

        val bgClipDrawable = ClipDrawable(backDrawable, Gravity.START, ClipDrawable.HORIZONTAL)
        bgClipDrawable.level = 10000
        val progressClip = ClipDrawable(gradientDrawable, Gravity.START, ClipDrawable.HORIZONTAL)

        val layerDrawable = LayerDrawable(arrayOf(bgClipDrawable, progressClip, progressClip))
        layerDrawable.setId(0, android.R.id.background)
        layerDrawable.setId(1, android.R.id.secondaryProgress)
        layerDrawable.setId(2, android.R.id.progress)
        progressDrawable = layerDrawable
        progress = progress
    }

    override fun getUITokenView(): CustomViewToken {
        return customViewToken
    }
}