package com.gh.gamecenter.personalhome.border

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.feature.exposure.time.TimeUtil
import com.gh.common.util.*
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.*
import com.gh.gamecenter.databinding.AvatarItemBinding
import com.gh.gamecenter.feature.entity.AvatarBorderEntity
import com.gh.gamecenter.login.user.UserManager
import com.lightgame.adapter.BaseRecyclerAdapter

class ChooseAvatarBorderAdapter(
    context: Context,
    private val mFragment: ChooseAvatarBorderFragment,
    private val mIsFree: Boolean,
    private val mCategoryId: String,
    private val mClickCallback: (entity: AvatarBorderEntity, isSelected: Boolean) -> Unit
) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    var entityList: ArrayList<AvatarBorderEntity> = arrayListOf()
    private var mIsFirstIn = true

    fun setListData(updateData: List<AvatarBorderEntity>) {
        entityList.clear()
        entityList.addAll(updateData)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return PendantBackgroundViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int = entityList.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is PendantBackgroundViewHolder) {
            val borderEntity = entityList[position]
            ImageUtils.display(holder.binding.imageSdv, borderEntity.url)
            // 是否可使用
            var isEnable = false
            when {
                // 普通挂件
                mIsFree -> {
                    isEnable = true
                    holder.binding.descTv.visibility = View.GONE
                }

                // 商品挂件但未兑换或者已过期，显示光能
                !mIsFree && (borderEntity.expire == null
                        || (borderEntity.expire != null
                        && borderEntity.expire!! != 0L
                        && borderEntity.expire!! < TimeUtil.currentTime())) -> {
                    isEnable = false
                    holder.binding.descTv.run {
                        visibility = View.VISIBLE
                        setTextColor(com.gh.gamecenter.common.R.color.secondary_red.toColor(mContext))
                        text = "${borderEntity.energy}光能"
                    }
                }

                // 商品挂件已兑换（expire=0代表永久可使用），显示有效期
                !mIsFree && borderEntity.expire != null
                        && (borderEntity.expire!! == 0L
                        || borderEntity.expire!! > TimeUtil.currentTime()) -> {
                    isEnable = true
                    holder.binding.descTv.run {
                        visibility = View.VISIBLE
                        setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
                        text = if (borderEntity.expire!! == 0L) {
                            "有效期9999天"
                        } else {
                            val duration = (borderEntity.expire!! - TimeUtil.currentTime()) / 86400
                            "有效期${duration}天"
                        }
                    }
                }
            }

            holder.binding.run {
                nameTv.text = borderEntity.name
                wearIv.visibleIf(UserManager.getInstance().userInfoEntity?.iconBorder?.id ?: "" == borderEntity.id)
                checkIv.visibleIf(SPUtils.getString(Constants.SP_CHOOSE_AVATAR_ID) == borderEntity.id)
                checkBorderView.visibleIf(SPUtils.getString(Constants.SP_CHOOSE_AVATAR_ID) == borderEntity.id)
            }

            if (SPUtils.getString(Constants.SP_CHOOSE_AVATAR_ID) == borderEntity.id) {
                mFragment.setParentBottomVisible(isEnable)
                if (mIsFirstIn) {
                    mClickCallback.invoke(borderEntity, true)
                    mIsFirstIn = false
                }
            }

            holder.itemView.setOnClickListener {
                IntegralLogHelper.logPendent(
                    "click_pendant",
                    "头像挂件页",
                    borderEntity.id,
                    mCategoryId
                )
                // 根据是否可使用来决定是否显示底部的使用/停用按钮
                mFragment.setParentBottomVisible(isEnable)
                if (isEnable) {
                    if (SPUtils.getString(Constants.SP_CHOOSE_AVATAR_ID) == borderEntity.id) {
                        SPUtils.setString(Constants.SP_CHOOSE_AVATAR_ID, "")
                        mClickCallback.invoke(borderEntity, false)
                    } else {
                        SPUtils.setString(Constants.SP_CHOOSE_AVATAR_ID, borderEntity.id)
                        mClickCallback.invoke(borderEntity, true)
                    }
                    holder.binding.checkIv.goneIf(SPUtils.getString(Constants.SP_CHOOSE_AVATAR_ID) != borderEntity.id)
                    holder.binding.checkBorderView.goneIf(SPUtils.getString(Constants.SP_CHOOSE_AVATAR_ID) != borderEntity.id)
                    notifyDataSetChanged()
                } else {
                    ToastUtils.toast("此头像暂未上线，敬请期待")
                }
            }
        }
    }

    class PendantBackgroundViewHolder(val binding: AvatarItemBinding) : BaseRecyclerViewHolder<String>(binding.root)
}