package com.gh.gamecenter.gamecollection.tag

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.databinding.GameCollectionSelectedTagItemBinding
import com.gh.gamecenter.databinding.GameCollectionTagItemBinding
import com.gh.gamecenter.databinding.ItemGameCollectionSelectedTagBinding
import com.gh.gamecenter.databinding.ItemGameCollectionTagBinding
import com.gh.gamecenter.entity.GameCollectionTagEntity
import com.gh.gamecenter.entity.TagInfoEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class GameCollectionTagAdapter(
    context: Context,
    val singleChoice: Boolean = true,
    val maxSelectCount: Int = 1,
    var selectedTagEntity: TagInfoEntity? = null,
    private val updateCallback: (() -> Unit),
    private val singleClickCallback: (() -> Unit)? = null
) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    private var mTagViewList = arrayListOf<ItemGameCollectionTagBinding>()
    var mTagList = arrayListOf<GameCollectionTagEntity>()
    var selectedTagCategory = ""
    var selectedTagEntityList = arrayListOf<TagInfoEntity>()

    fun setTagList(tagList: ArrayList<GameCollectionTagEntity>) {
        mTagList = tagList
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        return if (!singleChoice && position == 0) SELECTED_TAGS else TAG_ITEM
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = when (viewType) {
        SELECTED_TAGS -> GameCollectionSelectedTagViewHolder(
            GameCollectionSelectedTagItemBinding.inflate(mLayoutInflater, parent, false)
        )
        else -> GameCollectionTagItemViewHolder(
            GameCollectionTagItemBinding.inflate(mLayoutInflater, parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is GameCollectionSelectedTagViewHolder -> {
                holder.binding.hintTv.setTextColor(com.gh.gamecenter.common.R.color.text_instance.toColor(holder.binding.root.context))
                holder.binding.selectedTagFlexbox.removeAllViews()
                holder.binding.hintTv.goneIf(selectedTagEntityList.size != 0)
                for (tag in selectedTagEntityList) {
                    val selectedTagView =
                        ItemGameCollectionSelectedTagBinding.inflate(mLayoutInflater)
                            .apply {
                                tagTv.text = tag.name
                                root.setOnClickListener {
                                    selectedTagEntityList.remove(tag)
                                    updateCallback.invoke()
                                    notifyDataSetChanged()
                                }
                            }.root
                    holder.binding.selectedTagFlexbox.addView(selectedTagView)
                }
            }
            is GameCollectionTagItemViewHolder -> {
                val data = mTagList[if (singleChoice) position else position - 1]
                holder.binding.tagFlexbox.removeAllViews()
                holder.binding.titleTv.text = data.categoryName
                holder.binding.titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(holder.binding.root.context))
                holder.binding.titleTv.setPadding(
                    16F.dip2px(),
                    if (position == 0) 40F.dip2px() else 10F.dip2px(),
                    16F.dip2px(),
                    10F.dip2px()
                )
                holder.binding.tagFlexbox.setPadding(
                    12F.dip2px(),
                    5F.dip2px(),
                    12F.dip2px(),
                    if (position == itemCount - 1) 97F.dip2px() else 15F.dip2px()
                )
                for (tagEntity in data.tags) {
                    val tag =
                        if (singleChoice) getSingleTag(tagEntity, data.categoryName) else getMultipleTag(tagEntity)
                    mTagViewList.add(tag)
                    holder.binding.tagFlexbox.addView(tag.root)
                }
            }
        }
    }

    private fun getSingleTag(tag: TagInfoEntity, tagCategory: String) =
        ItemGameCollectionTagBinding.inflate(mLayoutInflater).apply {
            tagTv.text = tag.name
            tagTv.layoutParams = tagTv.layoutParams.apply { width = (DisplayUtils.getScreenWidth() - 56F.dip2px()) / 4 }
            selectedTagEntity?.let {
                if (tag == it) tagTv.isChecked = true
            }
            root.setOnClickListener {
                for (tagView in mTagViewList) {
                    if (tagView.tagTv != tagTv) tagView.tagTv.isChecked = false
                }
                tagTv.isChecked = !tagTv.isChecked
                if (tagTv.isChecked) {
                    selectedTagEntity = tag
                    selectedTagCategory = tagCategory
                    singleClickCallback?.invoke()
                } else {
                    selectedTagEntity = null
                    selectedTagCategory = ""
                    singleClickCallback?.invoke()
                }
            }
        }

    private fun getMultipleTag(tag: TagInfoEntity) =
        ItemGameCollectionTagBinding.inflate(mLayoutInflater).apply {
            tagTv.layoutParams = tagTv.layoutParams.apply { width = (DisplayUtils.getScreenWidth() - 56F.dip2px()) / 4 }
            tagTv.text = tag.name
            tagTv.isChecked = selectedTagEntityList.contains(tag)
            root.setOnClickListener {
                if (!tagTv.isChecked) {
                    if (selectedTagEntityList.size >= maxSelectCount) {
                        ToastUtils.showToast("最多选择${maxSelectCount}个标签")
                    } else {
                        tagTv.isChecked = !tagTv.isChecked
                        selectedTagEntityList.add(tag)
                        updateCallback.invoke()
                        notifyItemChanged(0)
                    }
                } else {
                    tagTv.isChecked = !tagTv.isChecked
                    if (selectedTagEntityList.contains(tag)) selectedTagEntityList.remove(tag)
                    updateCallback.invoke()
                    notifyItemChanged(0)
                }
            }
        }

    override fun getItemCount() = if (singleChoice) mTagList.size else mTagList.size + 1

    class GameCollectionSelectedTagViewHolder(var binding: GameCollectionSelectedTagItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    class GameCollectionTagItemViewHolder(var binding: GameCollectionTagItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    companion object {
        const val SELECTED_TAGS = 100
        const val TAG_ITEM = 101
    }
}