package com.huawei.rtcdemo.adapter;

import com.huawei.rtc.IRtcEngineEventHandler;
import com.huawei.rtc.models.LocalAudioStats;
import com.huawei.rtc.models.LocalVideoStats;
import com.huawei.rtc.models.RemoteAudioStats;
import com.huawei.rtc.models.RemoteVideoStats;
import com.huawei.rtc.models.StatsInfo;
import com.huawei.rtc.utils.RtcEnums;
import com.huawei.rtc.models.UserInfo.RoleType;
import java.util.ArrayList;
import java.util.List;

public class RtcEventHandler implements IRtcEngineEventHandler {
    private ArrayList<IRtcEngineEventHandler> mHandler = new ArrayList<>();

    @Override
    public void onWarning(int s, String s1) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onWarning(s, s1);
        }
    }

    @Override
    public void onError(int s, String s1) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onError(s, s1);
        }
    }

    @Override
    public void onJoinRoomSuccess(String s, String s1) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onJoinRoomSuccess(s, s1);
        }
    }

    @Override
    public void onJoinRoomFailure(int i, String s) {

    }

    @Override
    public void onLeaveRoom(String s, String s1, StatsInfo statsInfo) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onLeaveRoom(s, s1, statsInfo);
        }
    }

    @Override
    public void onUserJoined(String s, String s1, String s2) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onUserJoined(s, s1, s2);
        }
    }

    @Override
    public void onUserOffline(String s, String s1, int i) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onUserOffline(s, s1, i);
        }
    }

    @Override
    public void onFirstRemoteVideoDecoded(String s, String s1, int i, int i1) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onFirstRemoteVideoDecoded(s, s1, i, i1);
        }
    }

    @Override
    public void onAgreedStreamAvailable(String s, String s1, RtcEnums.StreamType streamType) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onAgreedStreamAvailable(s, s1, streamType);
        }
    }

    @Override
    public void onConnectionStateChange(RtcEnums.ConnStateTypes i, RtcEnums.ConnChangeReason i1, String s) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onConnectionStateChange(i, i1, s);
        }
    }

    @Override
    public void onLogUploadResult(int i) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onLogUploadResult(i);
        }
    }

    @Override
    public void onLogUploadProgress(int i) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onLogUploadProgress(i);
        }
    }

    @Override
    public void onUserRoleChanged(RoleType oldRole, RoleType newRole){
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onUserRoleChanged(oldRole, newRole);
        }
    }

    @Override
    public void onAudioRouteChanged(RtcEnums.AudioRoute audioRoute) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onAudioRouteChanged(audioRoute);
        }
    }

    @Override
    public void onVideoStats(List<LocalVideoStats> list, List<RemoteVideoStats> list1) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onVideoStats(list, list1);
        }
    }

    @Override
    public void onAudioStats(List<LocalAudioStats> list, List<RemoteAudioStats> list1) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onAudioStats(list, list1);
        }
    }

    @Override
    public void onUserSubStreamAvailable(String s, String s1, boolean b) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onUserSubStreamAvailable(s, s1,b);
        }
    }

    @Override
    public void onRenderVideoFrame(String s, String s1, byte[] bytes, int i, int i1, int i2, int i3) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onRenderVideoFrame(s, s1, bytes, i, i1, i2,i3);
        }
    }

    @Override
    public void onPlaybackAudioFrame(String s, byte[] bytes, int i, int i1, int i2, int i3, int i4) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onPlaybackAudioFrame(s, bytes, i, i1, i2, i3, i4);
        }
    }

    @Override
    public void onSubStreamStats(boolean b, LocalVideoStats lcoalStats, RemoteVideoStats remoteStats) {
        for (IRtcEngineEventHandler handler : mHandler) {
            handler.onSubStreamStats(b,lcoalStats,remoteStats);
        }
    }

    public void addHandler(IRtcEngineEventHandler handler) {
        mHandler.add(handler);
    }

    public void removeHandler(IRtcEngineEventHandler handler) {
        mHandler.remove(handler);
    }
}
