package com.gh.gamecenter.qa.video.detail

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.base.fragment.BaseFragment_TabLayout.PAGE_INDEX
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.ToolbarFragment

class ForumVideoDetailActivity : BaseActivity() {
    var containerFragment: Fragment? = null

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

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

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

    override fun onBackPressed() {
        if (containerFragment is ToolbarFragment
            && (containerFragment as ToolbarFragment).isAdded
            && !(containerFragment as ToolbarFragment).onBackPressed()
        ) {
            super.onBackPressed()
        }
    }

    override fun isAutoResetViewBackgroundEnabled(): Boolean = true

    override fun getBusinessId(): Pair<String, String> {
        return if (containerFragment?.arguments != null) {
            Pair(containerFragment?.requireArguments()?.getString(EntranceConsts.KEY_VIDEO_ID) ?: "", "")
        } else {
            super.getBusinessId()
        }
    }

    companion object {
        const val VIDEO_PATCH_REQUEST = 100

        fun getIntent(
            context: Context,
            videoId: String,
            bbsId: String
        ): Intent {
            return getIntent(context, videoId, bbsId, "", "", false)
        }

        fun getIntent(
            context: Context,
            videoId: String,
            bbsId: String,
            scrollToComment: Boolean = false
        ): Intent {
            return getIntent(context, videoId, bbsId, "", "", scrollToComment)
        }

        fun getRecommendIntent(
            context: Context,
            videoId: String,
            bbsId: String,
            recommendId: String
        ): Intent {
            return getIntent(context, videoId, bbsId, recommendId, "", false)
        }

        fun getSpecifiedCommentIntent(
            context: Context,
            videoId: String,
            topCommentId: String
        ): Intent {
            return getIntent(context, videoId, "", "", topCommentId, true)
        }

        fun getIntent(
            context: Context,
            videoId: String,
            bbsId: String,
            recommendId: String = "",
            topCommentId: String = "",
            scrollToComment: Boolean = false
        ): Intent {
            val intent = Intent(context, ForumVideoDetailActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_VIDEO_ID, videoId)
            intent.putExtra(EntranceConsts.KEY_BBS_ID, bbsId)
            intent.putExtra(EntranceConsts.KEY_RECOMMEND_ID, recommendId)
            intent.putExtra(EntranceConsts.KEY_TOP_COMMENT_ID, topCommentId)
            if (scrollToComment) {
                intent.putExtra(PAGE_INDEX, 1)
            }
            return intent
        }
    }

}