package com.gh.gamecenter.home.custom.ui

import android.content.Context
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.CommonCollection12ItemCustomBinding
import com.gh.gamecenter.databinding.CommonCollectionItemBinding
import com.gh.gamecenter.databinding.CommonCollectionItemCustomBinding
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.model.CustomPageItem.Companion.COMMON_CONTENT_COLLECTION_LAYOUT_DOUBLE_ROW_VERTICAL_CARD
import com.gh.gamecenter.home.custom.model.CustomPageItem.Companion.COMMON_CONTENT_COLLECTION_LAYOUT_VERTICAL_IMAGE_TEXT
import com.gh.gamecenter.home.custom.viewholder.CustomCommonCollectionViewHolder

/**
 * ui-双列banner  双列竖式卡片 竖排图文列表
 */
class CommonContentCollection12Ui(
    val binding: CommonCollection12ItemCustomBinding,
    private val listener: OnCollection12Listener
) {

    private val context: Context
        get() = binding.root.context

    fun bind(
        collection: CustomPageData.CommonContentCollection,
        leftPosition: Int
    ) {
        val data = collection.data

        val isFirstLine = leftPosition == 0
        binding.layoutTitle.root.goneIf(!isFirstLine) {
            binding.layoutTitle.tvTitle.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
        }

        CustomCommonCollectionViewHolder.setTitle(collection, binding.layoutTitle) {
            listener.navigateToCommonCollectionDetailPage(collection)
        }

        val width = (DisplayUtils.getScreenWidth() - 16F.dip2px() * 2 - 8F.dip2px()) / 2
        val height = if (collection.layout == COMMON_CONTENT_COLLECTION_LAYOUT_DOUBLE_ROW_VERTICAL_CARD) {
            width * 4 / 3
        } else {
            width / 2
        }

        bindSubView(
            data[leftPosition],
            binding.leftView,
            collection.layout,
            width,
            height,
            leftPosition
        )

        val rightPosition = leftPosition + 1
        if (rightPosition < data.size) {
            binding.rightView.root.visibility = View.VISIBLE
            bindSubView(
                data[rightPosition],
                binding.rightView,
                collection.layout,
                width,
                height,
                rightPosition
            )
        } else {
            binding.rightView.root.visibility = View.INVISIBLE

            val imageParams = binding.rightView.commonCollectionImage.layoutParams as ConstraintLayout.LayoutParams
            imageParams.width = width
            imageParams.height = 1
            binding.rightView.commonCollectionImage.layoutParams = imageParams
        }

        val isLastLine = leftPosition + 2 >= data.size
        val _paddingBottom =
            if (isLastLine || collection.layout == COMMON_CONTENT_COLLECTION_LAYOUT_VERTICAL_IMAGE_TEXT) {
                16f.dip2px()
            } else {
                8f.dip2px()
            }
        with(binding.root) {
            setPadding(paddingLeft, paddingTop, paddingRight, _paddingBottom)
        }
    }

    private fun bindSubView(
        contentEntity: CommonCollectionContentEntity,
        subBinding: CommonCollectionItemCustomBinding,
        layout: Int,
        width: Int,
        height: Int,
        position: Int
    ) {
        subBinding.run {
            ImageUtils.display(commonCollectionImage, contentEntity.image)
            maskView.goneIf(layout == COMMON_CONTENT_COLLECTION_LAYOUT_VERTICAL_IMAGE_TEXT || (contentEntity.title.isEmpty() && contentEntity.addedContent1.isNullOrEmpty()))
            titleTv.goneIf(layout == COMMON_CONTENT_COLLECTION_LAYOUT_VERTICAL_IMAGE_TEXT) {
                titleTv.text = contentEntity.title
            }
            desTv.goneIf(layout != COMMON_CONTENT_COLLECTION_LAYOUT_DOUBLE_ROW_VERTICAL_CARD) {
                desTv.text = contentEntity.addedContent1
            }
            linkTitleTv.goneIf(layout != COMMON_CONTENT_COLLECTION_LAYOUT_VERTICAL_IMAGE_TEXT) {
                linkTitleTv.text = contentEntity.title
            }
            linkDes1.goneIf(layout != COMMON_CONTENT_COLLECTION_LAYOUT_VERTICAL_IMAGE_TEXT) {
                linkDes1.text = contentEntity.addedContent1
                linkDes1.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(root.context))
            }
            linkDes2.goneIf(layout != COMMON_CONTENT_COLLECTION_LAYOUT_VERTICAL_IMAGE_TEXT) {
                linkDes2.text = contentEntity.addedContent2
                linkDes2.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(root.context))
            }

            val maskHeight = if (layout == COMMON_CONTENT_COLLECTION_LAYOUT_DOUBLE_ROW_VERTICAL_CARD) {
                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

            root.setOnClickListener {
                listener.onChildItemClick(position, contentEntity)
            }
        }
    }

    interface OnCollection12Listener {
        fun onChildItemClick(childPosition: Int, contentEntity: CommonCollectionContentEntity)

        fun navigateToCommonCollectionDetailPage(collection: CustomPageData.CommonContentCollection)
    }
}