package com.gh.gamecenter.toolbox

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.baselist.ListExecutor
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.ToolboxBlockItemBinding
import com.gh.gamecenter.entity.ToolBoxBlockEntity
import com.gh.gamecenter.common.entity.ToolBoxEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class ToolBoxBlockAdapter(context: Context, private val mViewModel: ToolBoxViewModel) :
    BaseRecyclerAdapter<ToolBoxBlockAdapter.ToolBoxBlockViewHolder>(context) {

    private var mEntityList = mutableListOf<ToolBoxBlockEntity>()

    fun setDataList(dataList: List<ToolBoxBlockEntity>) {
        if (dataList.isEmpty() || mEntityList.size > dataList.size) {
            mEntityList = ArrayList(dataList)
            notifyDataSetChanged()
            return
        }
        ListExecutor.workerExecutor.execute {
            val diffResult = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
                override fun getOldListSize(): Int {
                    return mEntityList.size
                }

                override fun getNewListSize(): Int {
                    return dataList.size
                }

                override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                    val oldItem = mEntityList.safelyGetInRelease(oldItemPosition)
                    val newItem = dataList.safelyGetInRelease(newItemPosition)
                    return oldItem == newItem
                }

                override fun areContentsTheSame(
                    oldItemPosition: Int,
                    newItemPosition: Int
                ) = areItemsTheSame(oldItemPosition, newItemPosition)
            })
            ListExecutor.uiExecutor.execute {
                mEntityList = ArrayList(dataList)
                diffResult.dispatchUpdatesTo(this)
            }
        }
    }

    fun setHistoryList(dataList: List<ToolBoxEntity>) {
        if (mEntityList.isEmpty() || dataList.isEmpty()) return
        if (mEntityList[0].categoryName == "最近使用") {
            mEntityList[0].toolboxList = dataList
            notifyItemChanged(0)
        } else {
            mEntityList.add(0, ToolBoxBlockEntity(categoryName = "最近使用", toolboxList = dataList))
            notifyDataSetChanged()
        }
    }

    override fun onBindViewHolder(holder: ToolBoxBlockViewHolder, position: Int) {
        holder.bindToolBoxBlock(mEntityList[position])
        holder.binding.divider.goneIf(position == itemCount - 1 || mEntityList[position].categoryName == "最近使用")
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        ToolBoxBlockViewHolder(ToolboxBlockItemBinding.inflate(mLayoutInflater, parent, false))

    override fun getItemCount() = mEntityList.size

    inner class ToolBoxBlockViewHolder(val binding: ToolboxBlockItemBinding) :
        BaseRecyclerViewHolder<ToolBoxBlockEntity>(binding.root) {

        private var mAdapter: ToolBoxItemAdapter? = null

        fun bindToolBoxBlock(toolBoxBlockEntity: ToolBoxBlockEntity) {
            val context = binding.root.context
            binding.divider.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(context))
            binding.titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            binding.expandTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
            binding.titleTv.text = toolBoxBlockEntity.categoryName
            binding.expandContainer.goneIf(toolBoxBlockEntity.toolboxList.size < 5)
            binding.expandContainer.setOnClickListener {
                toolBoxBlockEntity.isExpand = !toolBoxBlockEntity.isExpand
                if (toolBoxBlockEntity.isExpand)
                    mAdapter?.setDataList(toolBoxBlockEntity.toolboxList)
                else
                    mAdapter?.setDataList(toolBoxBlockEntity.toolboxList.take(4))
                updateExpandTv(toolBoxBlockEntity.isExpand)
            }
            if (mAdapter != null) {
                if (toolBoxBlockEntity.isExpand)
                    mAdapter?.setDataList(toolBoxBlockEntity.toolboxList)
                else
                    mAdapter?.setDataList(toolBoxBlockEntity.toolboxList.take(4))
                updateExpandTv(toolBoxBlockEntity.isExpand)
                return
            }
            mAdapter = ToolBoxItemAdapter(context, true, mViewModel)
            mAdapter?.setDataList(toolBoxBlockEntity.toolboxList.take(4))
            binding.toolboxRv.layoutManager = GridLayoutManager(context, 2)
            binding.toolboxRv.adapter = mAdapter
        }

        private fun updateExpandTv(isExpend: Boolean) {
            binding.expandTv.text = if (isExpend) "收起" else "展开全部"
            binding.expandIv.run {
                pivotX = width / 2F
                pivotY = height / 2F
                rotation = if (isExpend) 180F else 0F
            }
        }
    }
}