package com.gh.gamecenter.game.commoncollection

import android.content.Context
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.adapter.viewholder.CommonCollectionItemViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.ExposureLinkEntity
import com.gh.gamecenter.entity.SubjectEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class CommonCollectionAdapter(
    context: Context,
    private var mSubjectEntity: SubjectEntity,
) : BaseRecyclerAdapter<CommonCollectionItemViewHolder>(context) {

    private var mExposureClosure: ((position: Int, entity: ExposureLinkEntity) -> Unit)? = null
    private var mClickClosure: ((position: Int, contentEntity: CommonCollectionContentEntity) -> Unit)? = null

    fun setCallback(
        clickClosure: ((position: Int, contentEntity: CommonCollectionContentEntity) -> Unit),
        exposureClosure: ((position: Int, entity: ExposureLinkEntity) -> Unit)
    ) {
        mClickClosure = clickClosure
        mExposureClosure = exposureClosure
    }

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

    override fun onBindViewHolder(holder: CommonCollectionItemViewHolder, position: Int) {
        val collectionStyle = mSubjectEntity.style
        val contentEntity = mSubjectEntity.commonCollectionList!![position]
        val linkEntity = contentEntity.linkEntity
        var width = 0
        var height = 0
        when (collectionStyle) {
            "1-1" -> {
                width = 244F.dip2px()
                height = 122F.dip2px()
            }
            "1-2" -> {
                width = 160F.dip2px()
                height = 213F.dip2px()
            }
            "img-list" -> {
                width = 160F.dip2px()
                height = 80F.dip2px()
            }
        }

        if (linkEntity.type == "game") {
            mExposureClosure?.invoke(position, linkEntity)
        }

        holder.binding.apply {
            ImageUtils.display(commonCollectionImage, contentEntity.image)
            maskView.goneIf(collectionStyle == "img-list" || (contentEntity.title.isEmpty() && contentEntity.addedContent1.isNullOrEmpty()))
            titleTv.goneIf(!(collectionStyle == "1-1" || collectionStyle == "1-2"))
            titleTv.text = contentEntity.title
            desTv.goneIf(collectionStyle != "1-2")
            desTv.text = contentEntity.addedContent1
            linkTitleTv.goneIf(collectionStyle != "img-list")
            linkTitleTv.text = contentEntity.title
            linkDes1.goneIf(collectionStyle != "img-list")
            linkDes1.text = contentEntity.addedContent1
            linkDes1.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(root.context))
            linkDes2.goneIf(collectionStyle != "img-list")
            linkDes2.text = contentEntity.addedContent2
            linkDes2.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(root.context))

            if (collectionStyle == "1-1") {
                val maskParams = maskView.layoutParams as ConstraintLayout.LayoutParams
                maskParams.height = 38F.dip2px()
                maskView.layoutParams = maskParams
            }

            val imageParams = commonCollectionImage.layoutParams as ConstraintLayout.LayoutParams
            imageParams.width = width
            imageParams.height = height
            commonCollectionImage.layoutParams = imageParams

            val rootParams = root.layoutParams as RecyclerView.LayoutParams
            rootParams.width = width
            rootParams.leftMargin = if (position == 0) 16F.dip2px() else 0
            rootParams.rightMargin = if (position == itemCount - 1) 16F.dip2px() else 0
            root.layoutParams = rootParams

            root.setOnClickListener { mClickClosure?.invoke(position, contentEntity) }
        }
    }

    override fun getItemCount(): Int {
        return mSubjectEntity.commonCollectionList!!.size
    }

    fun checkResetData(updateDate: SubjectEntity) {
        val updateId = updateDate.id
        val id = mSubjectEntity.id
        val originalSize = mSubjectEntity.commonCollectionList?.size
        val updateSize = updateDate.commonCollectionList?.size
        mSubjectEntity = updateDate

        if (updateId != id || originalSize != updateSize) {
            notifyDataSetChanged()
        } else {
            notifyItemRangeChanged(0, itemCount)
        }
    }

    companion object {
        const val spanCount = 2
    }
}