package com.gh.gamecenter.game.commoncollection

import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
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.CommonCollection12ItemBinding
import com.gh.gamecenter.databinding.CommonCollectionItemBinding
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.SubjectEntity

class CommonCollection12ViewHolder(var binding: CommonCollection12ItemBinding) : RecyclerView.ViewHolder(binding.root) {

    fun bindView(
        subjectEntity: SubjectEntity,
        positionInOriginList: Int,
        clickClosure: (position: Int, contentEntity: CommonCollectionContentEntity) -> Unit
    ) {
        val linkList = subjectEntity.commonCollectionList ?: return

        val width = (DisplayUtils.getScreenWidth() - 16F.dip2px() * 2 - 8F.dip2px()) / 2
        val height = when (subjectEntity.style) {
            "1-1" -> width * 1 / 2
            "1-2" -> width * 213 / 160
            "img-list" -> width * 80 / 160
            else -> 0
        }

        val leftBinding = CommonCollectionItemBinding.bind(binding.leftView.root)
        val rightBinding = CommonCollectionItemBinding.bind(binding.rightView.root)

        bindSubView(
            linkList[positionInOriginList],
            leftBinding,
            subjectEntity.style ?: "img-list",
            width,
            height,
            positionInOriginList,
            clickClosure
        )

        if (linkList.size > positionInOriginList + 1) {
            binding.rightView.root.visibility = View.VISIBLE
            bindSubView(
                linkList[positionInOriginList + 1],
                rightBinding,
                subjectEntity.style ?: "img-list",
                width,
                height,
                positionInOriginList + 1,
                clickClosure
            )
        } 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
        }
    }

    private fun bindSubView(
        contentEntity: CommonCollectionContentEntity,
        subBinding: CommonCollectionItemBinding,
        collectionStyle: String,
        width: Int,
        height: Int,
        position: Int,
        clickClosure: (position: Int, contentEntity: CommonCollectionContentEntity) -> Unit
    ) {
        subBinding.run {
            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(R.color.text_subtitle.toColor(root.context))
            }
            linkDes2.goneIf(collectionStyle != "img-list") {
                linkDes2.text = contentEntity.addedContent2
                linkDes2.setTextColor(R.color.text_subtitleDesc.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

            root.setOnClickListener { clickClosure.invoke(position, contentEntity) }
        }
    }

}