package com.gh.gamecenter.common.view.titlebar

import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.view.ContextThemeWrapper
import androidx.constraintlayout.widget.ConstraintLayout
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toColor
import splitties.views.dsl.constraintlayout.*
import splitties.views.dsl.core.add
import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.wrapContent

/**
 * 面板标题栏
 * 一般用于底部拉起面板
 */
class BottomSheetTitleView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr, defStyleRes) {

    private lateinit var config: Config

    private var leftView: View? = null
    private var rightView: View? = null

    init {
        setBackgroundResource(R.drawable.background_shape_white_radius_12_top_only)
        initAttrs(context, attrs)
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.BottomSheetTitleView)
        val leftStyle = typedArray.getInt(R.styleable.BottomSheetTitleView_left_style, BUTTON_STYLE_TEXT)
        val leftText = typedArray.getString(R.styleable.BottomSheetTitleView_left_text) ?: ""
        val leftTextColor = typedArray.getColor(R.styleable.BottomSheetTitleView_left_text_color, -1)
        val leftIconResource = typedArray.getResourceId(R.styleable.BottomSheetTitleView_left_icon_resource, -1)
        val leftIconColor = typedArray.getColor(R.styleable.BottomSheetTitleView_left_icon_color, -1)
        val leftButtonBackgroundResource =
            typedArray.getResourceId(R.styleable.BottomSheetTitleView_left_button_background_resource, -1)
        val title = typedArray.getString(R.styleable.BottomSheetTitleView_title) ?: ""
        val rightStyle = typedArray.getInt(R.styleable.BottomSheetTitleView_right_style, -1)
        val rightText = typedArray.getString(R.styleable.BottomSheetTitleView_right_text) ?: ""
        val rightTextColor = typedArray.getColor(R.styleable.BottomSheetTitleView_right_text_color, -1)
        val rightIconResource = typedArray.getResourceId(R.styleable.BottomSheetTitleView_right_icon_resource, -1)
        val rightIconColor = typedArray.getColor(R.styleable.BottomSheetTitleView_right_icon_color, -1)
        val rightButtonBackgroundResource =
            typedArray.getResourceId(R.styleable.BottomSheetTitleView_right_button_background_resource, -1)

        config = Config(
            leftStyle = leftStyle,
            leftText = leftText,
            leftTextColor = leftTextColor,
            leftIconResource = leftIconResource,
            leftIconColor = leftIconColor,
            leftButtonBackgroundResource = leftButtonBackgroundResource,
            title = title,
            rightStyle = rightStyle,
            rightText = rightText,
            rightTextColor = rightTextColor,
            rightIconResource = rightIconResource,
            rightIconColor = rightIconColor,
            rightButtonBackgroundResource = rightButtonBackgroundResource
        )

        typedArray.recycle()

