package com.gh.gamecenter.qa.comment

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.constant.EntranceConsts.KEY_COMMENT_ID
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.doOnEnd
import com.gh.gamecenter.databinding.ActivityCommentBinding
import com.gh.gamecenter.entity.CommentEntity
import com.gh.gamecenter.gamecollection.detail.conversation.GameCollectionCommentConversationFragment
import com.gh.gamecenter.qa.answer.detail.AnswerDetailFragment
import com.gh.gamecenter.qa.comment.conversation.CommentConversationFragment
import com.lightgame.utils.Util_System_Keyboard

class CommentActivity : BaseActivity() {

    private val resultIntent = Intent()
    private var mShowInputOnly = false
    private lateinit var mBinding: ActivityCommentBinding

    override fun getLayoutId() = R.layout.activity_comment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        mBinding = ActivityCommentBinding.bind(mContentView)
        DisplayUtils.transparentStatusBar(this)
        val commentCount = intent.getIntExtra(COMMENT_COUNT, 0)
        val showKeyboard = intent.getBooleanExtra(SHOW_KEYBOARD, false)
        val isCommentConversation = intent.getBooleanExtra(EntranceConsts.KEY_IS_COMMENT_CONVERSATION, false)

        val answerId = intent.getStringExtra(ANSWER_ID) ?: ""
        val articleId = intent.getStringExtra(ARTICLE_ID) ?: ""
        val communityId = intent.getStringExtra(COMMUNITY_ID) ?: ""
        val videoId = intent.getStringExtra(VIDEO_ID) ?: ""
        val questionId = intent.getStringExtra(QUESTION_ID) ?: ""
        val commentId = intent.getStringExtra(KEY_COMMENT_ID) ?: ""
        val gameCollectionId = intent.getStringExtra(GAME_COLLECTION_ID) ?: ""

        val isVideoAuthor = intent.getBooleanExtra(IS_VIDEO_AUTHOR, false)
        val isStairsComment = intent.getBooleanExtra(IS_STAIRS_COMMENT, false)
        val commentEntity: CommentEntity? = intent.getParcelableExtra(COMMENT_ENTITY)
        val useReplyApi: Boolean = intent.getBooleanExtra(USE_REPLY_API, false)
        mShowInputOnly = intent.getBooleanExtra(SHOW_INPUT_ONLY, false)

        val commentCallback = object : AnswerDetailFragment.CommentListener {
            override fun onCountChange(count: Int) {
                resultIntent.putExtra(COMMENT_COUNT, count)
                setResult(Activity.RESULT_OK, resultIntent)
            }

            override fun onCommentDraftChange(draft: String) {
                resultIntent.putExtra(COMMENT_DRAFT, draft)
                setResult(Activity.RESULT_OK, resultIntent)
            }

            override fun onCommentSuccess(commentId: String) {
                resultIntent.putExtra(KEY_COMMENT_ID, commentId)
                setResult(Activity.RESULT_OK, resultIntent)
            }
        }
        var commentFragment = supportFragmentManager.findFragmentByTag(NewCommentFragment::class.java.name)
        if (commentFragment != null) {
            if (commentFragment is NewCommentFragment) {
                commentFragment.setCommentListener(commentCallback)
            }
        } else {
            commentFragment = when {
                isCommentConversation && gameCollectionId.isNotEmpty() && commentId.isNotEmpty() -> {
                    GameCollectionCommentConversationFragment().with(intent.extras)
                }
                isCommentConversation && commentId.isNotEmpty() -> {
                    CommentConversationFragment().with(intent.extras)
                }
                answerId.isNotEmpty() -> {
                    NewCommentFragment.getAnswerCommentInstance(
                        answerId,
                        showKeyboard,
                        commentCount,
                        mShowInputOnly,
                        commentCallback
                    )
                }
                articleId.isNotEmpty() -> {
                    NewCommentFragment.getCommunityArticleCommentInstance(
                        articleId,
                        communityId,
                        showKeyboard,
                        commentCount,
                        mShowInputOnly,
                        useReplyApi,
                        commentEntity,
                        commentCallback
                    )
                }
                questionId.isNotEmpty() -> {
                    NewCommentFragment.getCommunityQuestionCommentInstance(
                        questionId,
                        communityId,
                        showKeyboard,
                        commentCount,
                        mShowInputOnly,
                        useReplyApi,
                        commentEntity,
                        commentCallback
                    )
                }
                gameCollectionId.isNotEmpty() -> {
                    NewCommentFragment.getGameCollectionCommentInstance(
                        gameCollectionId,
                        commentId,
                        showKeyboard,
                        commentCount,
                        mShowInputOnly,
                        useReplyApi,
                        commentEntity,
                        commentCallback
                    )
                }
                else -> {
                    NewCommentFragment.getVideoCommentInstance(
                        videoId,
                        showKeyboard,
                        commentCount,
                        isVideoAuthor,
                        commentEntity,
                        mShowInputOnly,
                        isStairsComment,
                        useReplyApi,
                        commentCallback
                    )
                }
            }
        }

