package com.gh.gamecenter.qa.editor

import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.MD5Utils
import com.gh.gamecenter.common.utils.NetworkUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.ActivityFullScreenVideoBinding
import com.gh.gamecenter.forum.home.ForumScrollCalculatorHelper
import com.gh.gamecenter.video.detail.CustomManager
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder

class FullScreenVideoActivity : BaseActivity() {
    lateinit var mBinding: ActivityFullScreenVideoBinding
    private var mVideoUrl = ""

    override fun getLayoutId(): Int = R.layout.activity_full_screen_video

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }
        DisplayUtils.transparentStatusBar(this)
        DisplayUtils.hideNavigationBar(this)
        mBinding = ActivityFullScreenVideoBinding.bind(mContentView)

        mVideoUrl = intent.getStringExtra(EntranceConsts.KEY_URL) ?: ""
        val poster = intent.getStringExtra(EntranceConsts.KEY_POSTER_PATH) ?: ""
        val title = intent.getStringExtra(EntranceConsts.KEY_NAVIGATION_TITLE) ?: ""
        mBinding.videoView.backBtn.setOnClickListener { finish() }
        setUpTopVideo(title, mVideoUrl, poster)
    }

    private fun setUpTopVideo(title: String, url: String, poster: String) {
        GSYVideoOptionBuilder()
            .setIsTouchWigetFull(false)
            .setIsTouchWiget(false)
            .setRotateViewAuto(false)
            .setShowFullAnimation(false)
            .setSeekRatio(1f)
            .setUrl(url)
            .setVideoTitle(title)
            .setCacheWithPlay(true)
            .build(mBinding.videoView)

        mBinding.videoView.updateThumb(poster)

        if (NetworkUtils.isWifiConnected(this)) {
            mBinding.videoView.startPlayLogic(isAutoPlay = true)
        }
        mBinding.videoView.observeVolume(this)
    }

    override fun onResume() {
        super.onResume()
        CustomManager.onResume(mBinding.videoView.getKey())
    }

    override fun onPause() {
        super.onPause()
        CustomManager.onPause(mBinding.videoView.getKey())
        val currentPosition = mBinding.videoView.getCurrentPosition()
        ForumScrollCalculatorHelper.savePlaySchedule(MD5Utils.getContentMD5(mVideoUrl), currentPosition)
    }

    override fun onDestroy() {
        super.onDestroy()
        CustomManager.releaseAllVideos(mBinding.videoView.getKey())
        mBinding.videoView.disposableTimer()
    }

    companion object {
        fun start(context: Context, title: String, url: String, poster: String) {
            val intent = Intent(context, FullScreenVideoActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_URL, url)
            intent.putExtra(EntranceConsts.KEY_NAVIGATION_TITLE, title)
            intent.putExtra(EntranceConsts.KEY_POSTER_PATH, poster)
            context.startActivity(intent)
        }
    }

}