package com.adealink.weparty.commonui.dialogfragment

import android.util.TypedValue
import android.view.View
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.aab.util.getCompatDrawable
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.weparty.R
import com.adealink.weparty.commonui.dialogfragment.data.BaseActionItem
import com.adealink.weparty.commonui.dialogfragment.data.CustomActionItem
import com.adealink.weparty.commonui.dialogfragment.data.TextActionItem
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.databinding.DialogCommonActionBinding
import com.adealink.weparty.databinding.ItemCommonTextActionBinding

class CommonTextActionDialog : BottomDialogFragment(R.layout.dialog_common_action) {
    private val binding by viewBinding(DialogCommonActionBinding::bind)
    private var title: String? = null
    private var itemIndex = 0
    private var maxContentHeight = -1
    private var actionListener: CommonTextActionListener? = null
    private var textActionList = mutableListOf<BaseActionItem>()

    override fun initViews() {
        super.initViews()
        binding.btnCancel.setOnClickListener {
            dismiss()
        }
        title?.let {
            binding.tvTitle.show()
            binding.vTitleBottom.show()
            binding.tvTitle.text = it
        } ?: let {
            binding.tvTitle.gone()
            binding.vTitleBottom.gone()
        }
        textActionList.forEach {
            when (it) {
                is TextActionItem -> {
                    addTextActionItem(it.viewId, it.content, it.enable)
                }

                is CustomActionItem -> {
                    addCustomItem(it.viewId, it.itemView, it.enable)
                }
            }
        }
        binding.llItem.post {
            if (maxContentHeight > 0) {
                val targetContentHeight = binding.llItem.measuredHeight
                if (targetContentHeight > maxContentHeight) {
                    binding.scroll.updateLayoutParams<ConstraintLayout.LayoutParams> {
                        this.height = maxContentHeight
                    }
                }
            }
        }
    }

    fun setTitle(title: String?) {
        this.title = title
    }

    fun setMaxContentHeight(maxHeight: Int) {
        this.maxContentHeight = maxHeight
    }

    fun setActionList(list: MutableList<BaseActionItem>) {
        this.textActionList = list
    }

    fun setListener(listener: CommonTextActionListener?): CommonTextActionDialog {
        this.actionListener = listener
        return this
    }

    private fun addTextActionItem(
        @IdRes viewId: Int,
        text: CharSequence?,
        enable: Boolean,
    ) {
        val item =
            ItemCommonTextActionBinding.inflate(layoutInflater, binding.root, false)
        item.root.id = viewId
        item.root.text = text
        if (itemIndex == 0) {
            item.root.background = getCompatDrawable(R.drawable.commonui_text_action_top)
        } else {
            item.root.background = getCompatDrawable(R.drawable.commonui_text_action_normal)
        }
        item.root.tag = itemIndex++
        if (enable) {
            item.root.isEnabled = true
            item.root.setOnClickListener {
                actionListener?.onActionClick(viewId)
                actionListener = null
                dismiss()
            }
            item.root.setTextColor(getCompatColor(R.color.black))
            item.root.setTextSize(
                TypedValue.COMPLEX_UNIT_DIP,
                16f
            )
        } else {
            item.root.isEnabled = false
            item.root.setTextColor(getCompatColor(R.color.color_AAAAAA))
            item.root.setTextSize(
                TypedValue.COMPLEX_UNIT_DIP,
                14f
            )
        }

        binding.llItem.addView(item.root)
    }

    private fun addCustomItem(viewId: Int, itemView: View, enable: Boolean) {
        itemView.id = viewId
        if (itemIndex == 0) {
            itemView.background = getCompatDrawable(R.drawable.commonui_text_action_top)
        } else {
            itemView.background = getCompatDrawable(R.drawable.commonui_text_action_normal)
        }
        itemView.tag = itemIndex++
        if (enable) {
            itemView.isEnabled = true
            itemView.setOnClickListener {
                actionListener?.onActionClick(viewId)
                actionListener = null
                dismiss()
            }
        } else {
            itemView.isEnabled = false
        }
        binding.llItem.addView(itemView)
    }

    interface CommonTextActionListener {
        fun onActionClick(@IdRes viewId: Int)
    }

    class Builder {
        private var title: String? = null
        private var actionListener: CommonTextActionListener? = null
        private var actionList = mutableListOf<BaseActionItem>()
        private var maxContentHeight = -1

        fun addTitle(title: String): Builder {
            this.title = title
            return this
        }
        fun addItem(viewId: Int, itemView: View, enable: Boolean = true): Builder {
            this.actionList.add(CustomActionItem(viewId, itemView, enable))
            return this
        }

        fun addItem(
            viewId: Int,
            @StringRes resId: Int,
            enable: Boolean = true,
        ): Builder {
            this.actionList.add(TextActionItem(viewId, getCompatString(resId), enable))
            return this
        }

        fun addItem(viewId: Int, content: CharSequence, enable: Boolean = true): Builder {
            this.actionList.add(TextActionItem(viewId, content, enable))
            return this
        }

        fun addItem(viewId: Int, content: String, enable: Boolean = true): Builder {
            this.actionList.add(TextActionItem(viewId, content, enable))
            return this
        }

        fun actionListener(listener: CommonTextActionListener?): Builder {
            this.actionListener = listener
            return this
        }

        fun maxContentHeight(maxHeight: Int): Builder {
            this.maxContentHeight = maxHeight
            return this
        }

        fun build(): CommonTextActionDialog {
            val dialog = CommonTextActionDialog()
            dialog.setTitle(this@Builder.title)
            dialog.setMaxContentHeight(this@Builder.maxContentHeight)
            dialog.setListener(actionListener)
            dialog.setActionList(actionList)
            return dialog
        }
    }
}