package com.ez.ertc.meeting.presenter;

import com.ez.basertc.RTCConstant;
import com.ez.dclog.RTCStatsListener;
import com.ez.dclog.statistics.ERTCLocalAudioStats;
import com.ez.dclog.statistics.ERTCLocalVideoStats;
import com.ez.dclog.statistics.ERTCRemoteAudioStats;
import com.ez.dclog.statistics.ERTCRemoteVideoStats;
import com.ez.ertcmeeting.bean.DeviceState;
import com.ez.ertcmeeting.bean.UserAccount;

import java.lang.ref.WeakReference;

public class ErtcStatsListener extends RTCStatsListener {
    private WeakReference<ErtcRoomPresenter> mReference;
    public ErtcStatsListener(ErtcRoomPresenter roomPresenter) {
        this.mReference = new WeakReference(roomPresenter);
    }

    @Override
    public void onLocalVideoStats(final ERTCLocalVideoStats localVideoStats) {
        super.onLocalVideoStats(localVideoStats);
//        LogUtil.d("EZRTCImpl", "onLocalVideoStats " + localVideoStats.toString());
        final ErtcRoomPresenter roomPresenter = mReference.get();
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(roomPresenter.mMonitorDialog != null) {
                    int streamType = localVideoStats.videoList.get(0).streamType;
                    if(streamType == RTCConstant.ERTC_VIDEO_STREAM_TYPE_SHARE) {
                        roomPresenter.mMonitorDialog.setShareVideoStats(roomPresenter.isSharing() ? localVideoStats : null);
                    } else if(streamType == RTCConstant.ERTC_VIDEO_STREAM_TYPE_SMALL || streamType == RTCConstant.ERTC_VIDEO_STREAM_TYPE_BIG) {
                        roomPresenter.mMonitorDialog.setLocalVideoStats(roomPresenter.mMyAccount.getVideoStatus() == DeviceState.DS_Active.getValue() ? localVideoStats : null);
                    }
                }
            }
        });
    }

    @Override
    public void onLocalAudioStats(final ERTCLocalAudioStats localAudioStats) {
        super.onLocalAudioStats(localAudioStats);
//        LogUtil.d("EZRTCImpl", "onLocalAudioStats " + localAudioStats.toString());
        final ErtcRoomPresenter roomPresenter = mReference.get();
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (roomPresenter.mMonitorDialog != null) {
                    roomPresenter.mMonitorDialog.setLocalAudioStats(roomPresenter.mMyAccount.getAudioStatus() == DeviceState.DS_Active.getValue() ? localAudioStats : null);
                }
            }
        });
    }

    @Override
    public void onRemoteVideoStats(final ERTCRemoteVideoStats remoteVideoStats) {
        super.onRemoteVideoStats(remoteVideoStats);
//        LogUtil.d("EZRTCImpl", "onRemoteVideoStats " + remoteVideoStats.toString());
        final ErtcRoomPresenter roomPresenter = mReference.get();
        UserAccount videoAccount = roomPresenter.getAccountByUserId(remoteVideoStats.userId);
        if(videoAccount == null) return;
        if(remoteVideoStats.userId.equals(roomPresenter.mSharingAccountId)){
            roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (roomPresenter.mMonitorDialog != null) {
                        roomPresenter.mMonitorDialog.setShareVideoStats(remoteVideoStats);
                    }
                }
            });
            return;
        }
        if(videoAccount.getVideoStatus() != DeviceState.DS_Active.getValue()) return;
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (roomPresenter.mMonitorDialog != null) {
                    roomPresenter.mMonitorDialog.addRemoteVideoStats(remoteVideoStats);
                }
            }
        });
    }

    @Override
    public void onRemoteAudioStats(final ERTCRemoteAudioStats remoteAudioStats) {
        super.onRemoteAudioStats(remoteAudioStats);
//        LogUtil.d("EZRTCImpl", "onRemoteAudioStats " + remoteAudioStats.toString());
        final ErtcRoomPresenter roomPresenter = mReference.get();
        UserAccount audioAccount = roomPresenter.getAccountByUserId(remoteAudioStats.userId);
        if(audioAccount == null) return;
        if(audioAccount.getAudioStatus() != DeviceState.DS_Active.getValue()) return;
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (roomPresenter.mMonitorDialog != null) {
                    roomPresenter.mMonitorDialog.addRemoteAudioStats(remoteAudioStats);
                }
            }
        });
    }
}
