package com.gh.gamecenter.feedback.view.suggest

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.core.os.bundleOf
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.entity.SimpleGameEntity
import com.gh.gamecenter.common.entity.SuggestType
import com.gh.gamecenter.common.utils.updateStatusBarColor
import com.gh.gamecenter.feature.utils.PlatformUtils
import com.gh.gamecenter.feedback.R

/**
 * 意见反馈
 */
class SuggestionActivity : ToolBarActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        initParams()
        super.onCreate(savedInstanceState)
    }

    private fun initParams() {
        val uri = intent?.data
        if (uri != null) {
            val suggestionType = uri.getQueryParameter(EntranceConsts.KEY_TYPE)
            val gameId = uri.getQueryParameter(EntranceConsts.KEY_GAMEID)
            val packageMd5 = uri.getQueryParameter(EntranceConsts.KEY_PACKAGE_MD5)
            val platform = uri.getQueryParameter(EntranceConsts.KEY_PLATFORM)
            val platformName = PlatformUtils.getInstance(this).getPlatformName(platform)
            val content = if (TextUtils.isEmpty(gameId) || TextUtils.isEmpty(packageMd5)) String.format(
                "%s—V%s—%s（V%s），",
                uri.getQueryParameter(EntranceConsts.KEY_GAME_NAME),
                uri.getQueryParameter(EntranceConsts.KEY_VERSION),
                if (TextUtils.isEmpty(platformName)) platform else platformName,
                uri.getQueryParameter(EntranceConsts.KEY_PLATFORM_VERSION)
            ) else String.format(
                "%s-%s-V%s\n游戏ID：%s\n游戏包MD5：%s\n",
                uri.getQueryParameter(EntranceConsts.KEY_GAME_NAME),
                if (TextUtils.isEmpty(platformName)) platform else platformName,
                uri.getQueryParameter(EntranceConsts.KEY_VERSION), gameId, packageMd5
            )
            val gameName = uri.getQueryParameter(EntranceConsts.KEY_GAME_NAME)
            val packageName = uri.getQueryParameter(EntranceConsts.KEY_GAME_PKG)

            // 畅玩助手反馈跳转到【意见反馈-游戏问题】
            if (suggestionType == "vgame") {
                intent.putExtra(NORMAL_FRAGMENT_NAME, SuggestGameFragment::class.java.canonicalName)
                intent.putExtra(
                    NORMAL_FRAGMENT_BUNDLE, bundleOf(
                        EntranceConsts.KEY_CONTENT to content,
                        EntranceConsts.KEY_SMOOTH_GAME to true,
                        EntranceConsts.KEY_HIDE_SUGGEST_HINT to "【畅玩问题】",
                        SimpleGameEntity::class.java.simpleName to SimpleGameEntity(
                            name = gameName ?: "",
                            packageName = packageName ?: ""
                        )
                    )
                )
            }
        }
    }

    override fun provideNormalIntent(): Intent {
        return getTargetIntent(
            this,
            SuggestionActivity::class.java,
            SuggestionCategoryFragment::class.java,
            intent?.extras
        )
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        updateStatusBarColor(com.gh.gamecenter.common.R.color.ui_surface, com.gh.gamecenter.common.R.color.ui_surface)
    }

    override fun updateStaticViewBackground(view: View?) {
        updateStaticView(view, listOf(R.id.flexbox, R.id.suggestIdentityRg))
    }

    companion object {
        fun startSuggestionActivity(
            context: Context?,
            suggestType: SuggestType?,
            hiddenHint: String? = "",
            suggestHintType: String? = "",
            content: String? = "",
            game: SimpleGameEntity? = null,
            isQaFeedback: Boolean = false,
            qaContentId: String? = "",
            fromRatingKey: Boolean = false,
            diagnosis: String = "",
            forceLogin: Boolean = true
        ) {
            context?.startActivity(
                getIntent(
                    context,
                    suggestType,
                    hiddenHint,
                    suggestHintType,
                    content,
                    game,
                    isQaFeedback,
                    qaContentId,
                    fromRatingKey,
                    diagnosis,
                    forceLogin
                )
            )
        }

        fun getIntent(
            context: Context?,
            suggestType: SuggestType?,
            hiddenHint: String? = "",
            suggestHintType: String? = "",
            content: String? = "",
            game: SimpleGameEntity? = null,
            isQaFeedback: Boolean = false,
            qaContentId: String? = "",
            fromRatingKey: Boolean = false,
            diagnosis: String = "",
            forceLogin: Boolean = true,
            isSmoothGame: Boolean = false
        ): Intent? {
            if (suggestType != null) {
                return when (suggestType) {
                    SuggestType.APP -> {
                        getSuggestAppIntent(
                            context,
                            forceLogin,
                            content,
                            hiddenHint,
                            diagnosis,
                            isQaFeedback,
                            qaContentId
                        )
                    }

                    SuggestType.GAME -> {
                        getSuggestGameIntent(
                            context,
                            content,
                            suggestHintType,
                            hiddenHint,
                            isPlugin = false,
                            isSmoothGame = isSmoothGame,
                            isQaFeedback,
                            qaContentId,
                            fromRatingKey,
                            game
                        )
                    }

                    SuggestType.UPDATE -> {
                        getSuggestUpdateIntent(
                            context,
                            game?.name,
                            content,
                            hiddenHint,
                            isPlugin = false,
                            isSmoothGame = isSmoothGame,
                            isQaFeedback,
                            qaContentId
                        )
                    }

                    SuggestType.FUNCTION -> {
                        getSuggestFunctionIntent(context, content, hiddenHint, isQaFeedback, qaContentId)
                    }

                    SuggestType.COPYRIGHT -> {
                        getSuggestCopyrightIntent(context, hiddenHint, isQaFeedback, qaContentId)
                    }

                    else -> null
                }
            } else {
                return null
            }
        }

        fun getSuggestionCategoryIntent(context: Context, bundle: Bundle? = null): Intent {
            return getTargetIntent(
                context,
                SuggestionActivity::class.java,
                SuggestionCategoryFragment::class.java,
                bundle
            )
        }

        fun getSuggestAppIntent(
            context: Context?,
            forceLogin: Boolean = true,
            content: String? = "",
            hiddenHint: String? = "",
            diagnosisResult: String? = "",
            isQaFeedback: Boolean = false,
            qaContentId: String? = ""
        ): Intent {
            return getTargetIntent(
                context,
                SuggestionActivity::class.java,
                SuggestAppFragment::class.java,
                bundleOf(
                    EntranceConsts.KEY_FORCE_LOGIN to forceLogin,
                    EntranceConsts.KEY_CONTENT to content,
                    EntranceConsts.KEY_HIDE_SUGGEST_HINT to hiddenHint,
                    EntranceConsts.KEY_DIAGNOSIS to diagnosisResult,
                    EntranceConsts.KEY_IS_QA_FEEDBACK to isQaFeedback,
                    EntranceConsts.KEY_QA_CONTENT_ID to qaContentId
                )
            )
        }

        fun getSuggestGameIntent(
            context: Context?,
            content: String? = "",
            suggestHintType: String? = "",
            hiddenHint: String? = "",
            isPlugin: Boolean = false,
            isSmoothGame: Boolean = false,
            isQaFeedback: Boolean = false,
            qaContentId: String? = "",
            fromRatingKey: Boolean = false,
            game: SimpleGameEntity? = null
        ): Intent {
            return getTargetIntent(
                context,
                SuggestionActivity::class.java,
                SuggestGameFragment::class.java,
                bundleOf(
                    EntranceConsts.KEY_CONTENT to content,
                    EntranceConsts.KEY_SUGGEST_HINT_TYPE to suggestHintType,
                    EntranceConsts.KEY_HIDE_SUGGEST_HINT to hiddenHint,
                    EntranceConsts.KEY_PLUGIN to isPlugin,
                    EntranceConsts.KEY_SMOOTH_GAME to isSmoothGame,
                    EntranceConsts.KEY_IS_QA_FEEDBACK to isQaFeedback,
                    EntranceConsts.KEY_QA_CONTENT_ID to qaContentId,
                    SuggestAppFragment.KEY_FROM_RATING to fromRatingKey,
                    SimpleGameEntity::class.java.simpleName to game
                )
            )
        }

        fun getSuggestUpdateIntent(
            context: Context?,
            seekGameName: String? = "",
            content: String? = "",
            hiddenHint: String? = "",
            isPlugin: Boolean = false,
            isSmoothGame: Boolean = false,
            isQaFeedback: Boolean = false,
            qaContentId: String? = ""
        ): Intent {
            return getTargetIntent(
                context,
                SuggestionActivity::class.java,
                SuggestUpdateFragment::class.java,
                bundleOf(
                    EntranceConsts.KEY_SEEK_GAME to seekGameName,
                    EntranceConsts.KEY_CONTENT to content,
                    EntranceConsts.KEY_HIDE_SUGGEST_HINT to hiddenHint,
                    EntranceConsts.KEY_PLUGIN to isPlugin,
                    EntranceConsts.KEY_SMOOTH_GAME to isSmoothGame,
                    EntranceConsts.KEY_IS_QA_FEEDBACK to isQaFeedback,
                    EntranceConsts.KEY_QA_CONTENT_ID to qaContentId
                )
            )
        }

        fun getSuggestFunctionIntent(
            context: Context?,
            content: String? = "",
            hiddenHint: String? = "",
            isQaFeedback: Boolean = false,
            qaContentId: String? = ""
        ): Intent {
            return getTargetIntent(
                context,
                SuggestionActivity::class.java,
                SuggestFunctionFragment::class.java,
                bundleOf(
                    EntranceConsts.KEY_CONTENT to content,
                    EntranceConsts.KEY_HIDE_SUGGEST_HINT to hiddenHint,
                    EntranceConsts.KEY_IS_QA_FEEDBACK to isQaFeedback,
                    EntranceConsts.KEY_QA_CONTENT_ID to qaContentId
                )
            )
        }

        fun getSuggestCopyrightIntent(
            context: Context?,
            hiddenHint: String? = "",
            isQaFeedback: Boolean = false,
            qaContentId: String? = ""
        ): Intent {
            return getTargetIntent(
                context,
                SuggestionActivity::class.java,
                SuggestCopyrightFragment::class.java,
                bundleOf(
                    EntranceConsts.KEY_HIDE_SUGGEST_HINT to hiddenHint,
                    EntranceConsts.KEY_IS_QA_FEEDBACK to isQaFeedback,
                    EntranceConsts.KEY_QA_CONTENT_ID to qaContentId
                )
            )
        }
    }

}