package com.rc.core.util

import android.view.View
import android.widget.FrameLayout
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient
import com.tencent.smtt.sdk.WebChromeClient

/**
 * info
 *
 * @author ReiChin_
 */
class VideoFullScreenWebChromeClient(
    private val fullScreen: FrameLayout
) : WebChromeClient() {

    // 用于保存系统提供的全屏视频所在的view
    // 可将view添加到我们自己的视频播放锚点布局中
    private var mCustomView: View? = null
    private var mCustomViewCallback: IX5WebChromeClient.CustomViewCallback? = null

    override fun onShowCustomView(view: View?, callback: IX5WebChromeClient.CustomViewCallback?) {
        super.onShowCustomView(view, callback)
        // view为全屏时，系统提供的视频展示窗口
        // 如果view已经存在，则隐藏
        if (mCustomView != null) {
            callback?.onCustomViewHidden()
            return
        }
        mCustomView = view
        mCustomView?.visibility = View.VISIBLE
        mCustomViewCallback = callback
        fullScreen.addView(mCustomView)
        // 仅显示视频锚点布局
        fullScreen.visibility = View.VISIBLE
    }

    override fun onHideCustomView() {
        super.onHideCustomView()
        if (mCustomView == null) {
            return
        }
        mCustomView?.visibility = View.GONE
        fullScreen.removeView(mCustomView)
        mCustomView = null

        mCustomViewCallback?.onCustomViewHidden()
    }

}