package com.gh.gamecenter.amway.search

import android.app.Activity
import android.graphics.Typeface
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.*
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.showRegulationTestDialogIfNeeded
import com.gh.gamecenter.common.callback.SimpleCallback
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.AmwaySearchItemBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.gamedetail.rating.edit.RatingEditActivity
import com.halo.assistant.HaloApp
import com.lightgame.utils.Util_System_Keyboard
import com.lightgame.utils.Utils
import java.lang.ref.WeakReference

class AmwaySearchViewHolder(var binding: AmwaySearchItemBinding, val mViewModel: AmwaySearchViewModel) :
    RecyclerView.ViewHolder(binding.root) {

    fun bindView(gameEntity: GameEntity) {
        binding.iconIv.displayGameIcon(gameEntity)
        binding.nameTv.text = gameEntity.name
        binding.addIv.goneIf(!gameEntity.showComment)
        binding.hintTv.goneIf(gameEntity.showComment)

        if (gameEntity.showComment) {
            binding.nameTv.setTextColor(ContextCompat.getColor(binding.root.context, R.color.text_title))
            binding.nameTv.setTypeface(binding.nameTv.typeface, Typeface.BOLD)
        } else {
            binding.nameTv.setTextColor(ContextCompat.getColor(binding.root.context, R.color.text_subtitleDesc))
            binding.nameTv.setTypeface(binding.nameTv.typeface, Typeface.NORMAL)
        }

        binding.root.setOnClickListener {
            val activity = (binding.root.context) as Activity
            Util_System_Keyboard.hideSoftKeyboardByIBinder(binding.root.context, binding.root.windowToken)

            if (gameEntity.showComment) {
                val callback = object : SimpleCallback<Boolean> {
                    override fun onCallback(arg: Boolean) {
                        if (arg) {
                            // 已评论过此游戏
                            GameDetailActivity.startGameDetailActivity(
                                binding.root.context,
                                gameEntity,
                                "安利墙",
                                isSkipGameComment = true
                            )
                        } else {
                            it.context.showRegulationTestDialogIfNeeded {
                                val installPackageName = mViewModel.canUserCommentThisGame(gameEntity)
                                if (gameEntity.directComment || !installPackageName.isNullOrEmpty()) {
                                    MtaHelper.onEvent("发表评论", "进入", "选中游戏_${gameEntity.name}")
                                    val intent = RatingEditActivity.getIntent(
                                        binding.root.context,
                                        gameEntity,
                                        0.0F,
                                        installPackageName,
                                        true
                                    )
                                    activity.startActivity(intent)
                                    activity.finish()
                                } else {
                                    Utils.toast(activity, "安装游戏后才能评论哦")
                                }
                            }
                        }
                    }
                }
                CommentUtils.isUserCommentedOnThisGame(gameEntity.id, WeakReference(callback))
            } else {
                Utils.toast(HaloApp.getInstance().application, "该游戏暂不支持发表评论哦")
            }
        }
    }

}