package com.gh.gamecenter.gamedetail.desc

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.DefaultUrlHandler
import com.gh.common.util.*
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.ItemGameInfoBinding
import com.gh.gamecenter.gamedetail.dialog.GamePermissionDialogFragment
import com.gh.gamecenter.gamedetail.entity.GameInfo
import com.gh.gamecenter.gamedetail.entity.GameInfoItemData

class GameDetailInfoItemAdapter(
    val context: Context,
    val gameInfo: GameInfo,
    private val mViewModel: DescViewModel,
    val gameName: String
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    var datas = ArrayList<GameInfoItemData>()

    init {
        if (gameInfo.manufacturer.isNotEmpty()) {
            when (gameInfo.manufacturerType) {
                "manufacturer" -> {
                    datas.add(GameInfoItemData(title = "厂商", info = gameInfo.manufacturer))
                }
                "publisher" -> {
                    if (gameInfo.developer.isNotEmpty()) {
                        datas.add(GameInfoItemData(title = "开发商", info = gameInfo.developer))
                    }
                    datas.add(GameInfoItemData(title = "发行商", info = gameInfo.manufacturer))
                }
                "developer" -> {
                    datas.add(GameInfoItemData(title = "开发商", info = gameInfo.manufacturer))
                    if (gameInfo.publisher.isNotEmpty()) {
                        datas.add(GameInfoItemData(title = "发行商", info = gameInfo.publisher))
                    }
                }
                else -> {
                    //do nothing
                }
            }
        }
        if (gameInfo.supplier.isNotEmpty()) {
            datas.add(GameInfoItemData(title = "供应商", info = gameInfo.supplier))
        }
        if (gameInfo.creditCode.isNotEmpty()) {
            datas.add(GameInfoItemData(title = "统一社会信用代码", info = gameInfo.creditCode))
        }
        if (gameInfo.contact != null) {
            datas.add(
                GameInfoItemData(
                    title = gameInfo.contact!!.hint,
                    info = gameInfo.contact!!.qq,
                    actionStr = if (gameInfo.contact!!.type == "qq") "咨询" else "加入",
                    key = gameInfo.contact!!.key
                )
            )
        }
        if (gameInfo.version.isNotEmpty()) {
            datas.add(
                GameInfoItemData(
                    title = "当前版本",
                    info = gameInfo.version,
                    actionStr = if (gameInfo.requestUpdateStatus == "on" && mViewModel.game?.getApk()
                            ?.isNotEmpty() == true
                    ) "求更新" else ""
                )
            )
        }
        if (gameInfo.size.isNotEmpty()) {
            datas.add(GameInfoItemData(title = "游戏大小", info = gameInfo.size))
        }
        if (gameInfo.updateTime != 0L) {
            datas.add(GameInfoItemData(title = "更新时间", info = TimeUtils.getFormatTime(gameInfo.updateTime)))
        }
        if (gameInfo.recommendAge.isNotEmpty()) {
            datas.add(GameInfoItemData(title = "适龄等级", info = gameInfo.recommendAge))
        }
        if (!gameInfo.permissions.isNullOrEmpty()) {
            datas.add(GameInfoItemData(title = "权限及用途", info = "查看"))
        }
        if (!gameInfo.privacyPolicyUrl.isNullOrEmpty()) {
            datas.add(GameInfoItemData(title = "隐私政策", info = "查看"))
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return GameDetailInfoItemViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int = datas.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val gameInfoItemData = datas[position]
        if (holder is GameDetailInfoItemViewHolder) {
            holder.binding.divider.setBackgroundColor(R.color.divider.toColor(holder.binding.divider.context))
            holder.binding.infoTv.isSelected = true
            holder.binding.infoTv.text = gameInfoItemData.info
            holder.binding.titleTv.text = gameInfoItemData.title
            holder.binding.infoTv.setTextColor(R.color.text_title.toColor(holder.binding.infoTv.context))
            holder.binding.titleTv.setTextColor(R.color.text_title.toColor(holder.binding.infoTv.context))
            if (gameInfoItemData.actionStr.isNotEmpty()) {
                holder.binding.actionTv.visibility = View.VISIBLE
                holder.binding.actionTv.text = gameInfoItemData.actionStr
                holder.binding.actionTv.setTextColor(R.color.theme_font.toColor(holder.binding.infoTv.context))
            } else {
                holder.binding.actionTv.visibility = View.GONE
            }
            if (gameInfoItemData.title == "权限及用途" || gameInfoItemData.title == "隐私政策") {
                holder.binding.infoTv.setTextColor(ContextCompat.getColor(context, R.color.theme_font))
            }
            holder.binding.root.setOnClickListener {
                when (gameInfoItemData.title) {
                    "权限及用途" -> {
                        GamePermissionDialogFragment.show(context as AppCompatActivity, mViewModel.game, gameInfo)
                    }
                    "隐私政策" -> {
                        gameInfo.privacyPolicyUrl?.let {
                            if (!DefaultUrlHandler.transformNormalScheme(holder.binding.root.context, it, "隐私政策")) {
                                val intent =
                                    WebActivity.getIntent(holder.binding.root.context, it, "隐私政策", false, false)
                                holder.binding.root.context.startActivity(intent)
                            }
                        }
                    }
                }
            }
            holder.binding.actionTv.setOnClickListener {
                when (gameInfoItemData.actionStr) {
                    "求更新" -> {
                        MtaHelper.onEvent("游戏详情_新", "详细信息_我要求更新", gameName)
                        DialogHelper.showDialog(
                            context, "版本求更新", "如果游戏上线了新版本，您可以提交申请，让小助手尽快更新版本喔！",
                            "提交申请", "取消", {
                                mViewModel.sendSuggestion()
                            }, extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
                        )
                    }
                    "咨询" -> {
                        MtaHelper.onEvent("游戏详情_新", "详细信息_咨询", gameName)
                        if (ShareUtils.isQQClientAvailable(context)) {
                            DirectUtils.directToQqConversation(context, gameInfoItemData.info)
                        } else {
                            gameInfoItemData.info.copyTextAndToast("已复制")
                        }
                    }
                    "加入" -> {
                        MtaHelper.onEvent("游戏详情_新", "详细信息_加入", gameName)
                        if (ShareUtils.isQQClientAvailable(context)) {
                            DirectUtils.directToQqGroup(context, gameInfoItemData.key)
                        } else {
                            gameInfoItemData.info.copyTextAndToast("已复制")
                        }
                    }
                }
            }
        }
    }

    class GameDetailInfoItemViewHolder(var binding: ItemGameInfoBinding) : RecyclerView.ViewHolder(binding.root)
}