package com.gh.gamecenter.forum.home

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.feature.exposure.time.TimeUtil
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
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.core.utils.TimeUtils
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.databinding.ForumActivityItemBinding
import com.gh.gamecenter.entity.ForumActivityEntity

class ForumActivityAdapter(
    context: Context,
    val mViewModel: ForumActivityViewModel,
    val mEntrance: String
) : ListAdapter<ForumActivityEntity>(context) {

    var hasCategory = false

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                val view = mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false)
                FooterViewHolder(view)
            }

            else -> {
                ForumActivityViewHolder(parent.toBinding())
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList.isNotEmpty()) mEntityList.size + FOOTER_ITEM_COUNT else 0
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is ForumActivityViewHolder -> {
                holder.binding.run {
                    val isDarkModeOn = DarkModeUtils.isDarkModeOn(mContext)
                    root.background = R.drawable.bg_forum_activity_card.toDrawable(mContext)
                    root.setPadding(0, 0, 0, if (isDarkModeOn) 16F.dip2px() else 26F.dip2px())
                    root.layoutParams = (root.layoutParams as ViewGroup.MarginLayoutParams).apply {
                        setMargins(16F.dip2px(), if (position == 0 && !hasCategory) 8F.dip2px() else 0, 16F.dip2px(), 0)
                    }

                    val activityEntity = mEntityList[position]
                    ImageUtils.display(introPic, activityEntity.image.cover)
                    title.text = activityEntity.title
                    title.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
                    startTimeTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
                    endTimeTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))

                    var status = ""
                    val currentTime = TimeUtil.currentTime()
                    when {
                        // 未开始
                        currentTime < activityEntity.effectTime.start -> {
                            status = "未开始"
                            statusContainer.setCardBackgroundColor(com.gh.gamecenter.common.R.color.ui_container_1.toColor(mContext))
                            statusIv.setImageResource(R.drawable.ic_activity_waiting)
                            statusTv.text = "未开始"
                            statusTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
                            val startTime = TimeUtils.getFormatTime(activityEntity.effectTime.start, "yyyy.MM.dd")
                            val endTime = TimeUtils.getFormatTime(activityEntity.effectTime.end, "yyyy.MM.dd")
                            timeIv.visibility = View.VISIBLE
                            endTimeTv.visibility = View.VISIBLE
                            startTimeTv.text = startTime
                            endTimeTv.text = endTime
                        }

                        // 进行中
                        currentTime > activityEntity.effectTime.start
                                && (currentTime < activityEntity.effectTime.end
                                || activityEntity.effectTime.end == null
                                || activityEntity.effectTime.end == 0L) -> {
                            status = "进行中"
                            statusContainer.setCardBackgroundColor(com.gh.gamecenter.common.R.color.bg_1AFA8850.toColor(mContext))
                            statusIv.setImageResource(R.drawable.ic_activity_ongoing)
                            statusTv.text = "进行中"
                            statusTv.setTextColor(com.gh.gamecenter.common.R.color.text_FA8850.toColor(mContext))
                            if (activityEntity.effectTime.end == null || activityEntity.effectTime.end == 0L) {
                                timeIv.visibility = View.GONE
                                endTimeTv.visibility = View.GONE
                                startTimeTv.text = "长期有效"
                            } else {
                                val startTime = TimeUtils.getFormatTime(activityEntity.effectTime.start, "yyyy.MM.dd")
                                val endTime = TimeUtils.getFormatTime(activityEntity.effectTime.end, "yyyy.MM.dd")
                                timeIv.visibility = View.VISIBLE
                                endTimeTv.visibility = View.VISIBLE
                                startTimeTv.text = startTime
                                endTimeTv.text = endTime
                            }
                        }

                        // 正在评奖
                        currentTime > activityEntity.effectTime.end
                                && currentTime < activityEntity.awardTime.start -> {
                            status = "正在评奖"
                            statusContainer.setCardBackgroundColor(com.gh.gamecenter.common.R.color.bg_1AFA8850.toColor(mContext))
                            statusIv.setImageResource(R.drawable.ic_activity_selection)
                            statusTv.text = "正在评奖"
                            statusTv.setTextColor(com.gh.gamecenter.common.R.color.text_FA8850.toColor(mContext))
                            val startTime = TimeUtils.getFormatTime(activityEntity.effectTime.start, "yyyy.MM.dd")
                            val endTime = TimeUtils.getFormatTime(activityEntity.effectTime.end, "yyyy.MM.dd")
                            timeIv.visibility = View.VISIBLE
                            endTimeTv.visibility = View.VISIBLE
                            startTimeTv.text = startTime
                            endTimeTv.text = endTime
                        }

                        // 奖励发放中
                        currentTime > activityEntity.awardTime.start
                                && currentTime < activityEntity.awardTime.end -> {
                            status = "奖励发放中"
                            statusContainer.setCardBackgroundColor(com.gh.gamecenter.common.R.color.bg_1AFA8850.toColor(mContext))
                            statusIv.setImageResource(R.drawable.ic_activity_award)
                            statusTv.text = "奖励发放中"
                            statusTv.setTextColor(com.gh.gamecenter.common.R.color.text_FA8850.toColor(mContext))
                            val startTime = TimeUtils.getFormatTime(activityEntity.effectTime.start, "yyyy.MM.dd")
                            val endTime = TimeUtils.getFormatTime(activityEntity.effectTime.end, "yyyy.MM.dd")
                            timeIv.visibility = View.VISIBLE
                            endTimeTv.visibility = View.VISIBLE
                            startTimeTv.text = startTime
                            endTimeTv.text = endTime
                        }

                        // 已结束
                        currentTime > activityEntity.awardTime.end -> {
                            status = "已结束"
                            statusContainer.setCardBackgroundColor(com.gh.gamecenter.common.R.color.ui_container_1.toColor(mContext))
                            statusIv.setImageResource(R.drawable.ic_activity_end)
                            statusTv.text = "已结束"
                            statusTv.setTextColor(com.gh.gamecenter.common.R.color.text_instance.toColor(mContext))
                            val startTime = TimeUtils.getFormatTime(activityEntity.effectTime.start, "yyyy.MM.dd")
                            val endTime = TimeUtils.getFormatTime(activityEntity.effectTime.end, "yyyy.MM.dd")
                            timeIv.visibility = View.VISIBLE
                            endTimeTv.visibility = View.VISIBLE
                            startTimeTv.text = startTime
                            endTimeTv.text = endTime
                        }
                    }

                    root.setOnClickListener {
                        val categoryId = if (mViewModel.categoryId.isEmpty()) "all" else mViewModel.categoryId
                        NewLogUtils.logForumActivityClick(
                            categoryId,
                            activityEntity.id,
                            position,
                            activityEntity.link?.type ?: "",
                            activityEntity.link?.link ?: ""
                        )
                        if (status == "未开始") ToastUtils.toast("活动还未开始~")
                        if (status == "正在评奖") ToastUtils.toast("活动正在评奖中，请耐心等待评奖结果~")
                        if (status == "奖励发放中") ToastUtils.toast("活动奖励发放中~")
                        if (status == "已结束") ToastUtils.toast("活动已结束~")
                        if (activityEntity.link == null) {
                            DirectUtils.directToActivityDetail(
                                mContext,
                                activityEntity.id,
                                mViewModel.categoryId,
                                mEntrance
                            )
                        } else {
                            DirectUtils.directToLinkPage(
                                mContext,
                                activityEntity.link,
                                mEntrance,
                                "",
                                "社区-活动Tab"
                            )
                        }
                    }
                }
            }

            is FooterViewHolder -> {
                holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
            }
        }
    }

    inner class ForumActivityViewHolder(val binding: ForumActivityItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}