package com.gh.gamecenter.gamedetail.cloudarchive

import android.content.Context
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.ArchiveDownloadButtonHelper
import com.gh.gamecenter.R
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.CloudArchiveItemBinding
import com.gh.gamecenter.entity.ArchiveEntity
import com.gh.gamecenter.feature.entity.GameEntity

class CloudArchiveListAdapter(
    context: Context,
    private val mPackageName: String,
    private val mFragment: Fragment,
    private val mViewModel: CloudArchiveListViewModel,
    private val mGameEntity: GameEntity?
) : ListAdapter<ArchiveEntity>(context) {

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

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemViewType.ITEM_BODY -> CloudArchiveItemViewHolder(parent.toBinding())
            else -> FooterViewHolder(mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false))
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is CloudArchiveItemViewHolder -> {
                val entity = mEntityList[position]
                holder.binding.run {
                    entity.run {
                        titleTv.text = name
                        timeTv.text = time.update.formatTime("yyyy-MM-dd HH:mm")
                        versionTv.text = "版本：${gameVersion}"
                        contentTv.text = desc
                        userIcon.display("", user.icon, user.auth?.icon)
                        userName.text = user.name
                        usageTv.text = "${count.usage} 次使用"
                        ArchiveDownloadButtonHelper.bindItem(
                            mContext,
                            "游戏详情${mViewModel.getSortTypeValue()}",
                            mFragment,
                            mPackageName,
                            entity,
                            downloadBtn,
                            mGameEntity
                        )
                    }
                }
            }
            is FooterViewHolder -> holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
        }
    }

    inner class CloudArchiveItemViewHolder(val binding: CloudArchiveItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}