package com.gh.gamecenter.video.videomanager

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.LogUtils
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.view.BugFixedPopupWindow
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.EmptyCallback
import com.gh.gamecenter.databinding.CommunityVideoDraftItemBinding
import com.gh.gamecenter.entity.VideoDraftEntity

class VideoDraftAdapter(
    context: Context,
    private val mViewModel: VideoDraftViewModel,
    private val mEntrance: String,
    private val selectCallback: (VideoDraftEntity) -> Unit
) : ListAdapter<VideoDraftEntity>(context) {

    private val mPath = "草稿箱"

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

    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 {
        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_video_draft_item, parent, false)
                return VideoDraftViewHolder(CommunityVideoDraftItemBinding.bind(view))
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is VideoDraftViewHolder) {
            val draftEntity = mEntityList[position]
            holder.binding.articleDraftTitle.text = draftEntity.title.ifEmpty { "（缺少标题）" }
            if (draftEntity.bbsId.isNotEmpty() && draftEntity.game != null) {
                holder.binding.articleDraftCommunityName.apply {
                    text = draftEntity.game?.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.itemView.setOnClickListener {
                PermissionHelper.checkStoragePermissionBeforeAction(mContext,
                    object : EmptyCallback {
                        override fun onCallback() {
                            selectCallback.invoke(draftEntity)
                        }
                    })
            }
            holder.binding.articleDraftDelete.setOnClickListener {
                DialogHelper.showDialog(
                    mContext, "删除草稿",
                    "确定要删除视频草稿吗？删除之后不可恢复",
                    "确定删除", "暂时不了",
                    {
                        mViewModel.deleteDraft(draftEntity.id)
                        LogUtils.logVideoStreamingUpload("点击删除按钮", mPath, "", "")
                    }, extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
                )
            }
        } else if (holder is FooterViewHolder) {
            holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
        }
    }

    private fun showMorePopWindow(view: View, entity: VideoDraftEntity) {
        val inflater = LayoutInflater.from(view.context)
        val layout = inflater.inflate(R.layout.layout_popup_container, null)
        val popupWindow = BugFixedPopupWindow(
            layout,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        val container = layout.findViewById<LinearLayout>(R.id.container)
        val item = inflater.inflate(R.layout.layout_popup_option_item, container, false)
        container.addView(item)

        val hitText = item.findViewById<TextView>(R.id.hint_text)
        hitText.text = "删除"

        item.setOnClickListener {
            DialogHelper.showDialog(
                mContext, "删除草稿",
                "确定要删除视频草稿吗？删除之后不可恢复",
                "确定删除", "暂时不了",
                {
                    mViewModel.deleteDraft(entity.id)
                    LogUtils.logVideoStreamingUpload("点击删除按钮", mPath, "", "")
                }, extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
            )
            popupWindow.dismiss()
        }

        popupWindow.isTouchable = true
        popupWindow.isFocusable = true
        popupWindow.showAutoOrientation(view)
    }

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

class VideoDraftViewHolder(val binding: CommunityVideoDraftItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)