package com.gh.gamecenter.gamedetail.fuli.answer

import android.content.Context
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.syncpage.ISyncAdapterHandler
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.core.utils.StringUtils
import com.gh.gamecenter.common.utils.visibleIf
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.CommunityAnswerItemBinding
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.gamedetail.fuli.FuLiViewModel
import com.gh.gamecenter.qa.answer.CommunityAnswerItemViewHolder
import com.gh.gamecenter.qa.answer.detail.SimpleAnswerDetailActivity
import com.gh.gamecenter.qa.article.detail.ArticleDetailActivity
import com.gh.gamecenter.qa.entity.AnswerEntity
import com.gh.gamecenter.qa.entity.Questions
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailActivity
import com.halo.assistant.HaloApp
import com.lightgame.adapter.BaseRecyclerAdapter

class GameDetailAnswerAdapter(
    context: Context,
    var mViewModel: FuLiViewModel,
    private val mAnswerList: List<AnswerEntity>,
    private val mEntrance: String
) : BaseRecyclerAdapter<CommunityAnswerItemViewHolder>(context), ISyncAdapterHandler {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommunityAnswerItemViewHolder {
        val view = mLayoutInflater.inflate(R.layout.community_answer_item, parent, false)
        return CommunityAnswerItemViewHolder(CommunityAnswerItemBinding.bind(view))
    }

    override fun getItemCount(): Int = mAnswerList.size

    override fun onBindViewHolder(holder: CommunityAnswerItemViewHolder, position: Int) {
        holder.binding.contentContainer.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
        holder.commentAndVoteContainer.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
        holder.binding.contentContainer.setPadding(DisplayUtils.dip2px(12f), 0, DisplayUtils.dip2px(12f), 0)

        holder.binding.topLine.visibleIf(position > 0)
        holder.binding.imageContainer.setOffset(72f)
        val entity = mAnswerList[position]
        if ("community_article" == entity.type) {
            val questions = Questions()
            questions.title = entity.articleTitle
            entity.questions = questions
        }
        val path = "游戏详情-动态"
        holder.bindAnswerItem(entity, mEntrance, path)
        holder.binding.title.setOnClickListener {
            HaloApp.put(Constants.GAME_DETAIL_COME_IN, true)
            MtaHelper.onEvent("进入问答", "游戏详情", mViewModel.gameCommunity?.name + "+" + mViewModel.game?.name)
            if ("community_article" == entity.type) {
                MtaHelper.onEvent("游戏详情_新", "点击社区文章", mViewModel.game?.name + "+" + entity.questions.title)
                mContext.startActivity(
                    ArticleDetailActivity.getIntent(
                        mContext,
                        CommunityEntity(entity.articleCommunityId, ""),
                        entity.id!!,
                        mEntrance,
                        path
                    )
                )
            } else {
                MtaHelper.onEvent("游戏详情_新", "点击问题", mViewModel.game?.name + "+" + entity.questions.title)
                mContext.startActivity(
                    NewQuestionDetailActivity.getIntent(
                        mContext,
                        entity.questions.id,
                        mEntrance,
                        path
                    )
                )
            }

        }
        holder.itemView.setOnClickListener {
            HaloApp.put(Constants.GAME_DETAIL_COME_IN, true)
            MtaHelper.onEvent("进入问答", "游戏详情", mViewModel.gameCommunity?.name + "+" + mViewModel.game?.name)
            if ("community_article" == entity.type) {
                MtaHelper.onEvent("游戏详情_新", "点击社区文章", mViewModel.game?.name + "+" + entity.questions.title)
                mContext.startActivity(
                    ArticleDetailActivity.getIntent(
                        mContext,
                        CommunityEntity(entity.articleCommunityId, ""),
                        entity.id!!,
                        mEntrance,
                        path
                    )
                )
            } else {
                MtaHelper.onEvent(
                    "游戏详情_新",
                    "点击回答",
                    mViewModel.game?.name + "+" + StringUtils.shrinkStringWithDot(entity.brief, 10)
                )
                mContext.startActivity(
                    SimpleAnswerDetailActivity.getIntent(
                        mContext, entity.id
                            ?: "", mEntrance, path
                    )
                )
            }
        }
    }

    override fun getSyncData(position: Int): Pair<String, AnswerEntity>? {
        if (position >= mAnswerList.size) return null
        val entity = mAnswerList[position]
        return Pair(entity.id ?: "", entity)
    }
}