        supportFragmentManager.beginTransaction()
            .replace(R.id.answerCommentPlaceholderView, commentFragment!!, NewCommentFragment::class.java.name)
            .commitNowAllowingStateLoss()

        mBinding.maskView.alpha = 0f
        if (!isStairsComment) {
            mBinding.maskView.animate().alpha(1f).setDuration(300).start()
        }
        mBinding.answerCommentPlaceholderView.translationY = (DisplayUtils.getScreenHeight()).toFloat()
        mBinding.answerCommentPlaceholderView.animate().translationY(0f).setDuration(300).start()
        mBinding.maskView.setOnClickListener { finish() }
    }

    override fun finish() {
        Util_System_Keyboard.hideSoftKeyboard(this)
        overridePendingTransition(0, 0)
        mBinding.maskView.animate().alpha(0f).setDuration(300).start()
        mBinding.answerCommentPlaceholderView.animate()
            .translationY(DisplayUtils.getScreenHeight().toFloat())
            .setDuration(300)
            .doOnEnd {
                super.finish()
            }
            .start()
    }

    companion object {
        const val ANSWER_ID = "answer_id"
        const val COMMENT_COUNT = "comment_count"
        const val COMMENT_DRAFT = "comment_draft"
        const val COMMENT_ENTITY = "comment_entity"
        const val IS_VIDEO_AUTHOR = "isVideoAuthor"
        const val SHOW_KEYBOARD = "show_keyboard"
        const val IS_STAIRS_COMMENT = "is_stairs_comment"

        const val ARTICLE_ID = "article_id"
        const val QUESTION_ID = "question_id"
        const val COMMUNITY_ID = "community_id"

        const val SHOW_INPUT_ONLY = "show_input_only"

        const val USE_REPLY_API = "use_reply_api"

        const val VIDEO_ID = "video_id"

        const val GAME_COLLECTION_ID = "game_collection_id"

        const val REQUEST_CODE = 8123

        @JvmStatic
        fun getAnswerCommentIntent(
            context: Context,
            answerId: String,
            commentCount: Int? = 0,
            showKeyboard: Boolean
        ): Intent {
            return getAnswerCommentIntent(context, answerId, commentCount, showKeyboard, false)
        }

        @JvmStatic
        fun getAnswerCommentIntent(
            context: Context,
            answerId: String,
            commentCount: Int? = 0,
            showKeyboard: Boolean,
            showInputOnly: Boolean = false
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(ANSWER_ID, answerId)
            intent.putExtra(COMMENT_COUNT, commentCount)
            intent.putExtra(SHOW_KEYBOARD, showKeyboard)
            intent.putExtra(SHOW_INPUT_ONLY, showInputOnly)
            if (context is Activity) {
                context.overridePendingTransition(0, 0)
            }
            return intent
        }

        /**
         * 评论视频
         */
        @JvmStatic
        fun getVideoCommentIntent(
            context: Context, videoId: String, commentCount: Int? = 0, isVideoAuthor: Boolean,
            showKeyboard: Boolean, showInputOnly: Boolean = false, isStairsComment: Boolean = true
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(VIDEO_ID, videoId)
            intent.putExtra(COMMENT_COUNT, commentCount)
            intent.putExtra(IS_VIDEO_AUTHOR, isVideoAuthor)
            intent.putExtra(SHOW_KEYBOARD, showKeyboard)
            intent.putExtra(SHOW_INPUT_ONLY, showInputOnly)
            intent.putExtra(IS_STAIRS_COMMENT, isStairsComment)
            if (context is Activity) {
                context.overridePendingTransition(0, 0)
            }
            return intent
        }

        /**
         * 回复视频评论
         */
        fun getVideoCommentReplyIntent(
            context: Context, videoId: String, commentCount: Int? = 0, isVideoAuthor: Boolean,
            showKeyboard: Boolean, showInputOnly: Boolean = false, commentEntity: CommentEntity? = null
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(VIDEO_ID, videoId)
            intent.putExtra(COMMENT_COUNT, commentCount)
            intent.putExtra(IS_VIDEO_AUTHOR, isVideoAuthor)
            intent.putExtra(SHOW_KEYBOARD, showKeyboard)
            intent.putExtra(SHOW_INPUT_ONLY, showInputOnly)
            intent.putExtra(IS_STAIRS_COMMENT, false)
            intent.putExtra(COMMENT_ENTITY, commentEntity)
            intent.putExtra(USE_REPLY_API, true)
            if (context is Activity) {
                context.overridePendingTransition(0, 0)
            }
            return intent
        }

        /**
         * 回复帖子、视频、问题评论
         */
        @JvmStatic
        fun getCommentDetailIntent(
            context: Context,
            commentId: String,
            communityId: String,
            articleId: String,
            videoId: String,
            questionId: String,
            showKeyboard: Boolean = false,
            position: Int = -1,
            entrance: String,
            path: String
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, ToolBarActivity.mergeEntranceAndPath(entrance, path))
            intent.putExtra(EntranceConsts.KEY_COMMUNITY_ARTICLE_ID, articleId)
            intent.putExtra(VIDEO_ID, videoId)
            intent.putExtra(QUESTION_ID, questionId)
            intent.putExtra(EntranceConsts.KEY_COMMUNITY_ID, communityId)
            intent.putExtra(EntranceConsts.KEY_POSITION, position)
            intent.putExtra(KEY_COMMENT_ID, commentId)
            intent.putExtra(EntranceConsts.KEY_SHOW_KEYBOARD_IF_NEEDED, showKeyboard)
            intent.putExtra(EntranceConsts.KEY_PATH, path)
            intent.putExtra(EntranceConsts.KEY_IS_COMMENT_CONVERSATION, true)
            return intent
        }

        /**
         * 评论帖子
         */
        @JvmStatic
        fun getArticleCommentIntent(
            context: Context,
            articleId: String,
            commentCount: Int? = 0,
            showKeyboard: Boolean,
            communityId: String,
            commentEntity: CommentEntity? = null,
            showInputOnly: Boolean = false
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(ARTICLE_ID, articleId)
            intent.putExtra(COMMENT_COUNT, commentCount)
            intent.putExtra(SHOW_KEYBOARD, showKeyboard)
            intent.putExtra(COMMUNITY_ID, communityId)
            intent.putExtra(SHOW_INPUT_ONLY, showInputOnly)
            intent.putExtra(COMMENT_ENTITY, commentEntity)
            if (context is Activity) {
                context.overridePendingTransition(0, 0)
            }
            return intent
        }

        // 特殊样式的回复评论
        @JvmStatic
        fun getArticleCommentReplyIntent(
            context: Context,
            articleId: String,
            commentCount: Int? = 0,
            showKeyboard: Boolean,
            communityId: String,
            commentEntity: CommentEntity? = null,
            showInputOnly: Boolean = false
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(ARTICLE_ID, articleId)
            intent.putExtra(COMMENT_COUNT, commentCount)
            intent.putExtra(SHOW_KEYBOARD, showKeyboard)
            intent.putExtra(COMMUNITY_ID, communityId)
            intent.putExtra(SHOW_INPUT_ONLY, showInputOnly)
            intent.putExtra(COMMENT_ENTITY, commentEntity)
            intent.putExtra(USE_REPLY_API, true)
            if (context is Activity) {
                context.overridePendingTransition(0, 0)
            }
            return intent
        }

        /**
         * 评论问题(回答)
         */
        @JvmStatic
        fun getQuestionCommentIntent(
            context: Context,
            questionId: String,
            commentCount: Int? = 0,
            showKeyboard: Boolean,
            communityId: String,
            showInputOnly: Boolean = false
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(QUESTION_ID, questionId)
            intent.putExtra(COMMENT_COUNT, commentCount)
            intent.putExtra(SHOW_KEYBOARD, showKeyboard)
            intent.putExtra(COMMUNITY_ID, communityId)
            intent.putExtra(SHOW_INPUT_ONLY, showInputOnly)
            if (context is Activity) {
                context.overridePendingTransition(0, 0)
            }
            return intent
        }

        /**
         * 回复问题评论
         */
        fun getQuestionCommentReplyIntent(
            context: Context,
            questionId: String,
            communityId: String,
            commentCount: Int? = 0,
            showKeyboard: Boolean,
            showInputOnly: Boolean = false,
            commentEntity: CommentEntity? = null
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(QUESTION_ID, questionId)
            intent.putExtra(COMMENT_COUNT, commentCount)
            intent.putExtra(SHOW_KEYBOARD, showKeyboard)
            intent.putExtra(COMMUNITY_ID, communityId)
            intent.putExtra(SHOW_INPUT_ONLY, showInputOnly)
            intent.putExtra(COMMENT_ENTITY, commentEntity)
            intent.putExtra(USE_REPLY_API, true)
            if (context is Activity) {
                context.overridePendingTransition(0, 0)
            }
            return intent
        }

        /**
         * 评论游戏单
         */
        @JvmStatic
        fun getGameCollectionCommentIntent(
            context: Context,
            gameCollectionId: String,
            commentCount: Int? = 0
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(GAME_COLLECTION_ID, gameCollectionId)
            intent.putExtra(COMMENT_COUNT, commentCount)
            intent.putExtra(SHOW_KEYBOARD, true)
            intent.putExtra(SHOW_INPUT_ONLY, true)
            if (context is Activity) {
                context.overridePendingTransition(0, 0)
            }
            return intent
        }

        /**
         * 回复游戏单评论
         */
        fun getGameCollectionCommentReplyIntent(
            context: Context,
            gameCollectionId: String,
            commentId: String,
            commentCount: Int? = 0,
            commentEntity: CommentEntity? = null
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(GAME_COLLECTION_ID, gameCollectionId)
            intent.putExtra(KEY_COMMENT_ID, commentId)
            intent.putExtra(COMMENT_COUNT, commentCount)
            intent.putExtra(SHOW_KEYBOARD, true)
            intent.putExtra(SHOW_INPUT_ONLY, true)
            intent.putExtra(COMMENT_ENTITY, commentEntity)
            intent.putExtra(USE_REPLY_API, true)
            if (context is Activity) {
                context.overridePendingTransition(0, 0)
            }
            return intent
        }

        /**
         * 游戏单评论对话
         */
        @JvmStatic
        fun getGameCollectionCommentDetailIntent(
            context: Context,
            commentId: String,
            gameCollectionId: String,
            showKeyboard: Boolean = false,
            entrance: String,
            path: String
        ): Intent {
            val intent = Intent(context, CommentActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, ToolBarActivity.mergeEntranceAndPath(entrance, path))
            intent.putExtra(GAME_COLLECTION_ID, gameCollectionId)
            intent.putExtra(KEY_COMMENT_ID, commentId)
            intent.putExtra(EntranceConsts.KEY_SHOW_KEYBOARD_IF_NEEDED, showKeyboard)
            intent.putExtra(EntranceConsts.KEY_PATH, path)
            intent.putExtra(EntranceConsts.KEY_IS_COMMENT_CONVERSATION, true)
            return intent
        }
    }
}