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

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.therouter.router.Route
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.base.fragment.BaseFragment_TabLayout.PAGE_INDEX
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.constant.RouteConsts

/**
 * 视频贴详情
 */
@Route(path = RouteConsts.activity.forumVideoDetailActivity)
class ForumVideoDetailActivity : BaseActivity() {
    var containerFragment: Fragment? = null

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

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

        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,
            sourceEntrance: String = ""
        ): Intent {
            return getIntent(context, videoId, bbsId, "", "", false, sourceEntrance)
        }

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

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

        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,
            sourceEntrance: String = ""
        ): 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)
            intent.putExtra(EntranceConsts.KEY_SOURCE_ENTRANCE, sourceEntrance)
            if (scrollToComment) {
                intent.putExtra(PAGE_INDEX, 1)
            }
            return intent
        }
    }

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

}