package com.gh.gamecenter.gamecollection.choose

import android.annotation.SuppressLint
import android.content.Context
import android.view.MotionEvent
import android.view.ViewGroup
import androidx.core.widget.doOnTextChanged
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.core.utils.PatternUtils
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.ItemChooseGamesBinding
import com.gh.gamecenter.feature.entity.GameEntity

class ChooseGamesAdapter(context: Context, val dragListener: ItemDragListener) :
    ListAdapter<GameEntity>(context) {


    public override fun setListData(updateData: MutableList<GameEntity>?) {
        super.setListData(updateData)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ChooseGamesViewHolder(parent.toBinding())
    }

    override fun areItemsTheSame(oldItem: GameEntity?, newItem: GameEntity?): Boolean {
        return oldItem == newItem
    }

    override fun areContentsTheSame(oldItem: GameEntity?, newItem: GameEntity?): Boolean {
        return oldItem?.id == newItem?.id
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ChooseGamesViewHolder) {
            holder.binding.root.setBackgroundColor(R.color.background_white.toColor(mContext))
            holder.binding.recommendContainer.background = R.drawable.bg_shape_f5_radius_6.toDrawable(mContext)
            holder.binding.gameNameTv.setTextColor(R.color.text_title.toColor(mContext))
            holder.binding.recommendReasonEt.setTextColor(R.color.text_title.toColor(mContext))
            holder.binding.ratingScoreTv.setTextColor(R.color.text_subtitleDesc.toColor(mContext))
            holder.binding.recommendReasonEt.setHintTextColor(R.color.text_subtitleDesc.toColor(mContext))

            val gameEntity = mEntityList[position]
            holder.binding.root.tag = gameEntity.id
            holder.binding.gameNameTv.text = gameEntity.name
            holder.binding.gameIcon.displayGameIcon(gameEntity)
            holder.binding.recommendReasonEt.setText(gameEntity.recommendText)
            holder.binding.ratingScore.rating = gameEntity.recommendStar.toFloat()
            holder.binding.recommendReasonEt.filters = arrayOf(TextHelper.getFilter(45, "最多输入45个字"))
            holder.binding.recommendReasonEt.doOnTextChanged { text, _, _, _ ->
                if (holder.binding.root.tag != gameEntity.id) return@doOnTextChanged
                gameEntity.recommendText = text.toString()
            }
            holder.binding.recommendReasonEt.run {
                doOnTextChanged { text, start, _, _ ->
                    if (holder.binding.root.tag != gameEntity.id) return@doOnTextChanged
                    if (text?.contains("\n") == true) {
                        setText(text.toString().replace("\n", ""))
                        setSelection(start)
                        return@doOnTextChanged
                    }
                    if (PatternUtils.isHasSpace(text.toString())) {
                        setText(PatternUtils.replaceSpace(text.toString()))
                        setSelection(start)
                        return@doOnTextChanged
                    }
                }
            }
            holder.binding.ratingScore.setOnRatingChangeListener { _, rating ->
                if (holder.binding.root.tag != gameEntity.id) return@setOnRatingChangeListener
                gameEntity.recommendStar = rating.toInt()
            }
            holder.binding.deleteIv.setOnClickListener {
                dragListener.deleteItem(gameEntity)
            }
            holder.binding.dragView.setOnTouchListener { _, event ->
                consume {
                    if (event.action == MotionEvent.ACTION_DOWN) {
                        dragListener.startDragItem(holder)
                    }
                }
            }
            holder.binding.topView.setOnClickListener {
                dragListener.setToTop(holder)
            }
        }
    }

    override fun getItemCount(): Int = mEntityList.size

    class ChooseGamesViewHolder(val binding: ItemChooseGamesBinding) : BaseRecyclerViewHolder<GameEntity>(binding.root)

    interface ItemDragListener {
        fun startDragItem(holder: RecyclerView.ViewHolder)
        fun setToTop(holder: RecyclerView.ViewHolder)
        fun deleteItem(entity: GameEntity)
    }
}