package com.halo.assistant.fragment.user.auth

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
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.*
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.TimeUtils.getFormatTime
import com.gh.gamecenter.databinding.ItemUserAuthBinding
import com.gh.gamecenter.entity.UserAuthEntity
import com.lightgame.utils.Utils

class UserAuthAdapter(context: Context, private val mViewModel: UserAuthViewModel): ListAdapter<UserAuthEntity>(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 {
        return if (viewType == ItemViewType.ITEM_FOOTER) {
            FooterViewHolder(mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false))
        } else {
            UserAuthItemViewHolder(parent.toBinding())
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is UserAuthItemViewHolder) {
            val entity = mEntityList.safelyGetInRelease(position)
            holder.binding.run {
                nameTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
                applyingTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
                expireTimeTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
                timeTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
                divider.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(mContext))
                applyBtn.background = R.drawable.button_round_primary_light.toDrawable(mContext)
                applyBtn.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(mContext))
                ImageUtils.display(iconIv, entity?.auth?.icon)
                nameTv.text = entity?.auth?.text
                expireTimeTv.visibleIf(entity?.time != null)
                timeTv.visibleIf(entity?.time != null) {
                    timeTv.text = "${getFormatTime(entity?.time?.start ?: 0L, "yyyy.MM.dd")}-${getFormatTime(entity?.time?.end ?: 0L, "yyyy.MM.dd")}"
                }
                applyBtn.goneIf(entity?.isShow == true) {
                    applyBtn.setDebouncedClickListener {
                        mViewModel.postUserAuth(entity?.id ?: "")
                    }
                }
                applyingTv.goneIf(entity?.isShow == false) {
                    applyingTv.setOnClickListener {
                        Utils.toast(mContext, "无法取消认证展示")
                    }
                }
            }
        }

        if (holder is FooterViewHolder) {
            holder.initItemPadding()
            holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
            holder.hint.setTextColor(com.gh.gamecenter.common.R.color.text_instance.toColor(mContext))
            val lp = holder.itemView.layoutParams as RecyclerView.LayoutParams
            lp.height = DisplayUtils.dip2px(48F)
            lp.width = ViewGroup.LayoutParams.MATCH_PARENT
            holder.itemView.layoutParams = lp
        }
    }

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

    class UserAuthItemViewHolder(val binding: ItemUserAuthBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}