package com.cr.widget

import android.content.Context
import android.opengl.Visibility
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.cr.common.CrLiveStreamStateInfo
import com.cr.common.CrTimerManager
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.event.CrCommonAction
import com.cr.event.EventCommon
import com.squareup.otto.Subscribe

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/9/13 14:37
 * 描述：直播信息组件
 */
class CrLiveInfoWidget @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet?=null,
    defStyleAttr: Int = 0
):CrLinearLayoutWidget(context,attrs,defStyleAttr){
    // todo: 2023/9/13 组件定义
    private var liveInfoFps: TextView?= null  // define: 2023/9/13 帧率
    private var liveInfoVbps: TextView?= null // define: 2023/9/13 码率
    private var liveInfoResolution: TextView?=null // define: 2023/9/13 分辨率
    private var liveInfoPacket: TextView?=null // define: 2023/9/13 丢包率
    private var liveInfoRtt: TextView?=null // define: 2023/9/13 延迟
    private var liveTimer:TextView?= null // define: 2023/9/14 直播计时
    private var liveInfoPanel:LinearLayout?= null // define: 2023/9/13 直播信息容器
    /**
     * 初始化
     */
    init {
        // todo: 2023/9/13 创建视图
        initView(R.layout.wg_live)
        // todo: 2023/9/13 挂载控件
        joinControls()
    }

    /**
     * 覆写挂载控件
     */
    override fun joinControls() {
        liveInfoFps = findViewById(R.id.live_info_fps)
        liveInfoVbps = findViewById(R.id.live_info_vbps)
        liveInfoResolution = findViewById(R.id.live_info_resolution)
        liveInfoPacket = findViewById(R.id.live_info_packet)
        liveInfoRtt = findViewById(R.id.live_info_rtt)
        liveTimer = findViewById(R.id.live_info_timer)
        liveInfoPanel = findViewById(R.id.live_info_panel)
        liveInfoPanel?.visibility = GONE
    }

    /**
     * 生命周期
     * 该方法当View及其子View从XML文件中加载完成后触发调用
     */
    override fun onFinishInflate() {
        super.onFinishInflate()
    }

    /**
     * 生命周期
     * 当前View或其祖先的可见性改变时被调用
     * @param changedView View
     * @param visibility Int
     */
    override fun onVisibilityChanged(changedView: View, visibility: Int) {
        super.onVisibilityChanged(changedView, visibility)
    }

    /**
     * 生命周期
     * 当View被附着到一个窗口时触发
     */
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        // todo: 2023/9/13 注册订阅
        CrApplication.getEventBus().register(this)
    }

    /**
     * 案件生命周期
     * 当View离开附着的窗口时触发
     * Activity调用onDestroy方法时View就会离开窗口
     */
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        // todo: 2023/9/13 解除订阅
        CrApplication.getEventBus().unregister(this)
    }

    /**
     * 更新直播信息
     * @param event EventCommon<CrLiveStreamStateInfo>
     */
    @Subscribe
    fun onUpdate(event:EventCommon<CrLiveStreamStateInfo>){
        if(event.action == CrCommonAction.UPDATE_LIVE_INFO){
            liveInfoFps?.text = "${event.param?.fps}f"
            liveInfoVbps?.text = "${event.param?.vbps}k"
            liveInfoResolution?.text = "${event.param?.videoWidth}*${event.param?.videoHeight}"
            liveInfoPacket?.text = "${event.param?.packetLoss}%"
            liveInfoRtt?.text = "${event.param?.rtt}ms"
        }
    }

    /**
     * 订阅显示或隐藏
     * @param event EventCommon<Boolean>
     */
    @Subscribe
    fun onShowStatus(event:EventCommon<Boolean>){
        if(event.action == CrCommonAction.LIVE_INFO_STATE){
            if(event.param == true){
                liveInfoPanel?.visibility = VISIBLE
                // todo: 2023/9/14 设置计时器
                CrTimerManager.getInstance().startTimer(1,object:CrTimerManager.ITimerCountListener{
                    // todo: 2023/9/14 计时回调
                    override fun onTimer(count: Long, timeValue: String) {
                        mainHandler.post{
                            liveTimer?.text = timeValue
                        }
                    }
                })
            }else{
                liveInfoPanel?.visibility = GONE
                // todo: 2023/9/14 关闭计时器
                CrTimerManager.getInstance().stopTimer()
            }
        }
    }
}