package com.gh.gamecenter.forum.detail

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
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.utils.updateStatusBarColor

class ForumDetailActivity : BaseActivity() {

    private var mContainerFragment: Fragment? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_amway
    }

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

//        DisplayUtils.transparentStatusBar(this)
        updateStatusBarColor(R.color.background_white, R.color.background_white)

        mContainerFragment = supportFragmentManager.findFragmentByTag(ForumDetailFragment::class.java.name)
            ?: ForumDetailFragment().with(intent.extras)
        // 若 placeholder 外层为 RelativeLayout 的话，会出现莫名的偏移
        supportFragmentManager.beginTransaction()
            .replace(R.id.placeholder, mContainerFragment!!, ForumDetailFragment::class.java.name)
            .commitAllowingStateLoss()
    }

    override fun getBusinessId(): Pair<String, String> {
        return Pair(intent.getStringExtra(EntranceConsts.KEY_BBS_ID) ?: "", "")
    }

    override fun preventRecreateFragmentByFragmentManager(): Boolean = true

    override fun isAutoResetViewBackgroundEnabled() = true

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

    companion object {
        @JvmStatic
        fun getIntent(context: Context, bbsId: String, entrance: String): Intent {
            val intent = Intent(context, ForumDetailActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_BBS_ID, bbsId)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, entrance)
            return intent
        }

        @JvmStatic
        fun getTrendsIntent(context: Context, bbsId: String, entrance: String): Intent {
            val intent = Intent(context, ForumDetailActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_BBS_ID, bbsId)
            intent.putExtra(EntranceConsts.KEY_SCROLL_TO_TRENDS, true)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, entrance)
            return intent
        }
    }

    override fun handleBackPressed(): Boolean {
        (mContainerFragment as? ForumDetailFragment)?.run {
            return onBackPressed()
        }
        return super.handleBackPressed()
    }
}