package com.gh.gamecenter.qa.comment

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.gh.gamecenter.core.AppExecutor
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.databinding.FragmentAnswerCommentConversationBinding
import com.gh.gamecenter.entity.CommentEntity
import com.gh.gamecenter.common.entity.LinkEntity

// 评论详情-查看对话
class NewCommentConversationFragment : NewCommentFragment() {

    override var commentContainer: View? = null
    private var mLinkEntity: LinkEntity? = null
    private lateinit var mBinding: FragmentAnswerCommentConversationBinding

    override fun provideListAdapter(): ListAdapter<*> {
        if (mAdapter == null) {
            mEntrance = if (!TextUtils.isEmpty(mAnswerId)) {
                "(答案-评论详情-查看对话)"
            } else if (!TextUtils.isEmpty(mVideoId)) {
                "(视频-评论详情-查看对话)"
            } else {
                "(文章-评论详情-查看对话)"
            }
            mAdapter = NewCommentAdapter(requireContext(), mViewModel, false, this, this, mEntrance)
        }
        return mAdapter!!
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        mCommentId = arguments?.getString(EntranceConsts.KEY_COMMENTID) ?: ""
        mAnswerId = arguments?.getString(EntranceConsts.KEY_ANSWER_ID) ?: ""

        mArticleId = arguments?.getString(CommentActivity.ARTICLE_ID) ?: ""
        mCommunityId = arguments?.getString(CommentActivity.COMMUNITY_ID) ?: ""

        mVideoId = arguments?.getString(CommentActivity.VIDEO_ID) ?: ""
        mIsVideoAuthor = arguments?.getBoolean(CommentActivity.IS_VIDEO_AUTHOR, false) ?: false

        mCommentType = when {
            mAnswerId.isNotEmpty() -> CommentType.ANSWER_CONVERSATION
            mArticleId.isNotEmpty() -> CommentType.COMMUNITY_ARTICLE_CONVERSATION
            else -> CommentType.VIDEO_CONVERSATION
        }

        mLinkEntity = arguments?.getParcelable(EntranceConsts.KEY_LINK)
        super.onCreate(savedInstanceState)
        mBinding = FragmentAnswerCommentConversationBinding.bind(mCachedView)
        commentContainer = mBinding.pieceCommentTypingContainer.answerCommentContentContainer
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setNavigationTitle("查看对话")
        AppExecutor.uiExecutor.executeWithDelay({ commentContainer?.visibility = View.GONE }, 150)

        mListRv.setOnTouchListener { _, _ ->
            if (commentContainer?.visibility == View.VISIBLE) {
                commentContainer?.visibility = View.GONE
            }
            false
        }

        mLinkEntity?.let {
            mBinding.linkTv.visibility = View.VISIBLE
            mBinding.linkTv.text = (if ("answer" == it.type) "回答：" else "帖子：") + it.title
            mBinding.linkTv.setOnClickListener { _ ->
                DirectUtils.directToLinkPage(
                    requireContext(),
                    it,
                    mEntrance,
                    "查看对话"
                )
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_answer_comment_conversation
    }

    override fun onCommentCallback(entity: CommentEntity) {
        commentContainer?.visibility = View.VISIBLE
        mCommentEntity = entity
        setSoftInput(true)
    }
}