package com.gh.gamecenter.savegame

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.cloudarchive.CloudArchiveManagerActivity
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.databinding.ItemGameSaveListBinding
import com.gh.gamecenter.entity.GameEntity

class GameArchiveListInstalledAdapter(context: Context, var mViewModel: GameArchiveListViewModel) :
    ListAdapter<GameEntity>(context) {
    override fun getItemViewType(position: Int): Int {
        if (position == itemCount - 1) {
            return ItemViewType.ITEM_FOOTER
        }
        return ItemViewType.GAME_NORMAL
    }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is GameArchiveListViewHolder -> {
                val gameEntity = mEntityList[position]
                val padTop = if (position == 0) 16F.dip2px() else 8F.dip2px()
                holder.itemView.setPadding(16F.dip2px(), padTop, 16F.dip2px(), 8F.dip2px())
                holder.binding.run {
                    root.background = R.drawable.reuse_listview_item_style.toDrawable(root.context)
                    nameTv.setTextColor(R.color.text_title.toColor(root.context))
                    downloadBtn.text = "存档"
                    downloadBtn.setTextColor(R.color.theme_font.toColor(root.context))
                    iconIv.displayGameIcon(gameEntity)
                    nameTv.text = gameEntity.name
                    downloadBtn.setOnClickListener {
//                        ToastUtils.showToast("点击了存档")
                        mContext.startActivity(
                            CloudArchiveManagerActivity.getIntent(
                                mContext,
                                gameEntity,
                                gameEntity.configUrl,
                                "我的光环-游戏存档"
                            )
                        )

                    }
                    downloadBtn.background = R.drawable.button_round_primary_light.toDrawable(root.context)
                }
                holder.itemView.setOnClickListener {
                    GameDetailActivity.startGameDetailActivity(
                        mContext,
                        gameEntity.id,
                        ""
                    )
                }
            }
            is FooterViewHolder -> {
                holder.initItemPadding()
                holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
            }

        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList == null || mEntityList.isEmpty()) 0 else mEntityList.size + 1
    }

    override fun areItemsTheSame(oldItem: GameEntity?, newItem: GameEntity?): Boolean {
        return oldItem?.id == newItem?.id
    }

    class GameArchiveListViewHolder(var binding: ItemGameSaveListBinding) : RecyclerView.ViewHolder(binding.root)
}