package com.gh.gamecenter.qa.editor

import android.app.Activity
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() {

    private var mVideoUrl = ""
    private lateinit var mBinding: ActivityFullScreenVideoBinding
    private var mCurrentPosition = -1

    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) ?: ""
        val position = intent.getLongExtra(EntranceConsts.KEY_POSITION, -1L)
        mBinding.videoView.backBtn.setOnClickListener { finish() }
        setUpTopVideo(title, mVideoUrl, poster, position)
    }

    private fun setUpTopVideo(title: String, url: String, poster: String, position: Long) {
        GSYVideoOptionBuilder()
            .setIsTouchWigetFull(false)
            .setIsTouchWiget(false)
            .setRotateViewAuto(false)
            .setShowFullAnimation(false)
            .setSeekRatio(1f)
            .setUrl(url)
            .setVideoTitle(title)
            .setCacheWithPlay(true)
            .setGSYVideoProgressListener { _, _, currentPosition, _ ->
                mCurrentPosition = currentPosition
            }
            .also {
                if (position != -1L) {
                    it.setSeekOnStart(position)
                }
            }
            .build(mBinding.videoView)

        mBinding.videoView.updateThumb(poster)

        if (NetworkUtils.isWifiConnected(this) || position != -1L) {
            mBinding.videoView.startPlayLogic(position == -1L)
        }
        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 finish() {
        if (!isFinishing) {
            val newIntent = Intent().apply {
                putExtra(EntranceConsts.KEY_URL, mVideoUrl)
                putExtra(EntranceConsts.KEY_POSITION, mCurrentPosition)
            }
            setResult(RESULT_OK, newIntent)
        }
        super.finish()
    }

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

    companion object {
        const val REQUEST_CODE = 10013

        fun start(context: Context, title: String, url: String, poster: String, progress: Long = -1L) {
            val intent = Intent(context, FullScreenVideoActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_URL, url)
            intent.putExtra(EntranceConsts.KEY_POSITION, progress)
            intent.putExtra(EntranceConsts.KEY_NAVIGATION_TITLE, title)
            intent.putExtra(EntranceConsts.KEY_POSTER_PATH, poster)
            if (context is Activity) {
                context.startActivityForResult(intent, REQUEST_CODE)
            } else {
                context.startActivity(intent)
            }
        }
    }

}