package com.gh.gamecenter.qa.article.draft

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.removeDrawable
import com.gh.gamecenter.common.utils.setDrawableStart
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.databinding.CommunityArticleDraftItemBinding
import com.gh.gamecenter.qa.entity.ArticleDraftEntity

class ArticleDraftAdapter(
    context: Context,
    private val deleteCallback: (ArticleDraftEntity) -> Unit,
    private val selectCallback: (ArticleDraftEntity) -> Unit
) : ListAdapter<ArticleDraftEntity>(context) {

    override fun areItemsTheSame(oldItem: ArticleDraftEntity, newItem: ArticleDraftEntity): Boolean {
        return !TextUtils.isEmpty(oldItem.id) && oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: ArticleDraftEntity, newItem: ArticleDraftEntity): Boolean {
        return oldItem == newItem
    }

    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
    ): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        val view: View
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false)
                FooterViewHolder(view)
            }
            else -> {
                view = mLayoutInflater.inflate(R.layout.community_article_draft_item, parent, false)
                ArticleDraftViewHolder(CommunityArticleDraftItemBinding.bind(view))
            }
        }
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (holder is ArticleDraftViewHolder) {
            val entity = mEntityList[position]
            holder.binding.articleDraftTitle.text = entity.title.ifEmpty { "（缺少标题）" }
            if (entity.community.id.isNotEmpty() && entity.community.name.isNotEmpty()) {
                holder.binding.articleDraftCommunityName.apply {
                    text = entity.community.name
                    textSize = 11f
                    setPadding(10f.dip2px(), 6f.dip2px(), 10f.dip2px(), 6f.dip2px())
                    setDrawableStart(R.drawable.ic_forum_label)
                    background = ContextCompat.getDrawable(context, R.drawable.bg_shape_f5_radius_999)
                }
            } else {
                holder.binding.articleDraftCommunityName.apply {
                    text = "未选择论坛"
                    textSize = 12f
                    setPadding(0, 6f.dip2px(), 0, 6f.dip2px())
                    removeDrawable()
                    setBackgroundColor(ContextCompat.getColor(context, R.color.transparent))
                }
            }
            holder.binding.articleDraftDelete.setOnClickListener {
                DialogHelper.showDialog(
                    mContext, "警告", "确定要删除帖子草稿吗？删除之后不可恢复",
                    "确定", "取消", {
                        deleteCallback.invoke(entity)
                    }, extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
                )
            }
            holder.itemView.setOnClickListener {
                selectCallback.invoke(entity)
            }
        } else if (holder is FooterViewHolder) {
            holder.initItemPadding()
            holder.initFooterViewHolder(mIsLoading, isNetworkError, mIsOver)
        }
    }

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

}