package cn.muchinfo.rma.view.autoWidget

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.drawable.*
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import cn.muchinfo.rma.R

import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButtonDrawable


fun Context.getDrawableWithSize(imageRes: Int, width: Int, height: Int, padding: Int = 0): Drawable {
    return getDrawableWithSize(ContextCompat.getDrawable(this, imageRes)!!, width, height, arrayOf(padding, padding, padding, padding))
}

fun Context.getDrawableWithSize(imageRes: Int, width: Int, height: Int, paddingLeft: Int = 0, paddingTop: Int = 0, paddingRight: Int = 0, paddingBottom: Int = 0): Drawable {
    return getDrawableWithSize(ContextCompat.getDrawable(this, imageRes)!!, width, height, arrayOf(paddingLeft, paddingTop, paddingRight, paddingBottom))
}

fun Context.getDrawableWithSize(drawable: Drawable, width: Int, height: Int, padding: Array<Int>): Drawable {
    if (drawable is BitmapDrawable) {
        val bitmap = drawable.bitmap
        val bitmapDrawable = BitmapDrawable(resources, Bitmap.createScaledBitmap(bitmap, width, height, true))
        fun getPadding(index: Int): Int = padding.getOrNull(index) ?: 0
        val insetDrawable = InsetDrawable(bitmapDrawable, getPadding(0), getPadding(1), getPadding(2), getPadding(3))
        return LayerDrawable(arrayOf(insetDrawable))
    }
    return drawable
}

//fun Context.getTintDrawable(imageSrc: Int, tintColorStr: String, statusColor: Boolean = true): Drawable = getTintDrawable(ContextCompat.getDrawable(this, imageSrc)!!, Color.parseColor(tintColorStr), statusColor)
//fun Context.getTintDrawable(imageSrc: Int, tintColorStr: Int, statusColor: Boolean = true): Drawable = getTintDrawable(ContextCompat.getDrawable(this, imageSrc)!!, tintColorStr, statusColor)
//fun Context.getTintDrawable(imageSrc: Drawable, tintColorStr: String, statusColor: Boolean = true): Drawable = getTintDrawable(imageSrc, Color.parseColor(tintColorStr), statusColor)
//fun Context.getTintDrawable(imageSrc: Drawable, tintColor: Int, statusColor: Boolean = true): Drawable {
//    val drawableNormal = imageSrc.constantState?.newDrawable()?.mutate()
//    if (tintColor != 0) {
//        if (drawableNormal != null) {
//            DrawableCompat.setTint(drawableNormal, tintColor)
//        }
//    }
//    val shadeColor = ContextCompat.getColor(this, R.color.manager_normal_color)
//    return if (statusColor) drawableNormal?.let { PressedEffectStateListDrawable(it, shadeColor) } else drawableNormal
//}

fun createRoundRectStokeDrawable(color: String, corner: Int, stroke: Int = 1): Drawable = createRoundRectStokeDrawable(Color.parseColor(color), corner, stroke)
fun createRoundRectStokeDrawable(color: Int, corner: Int, stroke: Int = 1): Drawable = QMUIRoundButtonDrawable().apply {
    setIsRadiusAdjustBounds(false)
    setStroke(autoSize(stroke), color)
    cornerRadius = corner.toFloat()
}

fun createRoundRectDrawable(color: String, corner: Int, statusColor: Boolean = false): Drawable = createRoundRectDrawable(Color.parseColor(color), corner, statusColor)
fun createRoundRectDrawable(color: Int, corner: Int, statusColor: Boolean = false): Drawable {
    return if (statusColor) {
        createPressStateDrawable(
                createRectDrawable(color = color, corner = corner),
                createRectDrawable(color = color, corner = corner)
                        .apply { setColorFilter(Color.parseColor("#77FFFFFF"), PorterDuff.Mode.DARKEN) }
        )
    } else {
        createRectDrawable(color = color, corner = corner)
    }
}

private fun createRectDrawable(color: Int, corner: Int) = QMUIRoundButtonDrawable().apply {
    setIsRadiusAdjustBounds(false)
    setColor(color)
    cornerRadius = autoSize(corner).toFloat()
}

fun createCircleDrawable(color: String): Drawable = createCircleDrawable(Color.parseColor(color))
fun createCircleDrawable(color: Int): Drawable = QMUIRoundButtonDrawable().apply {
    setColor(color)
}

fun createSelectStateDrawable(normal: Drawable, selected: Drawable): Drawable {
    return StateListDrawable().apply {
        addState(intArrayOf(android.R.attr.state_selected), selected)
        addState(intArrayOf(), normal)
    }
}

fun createPressStateDrawable(normal: Drawable, selected: Drawable): Drawable {
    return StateListDrawable().apply {
        addState(intArrayOf(android.R.attr.state_pressed), selected)
        addState(intArrayOf(), normal)
    }
}