package com.gh.gamecenter.wrapper

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.gh.common.util.ViewPagerFragmentHelper
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.tracker.IBusiness
import com.gh.gamecenter.home.custom.CustomPageFragment
import com.lightgame.listeners.OnBackPressedListener

/**
 * 带常规标题栏Toolbar的外层Activity，支持下拉推送
 * 目前用于二级 自定义页面/多Tab导航页
 */
class ToolbarWrapperActivity : BaseActivity() {

    private var mContentFragment: Fragment? = null
    override fun getLayoutId(): Int = R.layout.activity_amway

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bundle = Bundle()
        bundle.putAll(intent.extras)
        if (!intent?.getStringExtra(EntranceConsts.KEY_MULTI_TAB_NAV_ID).isNullOrEmpty()) {
            bundle.putString(ViewPagerFragmentHelper.WRAPPER_FRAGMENT_NAME, TabWrapperFragment::class.java.name)
        } else if (!intent?.getStringExtra(EntranceConsts.KEY_CUSTOM_PAGE_ID).isNullOrEmpty()) {
            bundle.putString(ViewPagerFragmentHelper.WRAPPER_FRAGMENT_NAME, CustomPageFragment::class.java.name)
        }
        bundle.putBoolean(EntranceConsts.KEY_SHOW_DOWNLOAD_MENU, true)
        mContentFragment = supportFragmentManager.findFragmentByTag(ToolbarWrapperFragment::class.java.name)
            ?: ToolbarWrapperFragment().with(bundle)
        // 若 placeholder 外层为 RelativeLayout 的话，会出现莫名的偏移
        supportFragmentManager.beginTransaction()
            .replace(R.id.placeholder, mContentFragment!!, ToolbarWrapperFragment::class.java.name).commitAllowingStateLoss()
    }

    override fun handleBackPressed(): Boolean =
        (mContentFragment as? OnBackPressedListener)?.onHandleBackPressed() ?: false

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

    override fun getBusinessId(): Pair<String, String> = (mContentFragment as? IBusiness)?.getBusinessId() ?: Pair("", "")

    companion object {
        fun getMultiTabNavIntent(context: Context, multiTabNavId: String, multiTabNavName: String, entrance: String): Intent {
            val intent = Intent(context, ToolbarWrapperActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_MULTI_TAB_NAV_ID, multiTabNavId)
            intent.putExtra(EntranceConsts.KEY_MULTI_TAB_NAV_NAME, multiTabNavName)
            intent.putExtra(EntranceConsts.KEY_NAVIGATION_TITLE, multiTabNavName)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, entrance)
            return intent
        }

        fun getCustomPageIntent(context: Context, pageId: String, pageName: String, entrance: String): Intent {
            val intent = Intent(context, ToolbarWrapperActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_CUSTOM_PAGE_ID, pageId)
            intent.putExtra(EntranceConsts.KEY_CUSTOM_PAGE_NAME, pageName)
            intent.putExtra(EntranceConsts.KEY_NAVIGATION_TITLE, pageName)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, entrance)
            return intent
        }
    }

}