package com.gh.gamecenter.qa.editor

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.collection.AnswerFragment
import com.gh.gamecenter.databinding.AskAnswerItemBinding
import com.gh.gamecenter.qa.entity.AnswerEntity

class AnswerAdapter(context: Context, private val mEntrance: String?) : ListAdapter<AnswerEntity>(context) {

    override fun setListData(updateData: MutableList<AnswerEntity>?) {
        if (updateData != null) {
            for (data in updateData) {
                if (!data.active) updateData.remove(data)
            }
        }
        super.setListData(updateData)
    }

    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)
            }
            ItemViewType.ITEM_BODY -> {
                view = mLayoutInflater.inflate(R.layout.ask_answer_item, parent, false)
                AnswerViewHolder(AskAnswerItemBinding.bind(view))
            }
            else -> throw NullPointerException()
        }
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            ItemViewType.ITEM_BODY -> {
                val answerViewHolder = holder as AnswerViewHolder
                val entity = mEntityList[position]
                answerViewHolder.initCollectionAnswerViewHolder(
                    mContext,
                    entity,
                    mEntrance,
                    AnswerFragment.COLLECTION_ANSWER
                )
                answerViewHolder.binding.askAnswerItemUsericon.setOnClickListener {
                    selectAndBack(entity)
                }
                answerViewHolder.binding.askAnswerItemUsername.setOnClickListener {
                    selectAndBack(entity)
                }
                answerViewHolder.itemView.setOnClickListener {
                    selectAndBack(entity)
                }
            }
            ItemViewType.ITEM_FOOTER -> {
                val footerViewHolder = holder as FooterViewHolder
                footerViewHolder.initItemPadding()
                footerViewHolder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
            }
        }
    }

    private fun selectAndBack(entity: AnswerEntity) {
        if (mContext is Activity) {
            val intent = Intent()
            intent.putExtra(AnswerEntity::class.java.simpleName, entity)
            (mContext as Activity).setResult(Activity.RESULT_OK, intent)
            (mContext as Activity).finish()
        }
    }

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