package com.gh.gamecenter.toolbox

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.constant.Config
import com.gh.gamecenter.NewsDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity.Companion.getWebByCollectionTools
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.baselist.ListExecutor
import com.gh.gamecenter.common.entity.ToolBoxEntity
import com.gh.gamecenter.common.utils.DarkModeUtils
import com.gh.gamecenter.common.utils.ImageUtils.display
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.ItemToolboxBinding
import com.lightgame.adapter.BaseRecyclerAdapter

class ToolBoxItemAdapter(
    context: Context,
    private val isBlockInside: Boolean = false,
    private val mViewModel: ToolBoxViewModel
) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    private var mEntityList: List<ToolBoxEntity> = arrayListOf()
    private var mIsDarkModeOn = DarkModeUtils.isDarkModeOn(context)

    fun setDataList(dataList: List<ToolBoxEntity>, isSearch: Boolean = false) {
        if (mIsDarkModeOn != DarkModeUtils.isDarkModeOn(mContext)) {
            mEntityList = dataList
            notifyItemRangeChanged(0, itemCount)
            mIsDarkModeOn = DarkModeUtils.isDarkModeOn(mContext)
            return
        }
        if (mEntityList == dataList) {
            notifyItemRangeChanged(0, itemCount)
            return
        }
        if (dataList.isEmpty() || isSearch) {
            mEntityList = 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)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        ToolBoxViewHolder(
            ItemToolboxBinding.bind(
                mLayoutInflater.inflate(
                    R.layout.item_toolbox,
                    parent,
                    false
                )
            )
        )

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ToolBoxViewHolder) {
            val toolBoxEntity = mEntityList[position]
            initToolBoxViewHolder(holder, toolBoxEntity)
        }
    }

    override fun getItemCount() = mEntityList.size

    private fun initToolBoxViewHolder(viewHolder: ToolBoxViewHolder, toolBoxEntity: ToolBoxEntity) {
        viewHolder.binding.run {
            divider.setBackgroundColor(R.color.divider.toColor(mContext))
            toolboxItemTitle.setTextColor(R.color.text_title.toColor(mContext))
            toolboxItemDes.setTextColor(R.color.text_subtitleDesc.toColor(mContext))
        }

        viewHolder.binding.toolboxItemDes.text = toolBoxEntity.des
        viewHolder.binding.toolboxItemTitle.text = toolBoxEntity.name
        display(viewHolder.binding.toolboxItemGameThumb, toolBoxEntity.icon)
        viewHolder.binding.divider.goneIf(isBlockInside)
        viewHolder.binding.root.setOnClickListener {
            mViewModel.addToHistoryList(toolBoxEntity)

            val url = toolBoxEntity.url
            if (url != null && url.contains(Config.URL_ARTICLE)) {
                val newsId = url.substring(url.lastIndexOf("/") + 1, url.length - 5) // 5: ".html"
                val intent = NewsDetailActivity.getIntentById(mContext, newsId, "工具箱列表")
                mContext.startActivity(intent)
            } else {
                mContext.startActivity(
                    getWebByCollectionTools(
                        mContext,
                        toolBoxEntity,
                        false
                    )
                )
            }
        }
    }

    class ToolBoxViewHolder(val binding: ItemToolboxBinding) :
        BaseRecyclerViewHolder<ToolBoxEntity?>(binding.root)
}