package com.gh.gamecenter.qa.comment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.updateStatusBarColor
import com.gh.gamecenter.gamecollection.detail.conversation.GameCollectionCommentConversationFragment
import com.gh.gamecenter.qa.comment.conversation.CommentConversationFragment

class NewCommentDetailActivity : ToolBarActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        updateStatusBarColor(R.color.background_white, R.color.background_white)
        if (intent?.getBundleExtra(NORMAL_FRAGMENT_BUNDLE)?.getString(CommentActivity.QUESTION_ID)
                ?.isNotBlank() == true
        ) {
            setNavigationTitle("回答详情")
        } else {
            setNavigationTitle("评论详情")
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        updateStatusBarColor(R.color.background_white, R.color.background_white)
    }

    override fun getBusinessId(): Pair<String, String> {
        return if (targetFragment.arguments != null) {
            Pair(targetFragment.requireArguments().getString(EntranceConsts.KEY_COMMENT_ID) ?: "", "")
        } else {
            super.getBusinessId()
        }
    }

    companion object {

        @JvmStatic
        fun getArticleCommentIntent(
            context: Context,
            commentId: String,
            communityId: String,
            articleId: String,
            topCommentId: String,
            entrance: String,
            path: String
        ): Intent {
            val bundle = getCommonBundle(commentId, topCommentId, entrance, path).apply {
                putString(EntranceConsts.KEY_COMMUNITY_ID, communityId)
                putString(EntranceConsts.KEY_COMMUNITY_ARTICLE_ID, articleId)
            }
            return getTargetIntent(
                context,
                NewCommentDetailActivity::class.java,
                CommentConversationFragment::class.java,
                bundle
            )
        }

        @JvmStatic
        fun getAnswerCommentIntent(
            context: Context,
            commentId: String,
            questionId: String,
            topCommentId: String,
            entrance: String,
            path: String
        ): Intent {
            val bundle = getCommonBundle(commentId, topCommentId, entrance, path).apply {
                putString(CommentActivity.QUESTION_ID, questionId)
            }
            return getTargetIntent(
                context,
                NewCommentDetailActivity::class.java,
                CommentConversationFragment::class.java,
                bundle
            )
        }

        @JvmStatic
        fun getVideoCommentIntent(
            context: Context,
            commentId: String,
            videoId: String,
            topCommentId: String,
            entrance: String,
            path: String
        ): Intent {
            val bundle = getCommonBundle(commentId, topCommentId, entrance, path).apply {
                putString(CommentActivity.VIDEO_ID, videoId)
            }
            return getTargetIntent(
                context,
                NewCommentDetailActivity::class.java,
                CommentConversationFragment::class.java,
                bundle
            )
        }

        @JvmStatic
        fun getGameCollectionCommentIntent(
            context: Context,
            commentId: String,
            gameCollectionId: String,
            topCommentId: String,
            entrance: String,
            path: String
        ): Intent {
            val bundle = getCommonBundle(commentId, topCommentId, entrance, path).apply {
                putString(CommentActivity.GAME_COLLECTION_ID, gameCollectionId)
            }
            return getTargetIntent(
                context,
                NewCommentDetailActivity::class.java,
                GameCollectionCommentConversationFragment::class.java,
                bundle
            )
        }

        private fun getCommonBundle(
            commentId: String,
            topCommentId: String,
            entrance: String,
            path: String
        ) = Bundle().apply {
            putString(EntranceConsts.KEY_ENTRANCE, mergeEntranceAndPath(entrance, path))
            putInt(EntranceConsts.KEY_POSITION, 1)
            putString(EntranceConsts.KEY_COMMENT_ID, commentId)
            putString(EntranceConsts.KEY_TOP_COMMENT_ID, topCommentId)
            putString(EntranceConsts.KEY_PATH, path)
        }
    }
}