        initView()
    }

    private fun initView() {
        leftView = when (config.leftStyle) {
            BUTTON_STYLE_TEXT -> createTextView(context, true)

            BUTTON_STYLE_ICON -> createIconView(context, true)

            BUTTON_STYLE_BUTTON -> createButtonView(context, true)

            BUTTON_STYLE_TITLE -> createTitleView(context)

            else -> null

        }
        leftView?.let {

            add(it, lParams(it.wrapContent, it.matchParent) {
                startOfParent()
                topOfParent()
                bottomOfParent()
            })
        }

        rightView = when (config.rightStyle) {
            BUTTON_STYLE_TEXT -> createTextView(context, false)

            BUTTON_STYLE_ICON -> createIconView(context, false)

            BUTTON_STYLE_BUTTON -> createButtonView(context, false)

            else -> null
        }
        rightView?.let {
            add(it, lParams(it.wrapContent, it.matchParent) {
                endOfParent()
                topOfParent()
                bottomOfParent()
            })
        }

        if (config.leftStyle != BUTTON_STYLE_TITLE) {
            createTitleView(context).let {
                add(it, lParams(it.wrapContent, it.matchParent) {
                    endOfParent()
                    startOfParent()
                    topOfParent()
                    bottomOfParent()
                })
            }
        }
    }

    private fun createTitleView(context: Context): View {
        val themedContext = ContextThemeWrapper(context, R.style.TextHeadline)
        val textView = TextView(themedContext)
        textView.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
        textView.gravity = Gravity.CENTER
        textView.setTextColor(R.color.text_primary.toColor(context))
        textView.text = config.title
        return textView
    }

    private fun createButtonView(
        context: Context,
        isLeft: Boolean
    ): View {
        val backgroundResId = if (isLeft) {
            config.leftButtonBackgroundResource
        } else {
            config.rightButtonBackgroundResource
        }
        val textColor = if (isLeft) {
            config.leftTextColor
        } else {
            config.rightTextColor
        }

        val text = if (isLeft) {
            config.leftText
        } else {
            config.rightText
        }

        val themedContext = ContextThemeWrapper(context, R.style.BtnMiniStyle)
        val textView = TextView(themedContext)
        if (backgroundResId == -1) {
            textView.setBackgroundResource(R.drawable.bg_common_button_fill_blue)
        } else {
            textView.setBackgroundResource(backgroundResId)
        }
        textView.height = 24F.dip2px()

        if (textColor == -1) {
            textView.setTextColor(R.color.text_aw_primary.toColor(context))
        } else {
            textView.setTextColor(textColor)
        }
        textView.text = text

        val btnContainer = LinearLayout(context)
        btnContainer.gravity = Gravity.CENTER
        btnContainer.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
        btnContainer.addView(textView)
        return btnContainer
    }

    private fun createIconView(context: Context, isLeft: Boolean): View {
        val iconResId = if (isLeft) config.leftIconResource else config.rightIconResource
        val iconColor = if (isLeft) config.leftIconColor else config.rightIconColor

        val imageView = ImageView(context)
        if (iconResId != -1) {
            val drawable = AppCompatResources.getDrawable(context, iconResId)
            imageView.setImageDrawable(drawable)
        }

        if (iconColor != -1) {
            imageView.imageTintList = ColorStateList.valueOf(iconColor)
        }

        imageView.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
        return imageView
    }

    private fun createTextView(context: Context, isLeft: Boolean): View {
        val text = if (isLeft) config.leftText else config.rightText
        val textColor = if (isLeft) config.leftTextColor else config.rightTextColor
        val textView = TextView(context)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14F)
        textView.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
        textView.gravity = Gravity.CENTER
        textView.text = text
        if (textColor == -1) {
            textView.setTextColor(R.color.primary_theme.toColor(context))
        } else {
            textView.setTextColor(textColor)
        }
        return textView
    }

    fun setOnLeftClickListener(onClick: (View) -> Unit) {
        leftView?.setOnClickListener(onClick)
    }

    fun setOnRightClickListener(onClick: (View) -> Unit) {
        rightView?.setOnClickListener(onClick)
    }

    companion object {

        private const val BUTTON_STYLE_TEXT = 0 // 纯文本
        private const val BUTTON_STYLE_ICON = 1  // 图标
        private const val BUTTON_STYLE_BUTTON = 2 // 按钮
        private const val BUTTON_STYLE_TITLE = 3    // 标题
    }

    class Config(
        val leftStyle: Int = -1,
        val leftText: String = "",
        val leftTextColor: Int = -1,
        val leftIconResource: Int = -1,
        val leftIconColor: Int = -1,
        val leftButtonBackgroundResource: Int = -1,
        val title: String = "",
        val rightStyle: Int = -1,
        val rightText: String = "",
        val rightTextColor: Int = -1,
        val rightIconResource: Int = -1,
        val rightIconColor: Int = -1,
        val rightButtonBackgroundResource: Int = -1
    )
}