package com.gh.download.dialog

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.callback.OnViewClickListener
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.common.util.*
import com.gh.gamecenter.NewsDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.*
import com.gh.gamecenter.feature.entity.GameEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class DownloadDialogAdapter(
    context: Context,
    val viewModel: DownloadViewModel,
    val listData: List<DownloadDialogItemData>,
    val isCollectionPage: Boolean,
    private val mTraceEvent: ExposureEvent?,
    private val mEntrance: String,
    private val mLocation: String
) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    private val mPath = if (isCollectionPage) {
        "下载弹窗-二级页"
    } else {
        "下载弹窗"
    }

    override fun getItemViewType(position: Int): Int {
        val itemData = listData[position]
        if (itemData.links != null) return ITEM_LINK
        if (itemData.section != null) return ITEM_SECTION
        if (itemData.installed != null) return ITEM_INSTALLED
        if (itemData.instruction != null) return ITEM_INSTRUCTION
        if (itemData.platformRequest != null) return ITEM_PLATFORM_REQUEST
        return ITEM_NORMAL
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        return when (viewType) {
            ITEM_LINK -> {
                view = mLayoutInflater.inflate(R.layout.download_dialog_link_item, parent, false)
                DownloadDialogLinkItemViewHolder(DownloadDialogLinkItemBinding.bind(view))
            }
            ITEM_SECTION -> {
                view = mLayoutInflater.inflate(R.layout.download_dialog_section_item, parent, false)
                DownloadDialogSectionItemViewHolder(DownloadDialogSectionItemBinding.bind(view))
            }
            ITEM_INSTALLED -> {
                view = mLayoutInflater.inflate(R.layout.download_dialog_installed_item, parent, false)
                DownloadDialogInstalledItemViewHolder(DownloadDialogInstalledItemBinding.bind(view))
            }
            ITEM_INSTRUCTION -> {
                view = mLayoutInflater.inflate(R.layout.download_dialog_instruction_item, parent, false)
                DownloadDialogInstructionItemViewHolder(DownloadDialogInstructionItemBinding.bind(view))
            }
            ITEM_PLATFORM_REQUEST -> {
                view = mLayoutInflater.inflate(R.layout.download_dialog_platform_request_item, parent, false)
                DownloadDialogPlatformRequestItemViewHolder(DownloadDialogPlatformRequestItemBinding.bind(view))
            }
            else -> {
                view = mLayoutInflater.inflate(R.layout.download_dialog_item, parent, false)
                DownloadDialogItemViewHolder(DownloadDialogItemBinding.bind(view))
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is DownloadDialogSectionItemViewHolder -> {
                (holder.binding.root.layoutParams as RecyclerView.LayoutParams).apply {
                    topMargin = if (position > 0) {
                        if (position - 1 >= 0 && (listData[position - 1].installed != null || listData[position - 1].normal != null)) {
                            4f.dip2px()
                        } else 12f.dip2px()
                    } else 0
                    holder.binding.root.layoutParams = this
                }
                val section = listData[position].section
                holder.binding.title.text = if (section == DownloadDialogSectionType.OTHER) "更多版本" else "我的版本"
                holder.binding.otherVersionHint.goneIf(section != DownloadDialogSectionType.OTHER)
            }
            is DownloadDialogLinkItemViewHolder -> {
                val links = listData[position].links
                val clickListener = OnViewClickListener<GameEntity.PluginLink> { _, data ->
                    when (data.linkType) {
                        "dialog" -> {
                            DownloadLinkDialog.showDownloadDialog(mContext, data)
                        }
                        "article" -> {
                            mContext.startActivity(NewsDetailActivity.getIntentById(mContext, data.linkId, mEntrance))
                        }
                        "qa" -> {
                            DirectUtils.directToQa(mContext, data.linkText, data.linkId)
                        }
                        "qa_collection" -> {
                            DirectUtils.directToQaCollection(mContext, data.linkText, data.linkId)
                        }
                        else -> {
                            //Utils.toast(mContext, "暂不支持类型:" + data.linkType)
                            DirectUtils.directToLinkPage(mContext, data.getLinkEntity(), mEntrance, mPath)
                        }
                    }

//                    MtaHelper.onEvent(DownloadDialog.MTA_KEY, "点击", viewModel.gameEntity.name + "_" + data.title)
                }
                holder.binding.run {
                    links?.let {
                        leftLink.setOnClickListener { view ->
                            clickListener.onClick(view, links[0])
                        }
                        rightLink.setOnClickListener { view ->
                            clickListener.onClick(view, links[1])
                        }
                    }
                    leftLink.visibleIf(!links.isNullOrEmpty())
                    rightLink.visibleIf(links?.size ?: 0 > 1)
                    leftLink.text = if (links.isNullOrEmpty()) "" else links[0].title
                    rightLink.text = if (links?.size ?: 0 > 1) links?.get(1)?.title else ""
                    leftLink.background = GradientDrawable().apply {
                        cornerRadius = 8F.dip2px().toFloat()
                        setStroke(0.5F.dip2px(), R.color.divider.toColor(mContext))
                    }
                    rightLink.background = GradientDrawable().apply {
                        cornerRadius = 8F.dip2px().toFloat()
                        setStroke(0.5F.dip2px(), R.color.divider.toColor(mContext))
                    }
                }
            }
            is DownloadDialogInstructionItemViewHolder -> {
                holder.bindItem(listData, position, mEntrance)
            }
            is DownloadDialogPlatformRequestItemViewHolder -> {
                holder.bindItem(viewModel.gameEntity)
            }
            is DownloadDialogInstalledItemViewHolder -> {
                holder.bindInstalledItem(
                    listData[position].installed!!,
                    viewModel,
                    mTraceEvent,
                    mEntrance,
                    mPath,
                    mLocation
                )
                throwExceptionInDebug("合集页面不应该存在该条数据", isCollectionPage)
            }
            is DownloadDialogItemViewHolder -> {
                holder.bindItem(
                    listData,
                    position,
                    viewModel,
                    isCollectionPage,
                    mTraceEvent,
                    mEntrance,
                    mPath,
                    mLocation
                )
            }
        }
    }

    override fun getItemCount(): Int {
        return listData.size
    }

    companion object {
        private const val ITEM_LINK = 200
        private const val ITEM_SECTION = 201
        private const val ITEM_INSTALLED = 202
        private const val ITEM_NORMAL = 203
        private const val ITEM_INSTRUCTION = 204
        private const val ITEM_PLATFORM_REQUEST = 205

        const val ITEM_TAG_KEY = R.id.download_item_type
    }
}