package com.remax.visualnovel.app.delegate

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.LayoutToolbarBinding
import com.remax.visualnovel.extension.findActivityContext
import com.remax.visualnovel.extension.getBgColor
import com.remax.visualnovel.extension.getNavHeight
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.extension.setSize
import com.remax.visualnovel.utils.StatusBarUtils
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.ui.buttons.IconButtonView
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.expend.dsl.expand
import com.remax.visualnovel.widget.uitoken.handleUIToken
import com.dylanc.loadingstateview.BaseToolbarViewDelegate
import com.dylanc.loadingstateview.NavBtnType
import com.dylanc.loadingstateview.ToolbarConfig
import com.dylanc.loadingstateview.toolbarExtras

var ToolbarConfig.titleTextColorToken: Int? by toolbarExtras()
var ToolbarConfig.titleTextColor: Int? by toolbarExtras()
var ToolbarConfig.titleTextAlpha: Float? by toolbarExtras()

/**
 * 整个导航栏相关
 */
var ToolbarConfig.navBgColorToken: Int? by toolbarExtras()
var ToolbarConfig.navBgColor: Int? by toolbarExtras()
var ToolbarConfig.navBgAlpha: Float? by toolbarExtras() //一般和isFull = true 配合使用，因为只有当navbar和content布局重叠时，才需要滑动渐隐导航栏
var ToolbarConfig.navIsShow: Boolean? by toolbarExtras()

var ToolbarConfig.confirmEnabled: Boolean? by toolbarExtras()
var ToolbarConfig.isFull: Boolean? by toolbarExtras()
var ToolbarConfig.confirmContent: String? by toolbarExtras()
var ToolbarConfig.contractUSEnabled: Boolean? by toolbarExtras()

/**
 * 返回键相关
 */
var ToolbarConfig.navBackColorToken: Int? by toolbarExtras()
var ToolbarConfig.navBackColor: Int? by toolbarExtras()
var ToolbarConfig.onNavBackClick: (() -> Unit)? by toolbarExtras()
var ToolbarConfig.onNavBackIconToken: Int? by toolbarExtras()

var ToolbarConfig.iconButtonType: IconButtonType? by toolbarExtras()


enum class IconButtonType {
    ON_PIC, NORMAL
}

/**
 * 公共导航栏
 */
class ToolbarViewDelegate : BaseToolbarViewDelegate() {
    private lateinit var binding: LayoutToolbarBinding
    private var context: Context? = null

    override fun onCreateToolbar(inflater: LayoutInflater, parent: ViewGroup): View {
        context = parent.context
        binding = LayoutToolbarBinding.inflate(inflater, parent, false)
        return binding.root
    }

