package com.gh.common.provider

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.viewbinding.ViewBinding
import com.therouter.router.Route
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.common.util.ConcernContentUtils
import com.gh.common.view.ImageContainerView
import com.gh.gamecenter.ImageViewerActivity.Companion.getIntent
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.utils.toArrayList
import com.gh.gamecenter.core.provider.IConcernArticleUtilsProvider
import com.gh.gamecenter.databinding.RecyclerGameArticleBinding

@com.therouter.inject.ServiceProvider
class ConcernArticleUtilsProviderImpl : IConcernArticleUtilsProvider {
    override fun addContentPic(
        context: Context,
        linearLayout: LinearLayout,
        list: List<String>,
        entrance: String,
        width: Int
    ) {
        ConcernContentUtils.addContentPic(context, linearLayout, list, entrance, width)
    }

    override fun createArticleBinding(parent: ViewGroup): ViewBinding {
        val inflater = LayoutInflater.from(parent.context)
        return RecyclerGameArticleBinding.inflate(inflater, parent, false)
    }

    override fun initArticleStyle(binding: ViewBinding) {
        if (binding is RecyclerGameArticleBinding) {
            val context = binding.root.context
            binding.root
                .setBackground(ContextCompat.getDrawable(context, com.gh.gamecenter.common.R.drawable.reuse_listview_item_style))
            binding.tvGameName.setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.text_primary))
            binding.tvTime.setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.text_tertiary))
            binding.tvTitle.setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.text_primary))
            binding.tvDescription.setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.text_secondary))
            binding.tvComment.setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.text_tertiary))
            binding.tvShare.setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.text_tertiary))
        }
    }

    override fun getIvIcon(binding: ViewBinding): View =
        (binding as RecyclerGameArticleBinding).ivIcon

    override fun getTvGameName(binding: ViewBinding): TextView =
        (binding as RecyclerGameArticleBinding).tvGameName

    override fun getTvTime(binding: ViewBinding): TextView = (binding as RecyclerGameArticleBinding).tvTime

    override fun getTvTitle(binding: ViewBinding): TextView = (binding as RecyclerGameArticleBinding).tvTitle

    override fun getTvDescription(binding: ViewBinding): TextView =
        (binding as RecyclerGameArticleBinding).tvDescription

    override fun bindImgs(binding: ViewBinding, img: List<String>) {
        if (binding is RecyclerGameArticleBinding) {
            val images = img.map {
                ImageContainerView.ImageContainerData.ImageInfo(it, 3, 2)
            }
            val imageContainerData =
                ImageContainerView.ImageContainerData("", false, images, show = images.isNotEmpty())
            binding.ivImagesContainer.bindData(imageContainerData,
                object : ImageContainerView.OnImageContainerEventListener {
                    override fun onImageClick(
                        images: List<String>,
                        position: Int,
                        imageViewList: ArrayList<SimpleDraweeView>
                    ) {
                        val checkIntent = getIntent(
                            binding.root.context,
                            images.toArrayList(),
                            position,
                            imageViewList,
                            ""
                        )
                        binding.root.context.startActivity(checkIntent)
                    }

                    override fun onVideoCLick(videoId: String) = Unit

                })
        }
    }

    override fun getTvComment(binding: ViewBinding): TextView = (binding as RecyclerGameArticleBinding).tvComment

    override fun getTvShare(binding: ViewBinding): TextView = (binding as RecyclerGameArticleBinding).tvShare
}
