package com.gh.download.dialog

import android.view.View
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.common.constant.Config
import com.gh.common.exposure.ExposureEvent
import com.gh.common.util.*
import com.gh.download.DownloadManager
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.throwExceptionInDebug
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.databinding.DownloadDialogInstalledItemBinding
import com.gh.gamecenter.entity.ApkEntity
import com.lightgame.download.DownloadStatus

class DownloadDialogInstalledItemViewHolder(val binding: DownloadDialogInstalledItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root) {

    fun bindInstalledItem(
        apkEntity: ApkEntity,
        viewModel: DownloadViewModel,
        traceEvent: ExposureEvent?,
        entrance: String,
        path: String,
        location: String
    ) {

        val gameEntity = viewModel.gameEntity
        val apkCollection = apkEntity.apkCollection
        val pluginDesc = gameEntity.pluginDesc
        val apk = if (apkCollection != null) {
            ApkEntity(
                platformIcon = apkCollection.newIcon,
                platformName = apkCollection.name,
                remark = apkCollection.remark
            )
        } else apkEntity
        ImageUtils.display(binding.icon, apk.getPlatformIcon())
        binding.name.text = apk.getPlatformName()
        binding.remark.text = apk.remark
        binding.root.setBackgroundResource(R.drawable.download_dialog_item_background)
        binding.launch.text = "启动${pluginDesc}版"
        binding.pluggable.text = "${pluginDesc}此版本"
        binding.update.text = "更新${pluginDesc}版"

        if (apkEntity.apkLink != null) {
            throwExceptionInDebug("apkLink 不应该出现在这里")
        } else if (apkCollection != null || apkEntity.downloadInstruction.isNotEmpty()) {
            binding.collection.visibility = View.VISIBLE
            binding.collectionPluggableHint.visibility = View.GONE
            binding.downloadStatusIcon.visibility = View.GONE
            binding.progressbar.visibility = View.GONE
            binding.pluggable.visibility = View.GONE
            binding.install.visibility = View.GONE
            binding.update.visibility = View.GONE
            binding.status.visibility = View.GONE
            binding.launch.visibility = View.GONE
            if (apkCollection != null) {
                binding.remark.goneIf(apkCollection.remark.isEmpty())
//                binding.root.setBackgroundResource(R.drawable.download_dialog_item_collection_background)
            } else {
                binding.remark.goneIf(apkEntity.remark.isEmpty())
            }

            binding.collection.text = if (apkCollection != null) {
                if (apkCollection.showPluggableHint) {
                    binding.collectionPluggableHint.visibility = View.VISIBLE
                    binding.collection.setTextColor(R.color.text_05CBA3.toColor(binding.collection.context))
                    gameEntity.pluginDesc + "此版本"
                } else {
                    binding.collection.setTextColor(R.color.text_body.toColor(binding.collection.context))
                    "查看合集"
                }
            } else "查看详情"
            itemView.setTag(DownloadDialogAdapter.ITEM_TAG_KEY, DownloadDialogItemStatus.COLLECTION)
        } else {
            binding.collection.visibility = View.GONE

            val downloadEntity = DownloadManager.getInstance().getDownloadEntityByUrl(apkEntity.url)
            if (downloadEntity != null) {
                binding.downloadStatusIcon.visibility = View.VISIBLE
                binding.progressbar.visibility = View.VISIBLE
                binding.status.visibility = View.VISIBLE
                binding.remark.visibility = View.GONE
                binding.launch.visibility = View.GONE
                binding.pluggable.visibility = View.GONE
                binding.update.visibility = View.GONE
                binding.install.goneIf(DownloadStatus.done != downloadEntity.status)

                binding.downloadStatusIcon.setImageResource(R.drawable.download_dialog_status_pause)
                binding.progressbar.progress = downloadEntity.percent.toInt()
                binding.status.text = DownloadDialogItemViewHolder.getDownloadingStatusText(downloadEntity)

                if (DownloadStatus.done == downloadEntity.status) {
                    itemView.setTag(DownloadDialogAdapter.ITEM_TAG_KEY, DownloadDialogItemStatus.INSTALL)
                } else {
                    itemView.setTag(DownloadDialogAdapter.ITEM_TAG_KEY, DownloadDialogItemStatus.DOWNLOADING)
                }
            } else {
                binding.downloadStatusIcon.visibility = View.GONE
                binding.progressbar.visibility = View.GONE
                binding.status.visibility = View.GONE
                binding.install.visibility = View.GONE
                binding.remark.goneIf(apkEntity.remark.isEmpty())

                if (PackageUtils.isCanPluggable(apkEntity)) {
                    binding.pluggable.visibility = View.VISIBLE
                    binding.update.visibility = View.GONE
                    binding.launch.visibility = View.GONE

                    itemView.setTag(DownloadDialogAdapter.ITEM_TAG_KEY, DownloadDialogItemStatus.PLUGGABLE)
                } else if (PackageUtils.isCanUpdate(apkEntity, gameEntity.id)) {
                    binding.update.visibility = View.VISIBLE
                    binding.pluggable.visibility = View.GONE
                    binding.launch.visibility = View.GONE

                    itemView.setTag(DownloadDialogAdapter.ITEM_TAG_KEY, DownloadDialogItemStatus.UPDATE)
                } else if (PackageUtils.getGhId(apkEntity.packageName) == gameEntity.id) {
                    binding.status.visibility = View.VISIBLE
                    binding.pluggable.visibility = View.GONE
                    binding.update.visibility = View.GONE

                    var isFilter = false
                    Config.getSettings()?.gameDownloadBlackList?.forEach { pkgName ->
                        if (pkgName == apkEntity.packageName) {
                            isFilter = true
                        }
                    }

                    if (isFilter) {
                        // 已安装
                        binding.status.visibility = View.VISIBLE
                        binding.launch.visibility = View.GONE

                        binding.status.text = "已安装"
                        itemView.setTag(DownloadDialogAdapter.ITEM_TAG_KEY, DownloadDialogItemStatus.INSTALLED)
                    } else {
                        // 点击启动
                        binding.launch.visibility = View.VISIBLE
                        binding.status.visibility = View.GONE

                        binding.status.text = "点击启动"
                        itemView.setTag(DownloadDialogAdapter.ITEM_TAG_KEY, DownloadDialogItemStatus.LAUNCH)
                    }
                } else {
                    throwExceptionInDebug("下载弹窗-我的版本出现未知类型")
                }
            }
        }

        DownloadDialogItemViewHolder.setDownloadClickListener(
            itemView,
            apkEntity,
            viewModel,
            traceEvent,
            entrance,
            path,
            location
        )
    }
}