    override fun onBindToolbar(config: ToolbarConfig) {
        binding.apply {
            val expendSize = 4.dp
            tvTitle.text = config.title?.ifEmpty { " " } ?: " "
            context?.run {
                if (config.isFull == true) {
                    root.tag = "isFull"
                    (context?.findActivityContext() as? Activity)?.let {
                        StatusBarUtils.setTransparent(it)
                        navBg.setSize(height = it.getNavHeight())
                    }
                    navBg.alpha = 0f
                    tvTitle.alpha = 0f
                }
                //设置导航栏是否显示
                config.navIsShow?.let { root.isVisible = it }
                navBg.setBackgroundColor(getBgColor(config.colorType))
                //设置导航栏颜色
                config.navBgColorToken?.let { navBg.setBackgroundColor(handleUIToken(it)?.color ?: 0) }
                config.navBgColor?.let { navBg.setBackgroundColor(it) }
                //设置导航栏透明度
                config.navBgAlpha?.let { navBg.alpha = it }

                //设置标题颜色
                config.titleTextColorToken?.let { tvTitle.setTextColor(handleUIToken(it)?.color ?: 0) }
                config.titleTextColor?.let { tvTitle.setTextColor(it) }
                //设置标题透明度
                config.titleTextAlpha?.let { tvTitle.alpha = it }

                //设置返回按钮颜色
                config.navBackColorToken?.let { navBack.changeTextColor { textUIColorToken = getString(it) } }
                config.navBackColor?.let { navBack.setTextColor(it) }

                //设置confirm按钮是否可点
                config.confirmEnabled?.let { rightConfirmBtn.isEnabled = it }
                //设置confirm按钮的文字
                config.confirmContent?.let { rightConfirmBtn.text = it }
                //设置contract us按钮是否可点
                config.contractUSEnabled?.let { contractUsBtn.isEnabled = it }

                //设置iconfont按钮样式
                when (config.iconButtonType) {
                    IconButtonType.ON_PIC -> {
                        navBack.setButtonStyle(buttonName = IconButtonView.NavButton_OnPic)
                        rightIconBtn1.setButtonStyle(buttonName = IconButtonView.NavButton_OnPic)
                        rightIconBtn2.setButtonStyle(buttonName = IconButtonView.NavButton_OnPic)
                    }

                    else -> {
                        navBack.setButtonStyle(buttonName = IconButtonView.NavButton)
                        rightIconBtn1.setButtonStyle(buttonName = IconButtonView.NavButton)
                        rightIconBtn2.setButtonStyle(buttonName = IconButtonView.NavButton)
                    }
                }

                navBack.expand(expendSize, expendSize)
            }

            val setBackClick = {
                setOnClick(navBack) {
                    if (config.onNavBackClick != null) {
                        config.onNavBackClick?.invoke()
                    } else {
                        (context.findActivityContext() as? Activity)?.onBackPressed()
                    }
                }
            }

            when (config.navBtnType) {
                //navBack是返回按钮
                NavBtnType.BACK -> {
                    navBack.setText(R.string.icon_arrow_left_border)
                    setBackClick.invoke()
                    navBack.isVisible = true
                }
                //navBack是关闭按钮
                NavBtnType.ClOSE -> {
                    navBack.setText(R.string.icon_close)
                    setBackClick.invoke()
                    navBack.isVisible = true
                }

                //navBack是向下关闭按钮
                NavBtnType.DOWN -> {
                    navBack.setText(R.string.icon_arrow_down_border)
                    setBackClick.invoke()
                    navBack.isVisible = true
                }

                //navBack是自定义按钮
                NavBtnType.CUSTOM -> {
                    if (config.onNavBackIconToken != null) {
                        navBack.setText(config.onNavBackIconToken!!)
                        navBack.isVisible = true
                        setBackClick.invoke()
                    } else {
                        navBack.isInvisible = true
                    }
                }

                NavBtnType.INVISIBLE -> {
                    navBack.isInvisible = true
                }

                NavBtnType.NONE -> {
                    navBack.isVisible = false
                }
            }

            with(contractUsBtn) {
                if (config.contractUsBtnText != null) {
                    isVisible = true
                    val expandX = 24.dp
                    val expandY = 10.dp
                    expand(expandX, expandY)
                    if (config.contractUsBtnText != null) {
                        setText(config.contractUsBtnText ?: 0)
                    }
                    setOnClick(this) {
                        config.onContractUsBtnBlock?.invoke(this)
                    }
                } else {
                    isVisible = false
                    setOnClickListener(null)
                }
            }

            with(rightConfirmBtn) {
                if (config.showConfirmBtn == true) {
                    isVisible = true
                    setOnClick(this) {
                        config.onConfirmBtnBlock?.invoke(this)
                    }
                } else {
                    isVisible = false
                    setOnClickListener(null)
                }
            }

            with(rightIconBtn1) {
                if (config.showRightIconBtn1 != null) {
                    isVisible = true
                    setText(config.showRightIconBtn1!!)
                    expand(expendSize, expendSize)
                    setOnClick(this) {
                        config.onRightIconBtnBlock1?.invoke(this)
                    }
                } else {
                    isVisible = false
                    setOnClickListener(null)
                }
            }


            with(rightIconBtn2) {
                if (config.showRightIconBtn2 != null) {
                    isVisible = true
                    setText(config.showRightIconBtn2!!)
                    changeTextColor {
                        textUIColorToken = if (config.rightIconColorBtn2 != null) {
                            context.getString(config.rightIconColorBtn2!!)
                        } else {
                            context.getString(R.string.color_txt_primary_normal)
                        }
                    }

                    expand(expendSize, expendSize)
                    setOnClick(this) {
                        config.onRightIconBtnBlock2?.invoke(this)
                    }
                } else {
                    isVisible = false
                    setOnClickListener(null)
                }
            }

            with(rightBtn) {
                if (config.showRightBtn != null) {
                    isVisible = true
                    setText(config.showRightBtn!!)
                    setOnClick(this) {
                        config.onRightBtnBlock?.invoke(this)
                    }
                } else {
                    isVisible = false
                    setOnClickListener(null)
                }
            }

        }
    }
}