package com.gh.gamecenter.common.view

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Shader
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.StateListDrawable
import android.graphics.drawable.shapes.RectShape
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import com.gh.gamecenter.common.HaloApp
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.setDrawableStart
import com.gh.gamecenter.core.utils.DisplayUtils


object DrawableView {

    @JvmStatic
    fun getServerDrawableBySource(@ColorRes colorId: Int): Drawable {
        return getServerDrawable(ContextCompat.getColor(HaloApp.getInstance(), colorId))
    }

    @JvmStatic
    fun getServerDrawable(colorCode: String): Drawable {
        return getServerDrawable(Color.parseColor(colorCode))
    }

    @JvmStatic
    fun getServerDrawable(colorCode: Int): Drawable {
        val drawable = GradientDrawable()
        drawable.setColor(colorCode)
        drawable.cornerRadius = DisplayUtils.dip2px(2F).toFloat()
        return drawable
    }

    @JvmStatic
    fun getOvalDrawable(
        @ColorRes colorId: Int,
        radius: Float = 999F
    ): Drawable {
        val drawable = GradientDrawable()
        drawable.setColor(ContextCompat.getColor(HaloApp.getInstance(), colorId))
        drawable.cornerRadius = DisplayUtils.dip2px(radius).toFloat()
        return drawable
    }

    @JvmStatic
    fun getOvalDrawable(
        @ColorRes colorId: Int,
        radius: Float = 999F,
        context: Context = HaloApp.getInstance()
    ): Drawable {
        val drawable = GradientDrawable()
        drawable.setColor(ContextCompat.getColor(context, colorId))
        drawable.cornerRadius = DisplayUtils.dip2px(radius).toFloat()
        return drawable
    }

    @JvmStatic
    fun getOvalSelectorStyle(
        @ColorRes defaultColorId: Int,
        @ColorRes checkColorId: Int,
        context: Context = HaloApp.getInstance()
    ): StateListDrawable {
        val res = StateListDrawable()
        res.addState(intArrayOf(android.R.attr.state_checked), getOvalDrawable(checkColorId, context = context))
        res.addState(intArrayOf(), getOvalDrawable(defaultColorId, context = context))
        return res
    }

    @JvmStatic
    fun getSelectorColorStyle(
        @ColorRes defaultColorId: Int,
        @ColorRes checkColorId: Int,
        context: Context = HaloApp.getInstance()
    ): ColorStateList {
        val states = arrayOf(
            intArrayOf(-android.R.attr.state_checked),
            intArrayOf(android.R.attr.state_checked)
        )
        val colors = intArrayOf(
            ContextCompat.getColor(context, defaultColorId),
            ContextCompat.getColor(context, checkColorId)
        )
        return ColorStateList(states, colors)
    }

    @JvmStatic
    fun getStrokeDrawable(
        @ColorRes colorCode: Int,
        strokeWidth: Float = 1F,
        radius: Float = 999F,
        context: Context = HaloApp.getInstance()
    ): Drawable {
        val drawable = GradientDrawable()
        drawable.setStroke(
            strokeWidth.dip2px(),
            ContextCompat.getColor(context, colorCode)
        )
        drawable.cornerRadius = radius
        return drawable
    }

    @JvmStatic
    fun setTextDrawable(textView: TextView, @DrawableRes drawableId: Int?, text: String? = null) {
        val drawable = if (drawableId != null) {
            ContextCompat.getDrawable(HaloApp.getInstance(), drawableId)
        } else {
            null
        }
        textView.setDrawableStart(drawable)
        if (text != null) textView.text = text
    }

    // 需要跳转角度请自行设置 x0,y0,x1,y1
    @JvmStatic
    fun getGradientDrawable(width: Int, startColor: Int, endColor: Int): Drawable {
        val drawable = ShapeDrawable(RectShape())
        drawable.paint.shader = LinearGradient(
            0f,
            0F,
            width.toFloat(),
            0F,
            startColor,
            endColor,
            Shader.TileMode.REPEAT
        )
        return drawable
    }

    @JvmStatic
    fun getCornerDrawable(
        color: Int,
        radiusLT: Float,
        radiusRT: Float,
        radiusRB: Float,
        radiusLB: Float
    ): Drawable {
        val drawable = GradientDrawable()
        drawable.shape = GradientDrawable.RECTANGLE
        drawable.setColor(color)
        val radiusEach = floatArrayOf(
            radiusLT.dip2px().toFloat(),
            radiusLT.dip2px().toFloat(),
            radiusRT.dip2px().toFloat(),
            radiusRT.dip2px().toFloat(),
            radiusRB.dip2px().toFloat(),
            radiusRB.dip2px().toFloat(),
            radiusLB.dip2px().toFloat(),
            radiusLB.dip2px().toFloat()
        )
        drawable.cornerRadii = radiusEach
        return drawable
    }

    @JvmStatic
    fun getCornerGradientDrawable(startColor: Int, endColor: Int, radius: Float = 999F): Drawable {
        val drawable = GradientDrawable(
            GradientDrawable.Orientation.LEFT_RIGHT,
            intArrayOf(startColor, endColor)
        )
        drawable.cornerRadius = DisplayUtils.dip2px(radius).toFloat()
        return drawable
    }

    @JvmStatic
    fun getGradientDrawable(
        startColor: Int,
        endColor: Int,
        orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.LEFT_RIGHT,
        radius: Float = 999F
    ): Drawable {
        val drawable = GradientDrawable(orientation, intArrayOf(startColor, endColor))
        drawable.cornerRadius = DisplayUtils.dip2px(radius).toFloat()
        return drawable
    }

    fun convertAlphaKey(percent: Int): String {
        val hexString = Integer.toHexString(Math.round((255 * percent / 100).toFloat()))
        return (if (hexString.length < 2) "0" else "") + hexString
    }

    @JvmStatic
    fun getCheckSelectorDrawable(context: Context) =
        StateListDrawable().apply {
            addState(
                intArrayOf(android.R.attr.state_checked),
                AppCompatResources.getDrawable(context, R.drawable.ic_selector_selected)
            )
            addState(intArrayOf(), AppCompatResources.getDrawable(context, R.drawable.ic_selector_default))
        }
}