package com.gh.gamecenter.gamecollection.choose

import android.annotation.SuppressLint
import android.content.Context
import android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE
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.common.baselist.ListAdapter
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.PatternUtils
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(com.gh.gamecenter.common.R.color.ui_surface.toColor(mContext))
            holder.binding.recommendContainer.background = R.drawable.bg_shape_f8_radius_6.toDrawable(mContext)
            holder.binding.gameNameTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
            holder.binding.recommendReasonEt.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
            holder.binding.ratingScoreTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
            holder.binding.recommendReasonEt.setHintTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
            holder.binding.deleteIv.setImageResource(R.drawable.ic_choose_game_delete)
            holder.binding.dragView.setImageResource(R.drawable.ic_choose_games_drag)
            holder.binding.topView.setImageResource(R.drawable.ic_choose_games_top)

            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.inputType = TYPE_TEXT_FLAG_MULTI_LINE
            holder.binding.recommendReasonEt.setSingleLine(false)
            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)
    }
}