package com.ez.ertc.call.ui

import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import com.ez.ertc.MyApp
import com.ez.ertc.R
import com.ez.ertc.base.BaseActivity
import com.ez.ertc.call.service.StatsLiveDataService
import kotlinx.android.synthetic.main.activity_stats.*
import java.time.LocalDateTime

class StatsActivity : BaseActivity() {
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_stats)

        StatsLiveDataService.instance?.mERTCLocalAudioStats?.observeForever { mERTCLocalAudioStats ->
            tv_time.text = "回调时间： ${LocalDateTime.now().toString()}"
            tv_localaudio_sentBitrate.text = "音频传输码率：${mERTCLocalAudioStats.sentBitrate}"
            tv_localaudio_packetCount.text = "音频总包数：${mERTCLocalAudioStats.packetCount}"
            tv_localaudio_packetLossRate.text = "音频丢包率：${mERTCLocalAudioStats.packetLossRate}"
        }

        StatsLiveDataService.instance?.mERTCLocalVideoStats?.observeForever { mERTCLocalVideoStats ->
            tv_time.text = "回调时间： ${LocalDateTime.now().toString()}"
            tv_localvideo_sentBitrate.text = "视频传输码率：${mERTCLocalVideoStats.videoList.firstOrNull()?.sentBitrate}"
            tv_localvideo_sentFrameRate.text = "帧率大小：${mERTCLocalVideoStats.videoList.firstOrNull()?.sentFrameRate}"
            tv_localvideo_packetLossRate.text = "视频丢包率：${mERTCLocalVideoStats.packetLossRate}"
            tv_localvideo_streamType.text = "流类型：${mERTCLocalVideoStats.videoList.firstOrNull()?.streamType}"
            tv_localvideo_packetCount.text = "视频总包数：${mERTCLocalVideoStats.videoList.firstOrNull()?.packetCount}"
            tv_localvideo_width.text = "视频宽：${mERTCLocalVideoStats.videoList.firstOrNull()?.width}"
            tv_localvideo_height.text = "视频高：${mERTCLocalVideoStats.videoList.firstOrNull()?.height}"
            tv_localvideo_compensateLossRate.text = "补偿后丢包率：${mERTCLocalVideoStats.videoList.firstOrNull()?.compensateLossRate}"
        }

        StatsLiveDataService.instance?.mERTCRemoteAudioStats?.observeForever { mERTCRemoteAudioStats ->
            tv_time.text = "回调时间： ${LocalDateTime.now().toString()}"
            tv_remoteaudio_userid.text = "远端音频用户userId：${mERTCRemoteAudioStats.userId}"
            tv_remoteaudio_receivedBitrate.text = "音频传输码率：${mERTCRemoteAudioStats.receivedBitrate}"
            tv_remoteaudio_packetLossRate.text = "音频丢包率：${mERTCRemoteAudioStats.packetLossRate}"
            tv_remoteaudio_totalFrozenTime.text = "音频总的卡顿时长：${mERTCRemoteAudioStats.totalFrozenTime}"
            tv_remoteaudio_frozenRate.text = "音频卡顿比：${mERTCRemoteAudioStats.frozenRate}"
            tv_remoteaudio_packetCount.text = "音频总包数：${mERTCRemoteAudioStats.packetCount}"
            tv_remoteaudio_plcPacketCount.text = "音频帧补偿数量：${mERTCRemoteAudioStats.plcPacketCount}"
            tv_remoteaudio_uncompensateLoss.text = "音频补偿前丢包率（％）：${mERTCRemoteAudioStats.uncompensateLoss}"
            tv_remoteaudio_totalTime.text = "播放音频总时间：${mERTCRemoteAudioStats.totalTime}"
        }

        StatsLiveDataService.instance?.mERTCRemoteVideoStats?.observeForever { mERTCRemoteVideoStats ->
            tv_time.text = "回调时间： ${LocalDateTime.now().toString()}"
            tv_remotevideo_userid.text = "远端视频用户userId：${mERTCRemoteVideoStats.userId}"
            tv_remotevideo_receivedBitrate.text = "视频传输码率：${mERTCRemoteVideoStats.receivedBitrate}"
            tv_remotevideo_receivedFrameRate.text = "帧率大小：${mERTCRemoteVideoStats.receivedFrameRate}"
            tv_remotevideo_packetLossRate.text = "视频丢包率：${mERTCRemoteVideoStats.packetLossRate}"
            tv_remotevideo_totalFrozenTime.text = "视频总的卡顿时长：${mERTCRemoteVideoStats.totalFrozenTime}"
            tv_remotevideo_frozenRate.text = "视频卡顿比：${mERTCRemoteVideoStats.frozenRate}"
            tv_remotevideo_streamType.text = "流类型：${mERTCRemoteVideoStats.streamType}"
            tv_remotevideo_packetCount.text = "视频总包数：${mERTCRemoteVideoStats.packetCount}"
            tv_remotevideo_width.text = "视频宽：${mERTCRemoteVideoStats.width}"
            tv_remotevideo_height.text = "视频高：${mERTCRemoteVideoStats.height}"
            tv_remotevideo_uncompensateLoss.text = "视频补偿前丢包率（％）：${mERTCRemoteVideoStats.uncompensateLoss}"
        }

        btn_interval_5.setOnClickListener {
            MyApp.ertcEngine?.setQualityInterval(5)
        }
        
        btn_interval_10.setOnClickListener {
            MyApp.ertcEngine?.setQualityInterval(10)
        }

    }
}