package com.gh.gamecenter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.view.View
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.base.fragment.BaseFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.amway.AmwaySuccessFragment
import com.halo.assistant.fragment.SwitchInstallMethodFragment
import com.halo.assistant.fragment.user.ManuallyRealNameFragment
import com.halo.assistant.fragment.user.RealNameInfoFragment

/**
 * ShellActivity 用来包裹那些几乎没有交互且运营也不会想要知道页面访问量的静态 fragment (如安利墙的评论提交成功页面)
 */
class ShellActivity : ToolBarActivity() {

    private lateinit var mFragment: BaseFragment<Any>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (savedInstanceState == null) handleIntent(intent.extras)
    }

    private fun handleIntent(bundle: Bundle?) {
        // We parse the bundle to fragment to let fragment get data from bundle itself to survive configuration changes.
        val intentType = Type.fromString(bundle?.getString(INTENT_TYPE) ?: "")

        when (intentType) {
            Type.AMWAY_SUCCESS -> startFragment(AmwaySuccessFragment().with(bundle))
            Type.SWITCH_INSTALL_METHOD -> startFragment(SwitchInstallMethodFragment())
            Type.REAL_NAME_INFO -> startFragment(RealNameInfoFragment().with(bundle))
            Type.MANUALLY_REAL_NAME -> startFragment(ManuallyRealNameFragment().with(bundle?.getBundle(EntranceConsts.KEY_DATA)))
        }
    }

    override fun getLayoutId() = R.layout.activity_shell

    private fun startFragment(fragment: BaseFragment<Any>) {
        mFragment = fragment
        supportFragmentManager.beginTransaction().replace(R.id.placeholder, fragment).commitAllowingStateLoss()
    }

    override fun isAutoResetViewBackgroundEnabled(): Boolean = true

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

    companion object {
        const val INTENT_TYPE = "intent_type"

        @JvmStatic
        @JvmOverloads
        fun getIntent(context: Context, type: Type, extraParcelable: Parcelable? = null): Intent {
            val intent = Intent(context, ShellActivity::class.java)
            intent.putExtra(INTENT_TYPE, type.value)
            intent.putExtra(EntranceConsts.KEY_DATA, extraParcelable)
            return intent
        }
    }

    enum class Type(val value: String) {
        AMWAY_SUCCESS("amway_success"),
        SWITCH_INSTALL_METHOD("switch_install_method"),
        REAL_NAME_INFO("real_name_info"),
        MANUALLY_REAL_NAME("manually_real_name");

        companion object {
            fun fromString(typeString: String): Type {
                return values().find { typeString == it.value } ?: AMWAY_SUCCESS
            }
        }
    }

}