package com.gh.gamecenter.forum.home.follow

import android.content.Context
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts.GetContent
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.gh.common.util.SyncDataBetweenPageHelper
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.entity.PersonalHistoryEntity
import com.gh.gamecenter.feature.entity.AnswerEntity
import com.gh.gamecenter.feature.entity.ArticleEntity
import com.gh.gamecenter.feature.entity.CommentEntity
import com.gh.gamecenter.feature.entity.ForumVideoEntity
import com.gh.gamecenter.personalhome.home.UserHistoryFragment
import com.gh.gamecenter.qa.article.detail.ArticleDetailActivity
import com.gh.gamecenter.qa.entity.ArticleDetailEntity
import com.gh.gamecenter.qa.entity.QuestionsDetailEntity
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailActivity
import com.gh.gamecenter.qa.video.detail.ForumVideoDetailActivity

class FollowActivityResultLauncher(
    private val registry: ActivityResultRegistry,
    private val onResultListener: OnResultListener
) : DefaultLifecycleObserver {

    private lateinit var articleDetailLauncher: ActivityResultLauncher<LauncherDestination>

    private lateinit var commentEntityLauncher: ActivityResultLauncher<LauncherDestination>

    private lateinit var questionsDetailLauncher: ActivityResultLauncher<LauncherDestination>

    private lateinit var forumVideoLauncher: ActivityResultLauncher<LauncherDestination>

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        articleDetailLauncher = registry.register(
            KEY_ARTICLE_DETAIL_LAUNCHER,
            owner,
            object : ActivityResultContract<LauncherDestination, ArticleDetailResult?>() {
                override fun createIntent(context: Context, input: LauncherDestination): Intent {
                    val intent =
                        if (input.historyEntity != null) {
                            ArticleDetailActivity.getIntent(
                                context,
                                input.historyEntity.community,
                                input.historyEntity.id,
                                "",
                                UserHistoryFragment.PATH_USER_QUESTION_ANSWER,
                                sourceEntrance = "关注-用户动态"
                            )
                        } else {
                            ArticleDetailActivity.getIntent(
                                context,
                                CommunityEntity(input.answerEntity?.bbs?.id ?: ""),
                                input.answerEntity?.id ?: "",
                                "",
                                "",
                                sourceEntrance = "关注-论坛动态",
                            )
                        }
                    intent.putExtra(SyncDataBetweenPageHelper.DATA_POSITION_TAG, input.position)
                    intent.putExtra(SyncDataBetweenPageHelper.REQUEST_CODE_TAG, 101)
                    return intent
                }

                override fun parseResult(resultCode: Int, intent: Intent?): ArticleDetailResult? {
                    val position = intent?.getIntExtra(SyncDataBetweenPageHelper.DATA_POSITION_TAG, -1)
                    val resultData =
                        intent?.getParcelableExtra<ArticleDetailEntity>(ArticleDetailEntity::class.java.simpleName)
                    if (position != null && resultData != null) {
                        return ArticleDetailResult(position, resultData)
                    } else {
                        return null
                    }
                }
            })
        {
            it?.run {
                onResultListener.onArticleDetailResult(position, articleDetailEntity)
            }
        }

        commentEntityLauncher = registry.register(
            KEY_COMMENT_LAUNCHER,
            owner,
            object : ActivityResultContract<LauncherDestination, CommentResult?>() {
                override fun createIntent(context: Context, input: LauncherDestination): Intent {
                    val intent = NewQuestionDetailActivity.getSpecifiedCommentIntent(
                        context,
                        input.historyEntity?.question?.id ?: "",
                        input.historyEntity?.id ?: "",
                        "",
                        UserHistoryFragment.PATH_USER_QUESTION_ANSWER,
                        sourceEntrance = "关注-动态"
                    )
                    intent.putExtra(SyncDataBetweenPageHelper.DATA_POSITION_TAG, input.position)
                    intent.putExtra(SyncDataBetweenPageHelper.REQUEST_CODE_TAG, 102)
                    return intent
                }

                override fun parseResult(resultCode: Int, intent: Intent?): CommentResult? {
                    val position = intent?.getIntExtra(SyncDataBetweenPageHelper.DATA_POSITION_TAG, -1)
                    val commentEntity = intent?.getParcelableExtra<CommentEntity>(CommentEntity::class.java.simpleName)
                    if (position != null && commentEntity != null) {
                        return CommentResult(position, commentEntity)
                    } else {
                        return null
                    }
                }
            })
        {
            it?.run {
                onResultListener.onCommentResult(position, commentEntity)
            }
        }

        questionsDetailLauncher = registry.register(
            KEY_QUESTION_DETAIL_LAUNCHER,
            owner,
            object : ActivityResultContract<LauncherDestination, QuestionsDetailResult?>() {
                override fun createIntent(context: Context, input: LauncherDestination): Intent {
                    val intent = if (input.historyEntity != null) {
                        NewQuestionDetailActivity.getIntent(
                            context,
                            input.historyEntity.id,
                            "",
                            UserHistoryFragment.PATH_USER_QUESTION_ANSWER,
                            sourceEntrance = "关注-论坛动态"
                        )
                    } else {
                        if (input.answerEntity?.type == "question") {
                            NewQuestionDetailActivity.getIntent(
                                context, input.answerEntity.id,
                                "",
                                "",
                                sourceEntrance = "关注-论坛动态"
                            )
                        } else {
                            NewQuestionDetailActivity.getCommentIntent(
                                context,
                                input.answerEntity?.questions?.id ?: "",
                                input.answerEntity?.answerId ?: "",
                                "",
                                "",
                                sourceEntrance = "关注-论坛动态",
                            )
                        }

                    }
                    intent.putExtra(SyncDataBetweenPageHelper.DATA_POSITION_TAG, input.position)
                    intent.putExtra(SyncDataBetweenPageHelper.REQUEST_CODE_TAG, 103)
                    return intent
                }

                override fun parseResult(resultCode: Int, intent: Intent?): QuestionsDetailResult? {
                    val position = intent?.getIntExtra(SyncDataBetweenPageHelper.DATA_POSITION_TAG, -1)
                    val questionsDetailEntity =
                        intent?.getParcelableExtra<QuestionsDetailEntity>(QuestionsDetailEntity::class.java.simpleName)
                    return if (position != null && questionsDetailEntity != null) {
                        QuestionsDetailResult(position, questionsDetailEntity)
                    } else {
                        null
                    }
                }

            }
        ) {
            it?.run {
                onResultListener.onQuestionsDetailResult(position, questionsDetailEntity)
            }
        }

        forumVideoLauncher = registry.register(
            KEY_FORUM_VIDEO_LAUNCHER,
            owner,
            object : ActivityResultContract<LauncherDestination, ForumVideoResult?>() {
                override fun createIntent(context: Context, input: LauncherDestination): Intent {
                    val intent = if (input.historyEntity != null) {
                        ForumVideoDetailActivity.getIntent(
                            context,
                            input.historyEntity.id,
                            input.historyEntity.community.id,
                            sourceEntrance = "关注-个人动态"
                        )
                    } else {
                        ForumVideoDetailActivity.getIntent(
                            context,
                            input.answerEntity?.id ?: "",
                            input.answerEntity?.bbs?.id ?: "",
                            "关注-论坛动态"
                        )
                    }

                    intent.putExtra(SyncDataBetweenPageHelper.DATA_POSITION_TAG, input.position)
                    intent.putExtra(SyncDataBetweenPageHelper.REQUEST_CODE_TAG, 104)
                    return intent
                }

                override fun parseResult(resultCode: Int, intent: Intent?): ForumVideoResult? {
                    val position = intent?.getIntExtra(SyncDataBetweenPageHelper.DATA_POSITION_TAG, -1)
                    val forumVideoEntity =
                        intent?.getParcelableExtra<ForumVideoEntity>(ForumVideoEntity::class.java.simpleName)
                    return if (position != null && forumVideoEntity != null) {
                        ForumVideoResult(position, forumVideoEntity)
                    } else {
                        null
                    }
                }

            }
        )
        {
            it?.run {
                onResultListener.onForumVideoResult(position, forumVideoEntity)
            }
        }
    }

    fun onPersonItemClick(position: Int, entity: PersonalHistoryEntity) {
        when {
            entity.type == "community_article"
                    || entity.type == "community_article_vote" -> {
                articleDetailLauncher.launch(LauncherDestination(position, historyEntity = entity))
            }

            entity.type.contains("video") -> {
                forumVideoLauncher.launch(LauncherDestination(position, historyEntity = entity))
            }

            entity.type.contains("question") -> {
                questionsDetailLauncher.launch(LauncherDestination(position, historyEntity = entity))
            }

            else -> {
                commentEntityLauncher.launch(LauncherDestination(position, historyEntity = entity))
            }
        }
    }

    fun onBbsItemClick(position: Int, answerEntity: AnswerEntity) {
        when (answerEntity.type) {
            "community_article" -> {
                articleDetailLauncher.launch(LauncherDestination(position, answerEntity = answerEntity))
            }

            "video" -> {
                forumVideoLauncher.launch(LauncherDestination(position, answerEntity = answerEntity))
            }

            "question" -> {
                questionsDetailLauncher.launch(LauncherDestination(position, answerEntity = answerEntity))
            }

            "answer" -> {
                questionsDetailLauncher.launch(LauncherDestination(position, answerEntity = answerEntity))
            }
        }
    }

    companion object {

        private const val KEY_ARTICLE_DETAIL_LAUNCHER = "key_article_detail_launcher"
        private const val KEY_COMMENT_LAUNCHER = "key_comment_launcher"
        private const val KEY_QUESTION_DETAIL_LAUNCHER = "key_question_detail_launcher"
        private const val KEY_FORUM_VIDEO_LAUNCHER = "KEY_forum_video_LAUNCHER"
    }

    private data class LauncherDestination(
        val position: Int,
        val historyEntity: PersonalHistoryEntity? = null,
        val answerEntity: AnswerEntity? = null
    )

    private data class ArticleDetailResult(
        val position: Int,
        val articleDetailEntity: ArticleDetailEntity
    )

    private data class CommentResult(
        val position: Int,
        val commentEntity: CommentEntity
    )

    private data class QuestionsDetailResult(
        val position: Int,
        val questionsDetailEntity: QuestionsDetailEntity
    )

    private data class ForumVideoResult(
        val position: Int,
        val forumVideoEntity: ForumVideoEntity
    )

    interface OnResultListener {

        fun onArticleDetailResult(position: Int, articleDetailEntity: ArticleDetailEntity)

        fun onCommentResult(position: Int, commentEntity: CommentEntity)

        fun onQuestionsDetailResult(position: Int, questionsDetailEntity: QuestionsDetailEntity)

        fun onForumVideoResult(position: Int, forumVideoEntity: ForumVideoEntity)
    }
}