package com.gh.gamecenter.video.detail

import android.os.Bundle
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.constant.EntranceConsts.IS_DETAIL_PAGE
import java.util.*

/**
 * 旧视频页面
 */
// 由于兼容性问题，本游戏详情页不应该被低于 Android 4.4 的设备打开
class VideoDetailActivity : BaseActivity() {
    var uuid = UUID.randomUUID()
    var mContainerFragment: HomeVideoFragment? = null

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

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

        DisplayUtils.transparentStatusBar(this)

        val extras = intent.extras
        extras?.putString("uuid", uuid.toString())
        extras?.putBoolean(IS_DETAIL_PAGE, true)
        mContainerFragment =
            supportFragmentManager.findFragmentByTag(HomeVideoFragment::class.java.name) as? HomeVideoFragment
                ?: HomeVideoFragment().with(extras) as HomeVideoFragment

        supportFragmentManager.beginTransaction()
            .replace(com.gh.gamecenter.selector.R.id.layout_activity_content, mContainerFragment!!, HomeVideoFragment::class.java.name)
            .commitNowAllowingStateLoss()
    }

    override fun handleBackPressed(): Boolean {
        return if (mContainerFragment?.getCurrentFragment() != null) {
            mContainerFragment?.getCurrentFragment()?.onHandleBackPressed() ?: false
        } else false
    }

    override fun onDestroy() {
        CustomManager.releaseAllVideos("detail_$uuid")
        mBaseHandler.postDelayed({
            CustomManager.releaseAllVideos("detail_$uuid")
        }, 500)
        super.onDestroy()
    }

}