package com.gh.gamecenter.cloudarchive

import android.content.Context
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.ArchiveDownloadButtonHelper
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.formatTime
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.databinding.ItemMyArchiveBinding
import com.gh.gamecenter.databinding.ItemMyShareArchiveBinding
import com.gh.gamecenter.entity.ArchiveEntity
import com.gh.gamecenter.feature.entity.GameEntity

class MyArchiveAdapter(
    context: Context,
    private val mFragment: Fragment,
    private val mViewModel: MyArchiveViewModel,
    private val mCloudArchiveManagerViewModel: CloudArchiveManagerViewModel,
    private val mType: MyArchiveFragment.Type,
    private val mGameEntity: GameEntity?
) : ListAdapter<ArchiveEntity>(context) {

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) ItemViewType.ITEM_FOOTER else ItemViewType.ITEM_BODY
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
        if (viewType == ItemViewType.ITEM_FOOTER) FooterViewHolder(
            mLayoutInflater.inflate(
                com.gh.gamecenter.common.R.layout.refresh_footerview,
                parent,
                false
            )
        ) else if (mType == MyArchiveFragment.Type.MY_SHARE_ARCHIVE) {
            MyShareArchiveItemViewHolder(parent.toBinding())
        } else {
            MyArchiveItemViewHolder(parent.toBinding())
        }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyArchiveItemViewHolder) {
            val entity = mEntityList[position]
            holder.binding.run {
                titleTv.text = entity.name
                timeTv.text = entity.time.create.formatTime("yyyy-MM-dd HH:mm")
                versionTv.text = "版本：${entity.gameVersion}"
                optionsIv.setOnClickListener {
                    MyArchiveOptionDialogFragment.show(
                        mContext as AppCompatActivity,
                        mViewModel,
                        mType,
                        entity,
                        mGameEntity
                    )
                }
                val entrance = if (mType == MyArchiveFragment.Type.MY_ARCHIVE) "云存档-我的存档" else "云存档-我的下载"
                ArchiveDownloadButtonHelper.bindItem(
                    mContext,
                    entrance,
                    mFragment,
                    mGameEntity?.getUniquePackageName() ?: "",
                    mCloudArchiveManagerViewModel,
                    entity,
                    actionTv,
                    mGameEntity
                ) {
                    if (mContext is CloudArchiveManagerActivity) {
                        (mContext as CloudArchiveManagerActivity).updateMyShareArchive()
                        (mContext as CloudArchiveManagerActivity).updateMyDownloadArchive()
                    }
                }
            }
        }

        if (holder is MyShareArchiveItemViewHolder) {
            val entity = mEntityList[position]
            holder.binding.run {
                titleTv.text = entity.name
                timeTv.text = entity.time.update.formatTime("yyyy-MM-dd HH:mm")
                versionTv.text = "版本：${entity.gameVersion}"
                descTv.text = entity.desc
                optionsIv.setOnClickListener {
                    MyArchiveOptionDialogFragment.show(
                        mContext as AppCompatActivity,
                        mViewModel,
                        mType,
                        entity,
                        mGameEntity
                    )
                }
                ArchiveDownloadButtonHelper.bindItem(
                    mContext,
                    "云存档-我的分享",
                    mFragment,
                    mGameEntity?.getUniquePackageName() ?: "",
                    mCloudArchiveManagerViewModel,
                    entity,
                    actionTv,
                    mGameEntity
                ) {
                    if (mContext is CloudArchiveManagerActivity) {
                        (mContext as CloudArchiveManagerActivity).updateMyArchive()
                        (mContext as CloudArchiveManagerActivity).updateMyDownloadArchive()
                    }
                }
            }
        }

        if (holder is FooterViewHolder) {
            holder.initItemPadding()
            holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
        }
    }

    override fun getItemCount(): Int = if (mEntityList.isNullOrEmpty()) 0 else mEntityList.size + FOOTER_ITEM_COUNT

    class MyArchiveItemViewHolder(val binding: ItemMyArchiveBinding) : BaseRecyclerViewHolder<Any>(binding.root)
    class MyShareArchiveItemViewHolder(val binding: ItemMyShareArchiveBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}