package com.gh.gamecenter.home.custom.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.CommonCollectionItemBinding
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.ExposureLinkEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.model.CustomPageItem.Companion.COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_IMAGE_TEXT
import com.gh.gamecenter.home.custom.model.CustomPageItem.Companion.COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_SLIDE_BANNER
import com.gh.gamecenter.home.custom.model.CustomPageItem.Companion.COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_VERTICAL_CARD

class CustomCommonCollectionAdapter(
    context: Context,
    private val listener: OnEventListener
) :
    CustomBaseChildAdapter<CommonCollectionContentEntity, CustomCommonCollectionAdapter.CustomCommonCollectionItemViewHolder>(
        context
    ) {
    private var layout = 0

    fun setData(data: CustomPageData.CommonContentCollection) {
        layout = data.layout
        submitList(data.data)
    }

    override fun getKey(t: CommonCollectionContentEntity): String {
        return "${layout}-${t.id}"
    }

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

    override fun onBindViewHolder(holder: CustomCommonCollectionItemViewHolder, position: Int) {
        val item = getItem(position)
        var width = 0
        var height = 0
        when (layout) {
            COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_SLIDE_BANNER -> {    // 横向滑动banner
                width = 244F.dip2px()
                height = 122F.dip2px()
            }

            COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_VERTICAL_CARD -> {  // 横排竖式卡片
                width = 160F.dip2px()
                height = 213F.dip2px()
            }

            COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_IMAGE_TEXT -> { // 横排图文列表
                width = 160F.dip2px()
                height = 80F.dip2px()
            }
        }
        listener.addExposureEvent(position, item.linkEntity)
        holder.boundedLinkEntity = item.linkEntity

        holder.binding.apply {
            ImageUtils.display(commonCollectionImage, item.image)
            maskView.goneIf(layout == COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_IMAGE_TEXT || (item.title.isEmpty() && item.addedContent1.isNullOrEmpty()))
            titleTv.goneIf(layout == COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_IMAGE_TEXT)
            titleTv.text = item.title
            desTv.goneIf(layout != COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_VERTICAL_CARD)
            desTv.text = item.addedContent1
            linkTitleTv.goneIf(layout != COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_IMAGE_TEXT)
            linkTitleTv.text = item.title
            linkDes1.goneIf(layout != COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_IMAGE_TEXT)
            linkDes1.text = item.addedContent1
            linkDes1.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(root.context))
            linkDes2.goneIf(layout != COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_IMAGE_TEXT)
            linkDes2.text = item.addedContent2
            linkDes2.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(root.context))

            val maskHeight = if (layout == COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_SLIDE_BANNER) {
                60f.dip2px()
            } else {
                38F.dip2px()
            }
            val maskParams = maskView.layoutParams as ConstraintLayout.LayoutParams
            maskParams.height = maskHeight
            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 {
                listener.onChildItemClick(position, item)
            }
        }
    }

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

    companion object {
        const val spanCount = 2
    }

    class CustomCommonCollectionItemViewHolder(val binding: CommonCollectionItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root), IExposureProvider {
        var boundedLinkEntity: ExposureLinkEntity? = null

        override fun provideExposureData(): ExposureEvent? {
            return boundedLinkEntity?.exposureEvent?.getFreshExposureEvent()
        }
    }

    interface OnEventListener {
        fun addExposureEvent(childPosition: Int, exposureLinkEntity: ExposureLinkEntity)

        fun onChildItemClick(childPosition: Int, item: CommonCollectionContentEntity)
    }
}