package com.gh.gamecenter.gamecollection.mine

import android.view.LayoutInflater
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.databinding.ItemGameCollectionFlexTagBinding
import com.gh.gamecenter.databinding.ItemMyGameCollectionBinding
import com.gh.gamecenter.entity.GamesCollectionEntity
import com.gh.gamecenter.entity.TagInfoEntity
import com.gh.gamecenter.gamecollection.detail.GameCollectionDetailActivity
import com.gh.gamecenter.gamecollection.publish.GameCollectionEditActivity

class MyGameCollectionViewHolder(
    val binding: ItemMyGameCollectionBinding,
    val mViewModel: MyGameCollectionViewModel,
    val entrance: String,
    val path: String
) :
    BaseRecyclerViewHolder<GamesCollectionEntity>(binding.root) {

    fun bindGameCollectionItem(entity: GamesCollectionEntity) {
        ImageUtils.display(binding.posterView, entity.cover)
        binding.gameCollectionNameTv.text = entity.title
        binding.createTimeTv.goneIf(!(entity.status == "pass" && entity.display != "self_only"))
        binding.createTimeTv.text = TimeUtils.getFormatTime(entity.time?.create ?: 0, "MM-dd")
        binding.statusView.goneIf(!(entity.status != "pass" || entity.display == "self_only"))
        binding.publishBtn.goneIf(entity.display != "self_only")
        initTagsUI(entity.tags ?: arrayListOf())
        val statusLabelRes = entity.getStatusLabelRes()
        if (statusLabelRes > 0) {
            binding.statusView.setImageResource(statusLabelRes)
        }

        binding.statusView.setOnClickListener {
            when {
                entity.display == "self_only" && entity.status == "pass" -> {
                    DialogHelper.showDialog(
                        binding.root.context,
                        "仅自己可见",
                        "游戏单开启“仅自己可见”后，将不会对其他用户展示，您可以通过关闭仅自己可见或者投稿分享游戏单",
                        "我知道了",
                        "",
                        {

                        },
                        extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
                    )
                }
                entity.status == "pending" -> {
                    DialogHelper.showDialog(binding.root.context, "审核中", "游戏单会在1-2个工作日内审核完成，请您耐心等候", "我知道了", "", {

                    }, extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true))
                }
                entity.status == "failed" -> {
                    DialogHelper.showDialog(
                        binding.root.context,
                        "未通过",
                        "您的游戏单可能含有违规内容或语意不明确，请您仔细检查后再次尝试提交",
                        "我知道了",
                        "",
                        {

                        },
                        extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
                    )
                }
            }
        }
        binding.root.setOnClickListener {
            if (entity.status == "pass") {
                binding.root.context.startActivity(
                    GameCollectionDetailActivity.getIntent(
                        binding.root.context,
                        entity.id
                    )
                )
            } else {
                binding.statusView.performClick()
            }
        }

        binding.publishBtn.setOnClickListener {
            if ((entity.count?.game ?: 0) >= 8) {
                DialogHelper.showDialog(
                    binding.root.context,
                    "温馨提示",
                    "投稿通过后，将自动关闭“仅自己可见”，所有用户都能浏览到游戏单，确定投稿？",
                    "确定",
                    "取消",
                    {
                        mViewModel.publishGameCollection(entity)
                    },
                    extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
                )
            } else {
                DialogHelper.showDialog(binding.root.context, "温馨提示", "游戏单需要收录至少8个游戏，才可以投稿至游戏单广场哦~", "添加游戏", "我知道了", {
                    binding.root.context.startActivity(
                        GameCollectionEditActivity.getIntent(
                            binding.root.context,
                            entity,
                            entrance,
                            path
                        )
                    )
                }, extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true))
            }
        }
        binding.editBtn.setOnClickListener {
            binding.root.context.startActivity(
                GameCollectionEditActivity.getIntent(
                    binding.root.context,
                    entity,
                    entrance,
                    path
                )
            )
        }
        binding.deleteBtn.setOnClickListener {
            DialogHelper.showDialog(binding.root.context, "温馨提示", "游戏单删除后将无法恢复，是否确认删除", "确定", "取消", {
                mViewModel.deleteGameCollection(entity)
            }, extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true))
        }
    }

    private fun initTagsUI(tags: ArrayList<TagInfoEntity>) {
        binding.gameCollectionTagsContainer.removeAllViews()
        tags.forEachIndexed { index, tag ->
            val tagBinding =
                ItemGameCollectionFlexTagBinding.inflate(LayoutInflater.from(binding.root.context), null, false)
            tagBinding.tagNameTv.text = tag.name
            tagBinding.divider.goneIf(index == tags.size - 1)
            binding.gameCollectionTagsContainer.addView(tagBinding.root)
        }
    }
}