package com.gh.gamecenter.forum.home.follow.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.RecyclerFollowedListAllBinding
import com.gh.gamecenter.databinding.RecyclerFollowedListBbsBinding
import com.gh.gamecenter.databinding.RecyclerFollowedListPersonBinding
import com.gh.gamecenter.entity.FollowUserEntity
import com.gh.gamecenter.forum.home.follow.ResetDataChangeHelper
import com.gh.gamecenter.forum.home.follow.model.FollowRepository.Companion.FOLLOW_UPDATE_TYPE_USER

class FollowedHeaderAdapter(
    private val context: Context,
    private val listener: OnChildEventListener
) :
    RecyclerView.Adapter<ViewHolder>() {

    /**
     *  1.当 _selectedPosition 为 -1 时
     *  不显示 viewAll 按钮
     *  显示选中框
     *
     *  2. 当_selectedPosition 为 -2 时
     *  不显示 viewAll 按钮
     *  暂无选中框
     */
    private var _selectedPosition = -1

    private val showAll: Boolean
        get() = _selectedPosition == -1

    private val resetDataChangeHelper = ResetDataChangeHelper<FollowUserEntity>(context, this)

    val dataList: List<FollowUserEntity>
        get() = resetDataChangeHelper.dataList

    fun submitList(data: List<FollowUserEntity>, selectedPosition: Int = -1) {
        _selectedPosition = selectedPosition
        resetDataChangeHelper.submitList(data) {
            "${it.id}-${it.type}-${it.isShowTip}"
        }
    }

    override fun getItemCount(): Int =
        if (showAll) resetDataChangeHelper.dataCount + 1 else resetDataChangeHelper.dataCount

    override fun getItemViewType(position: Int): Int {
        return if (showAll) {
            if (position < resetDataChangeHelper.dataCount) {
                if (resetDataChangeHelper.getItem(position).type == FOLLOW_UPDATE_TYPE_USER) {
                    VIEW_TYPE_BBS
                } else {
                    VIEW_TYPE_GAME
                }
            } else {
                VIEW_TYPE_VIEW_ALL
            }
        } else {
            if (resetDataChangeHelper.getItem(position).type == FOLLOW_UPDATE_TYPE_USER) {
                VIEW_TYPE_BBS
            } else {
                VIEW_TYPE_GAME
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        when (viewType) {
            VIEW_TYPE_GAME -> {
                FollowedGameViewHolder(parent.toBinding(), ::onSelectedChanged)
            }

            VIEW_TYPE_BBS -> {
                FollowedPersonViewHolder(parent.toBinding(), ::onSelectedChanged)
            }

            else -> FollowedViewAllViewHolder(parent.toBinding(), listener::onViewAll)
        }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (holder) {
            is FollowedGameViewHolder -> {

                holder.bindView(
                    resetDataChangeHelper.getItem(position),
                    position,
                    _selectedPosition == position,
                    showAll
                )
            }

            is FollowedPersonViewHolder -> {
                holder.bindView(
                    resetDataChangeHelper.getItem(position),
                    position,
                    _selectedPosition == position,
                    showAll
                )
            }
        }

    }

    private fun onSelectedChanged(position: Int) {
        selectPosition(position)
        listener.onViewItem(position)
    }

    fun selectPosition(position: Int) {
        if (_selectedPosition != -1 && _selectedPosition != position) {
            val lastSelectedPosition = _selectedPosition
            _selectedPosition = position
            notifyItemChanged(lastSelectedPosition)
            notifyItemChanged(_selectedPosition)

            _selectedPosition = position
        }
    }

    fun clearSelectedPosition() {
        val lastSelectedPosition = _selectedPosition
        _selectedPosition = -2
        if (lastSelectedPosition >= 0) {
            notifyItemChanged(lastSelectedPosition)
        }
    }

    companion object {
        private const val VIEW_TYPE_GAME = 0
        private const val VIEW_TYPE_BBS = 1
        private const val VIEW_TYPE_VIEW_ALL = 2
    }

    private class FollowedGameViewHolder(
        val binding: RecyclerFollowedListBbsBinding,
        private val itemClick: (Int) -> Unit,
    ) : ViewHolder(binding.root) {

        fun bindView(item: FollowUserEntity, position: Int, isSelected: Boolean, showName: Boolean) {
            if (showName) {
                binding.tvName.goneIf(false)
                binding.tvName.text = item.bbs.name
            } else {
                binding.tvName.goneIf(true)
            }
            binding.tvName.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(itemView.context))
            binding.ivIcon.displayGameIcon(item.icon, null)
            val resourceId = if (isSelected) {
                com.gh.gamecenter.common.R.drawable.background_shape_theme_radius_14
            } else {
                com.gh.gamecenter.common.R.drawable.background_shape_white_radius_14
            }

            binding.gIndicator.goneIf(!item.isShowTip)

            binding.vIconBackground.background = resourceId.toDrawable(itemView.context)

            itemView.setOnClickListener {
                itemClick(position)
            }
        }
    }

    private class FollowedPersonViewHolder(
        val binding: RecyclerFollowedListPersonBinding,
        private val itemClick: (Int) -> Unit,
    ) : ViewHolder(binding.root) {

        fun bindView(item: FollowUserEntity, position: Int, isSelected: Boolean, showName: Boolean) {
            if (showName) {
                binding.tvName.goneIf(false)
                binding.tvName.text = item.user.name
            } else {
                binding.tvName.goneIf(true)
            }
            binding.tvName.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(itemView.context))
            binding.ivIcon.displayGameIcon(item.icon, null)
            val resourceId = if (isSelected) {
                com.gh.gamecenter.common.R.drawable.background_shape_theme_radius_999
            } else {
                com.gh.gamecenter.common.R.drawable.background_shape_white_radius_999
            }
            binding.gIndicator.goneIf(!item.isShowTip)
            binding.vIconBackground.background = resourceId.toDrawable(itemView.context)

            itemView.setOnClickListener {
                itemClick(position)
            }
        }
    }

    private class FollowedViewAllViewHolder(
        binding: RecyclerFollowedListAllBinding,
        private val viewAll: () -> Unit,
    ) : ViewHolder(binding.root) {

        init {
            binding.root.setOnClickListener {
                viewAll()
            }
        }
    }

    interface OnChildEventListener {

        fun onViewItem(position: Int)

        fun onViewAll()
    }
}