//
//  ERTCVideoMeetingViewController+Video.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/5/13.
//

#import "ERTCVideoMeetingViewController+Video.h"
#import "UIView+Toast.h"
#import "ERTCPlayerViewManager.h"
#import "ERTCVideoMeetingManager.h"
#import "ERTCMembersNetworkManager.h"

@implementation ERTCVideoMeetingViewController (Video)

- (void)onEnterRoomSuccess:(NSInteger)result {
    ERTCLog(@"加入房间成功");
    [EZSaaSRTCProgressHUD showSuccessWithStatus:@"加入会议成功"];
    dispatch_async(dispatch_get_main_queue(), ^{
        ERTCUserInfo *localUser = [[ERTCUserInfo alloc] initWithUID:self.userId];
        [ERTCVideoMeetingManager sharedInstance].localUserInfo = localUser;
        [self.vcs_player setDefaultParam];
        
    });
}

- (void)onExitRoom:(ERTCSelfExitReason)reason {
    ERTCLog(@"已退出房间，原因：%ld",(long)reason);
    [self showAlert:@"确认退出房间？" withSureAction:^{
        [[ERTCPlayerViewManager sharedInstance] exitRoom];
        [self.navigationController popViewControllerAnimated:YES];
        
    } andCencelAction:^{
        ;
    }];
   
}

- (void)onRemoteUserEnterRoom:(NSString *)userId {
    NSLog(@"远端用户进入房间:%@",userId);
    [[ERTCVideoMeetingManager sharedInstance] onRemoteUserEnterRoom:userId];
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:userId];
    [self.vcs_player onMemberEnterRoom:info];
    
}

- (void)onRemoteUserLeaveRoom:(NSString *)userId reason:(NSInteger)reason {
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:userId];
    [self.vcs_player onMemberExitRoom:info];
    [[ERTCVideoMeetingManager sharedInstance] onRemoteUserLeaveRoom:userId];
    ERTCLog(@"[%@]退出房间，原因：%ld",userId,reason);
//    [self showMessage:[NSString stringWithFormat:@"[%@]退出房间，原因：%ld",userId,reason]];
}

- (void)onUserVideoAvailable:(NSString *)userId available:(BOOL)available{
    ERTCLog(@"[%@]视频变更:%d",userId,available);
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:userId];
    info.isVideoAvailable = available;
    if ([ERTCVideoMeetingManager sharedInstance].enableAudioMode) {
        info.isVideoAvailable = NO;
        [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:info.uid withStream:ERTCVideoStreamTypeSmall];
    }
    NSNumber *videoStateNumber = [NSNumber numberWithBool:available];
   [[ERTCVideoMeetingManager sharedInstance].userVideoStateDic setObject:videoStateNumber forKey:userId];
    [self.vcs_player onMemberInfoUpdate:info];
}


- (void)onUserScreenShareAvailable:(NSString *)userId available:(BOOL)available {
    ERTCLog(@"[%@]屏幕共享变更:%d",userId,available);
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:userId];
    info.isSharingAccount = available;
    [self.vcs_player showRemoteShare:userId avaliable:available];
}


- (void)onUserAudioAvailable:(NSString *)userId available:(BOOL)available {
    ERTCLog(@"[%@]音频变更:%d",userId,available);
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:userId];
    info.isAudioAvailable = available;
    [self.vcs_player onMemberInfoUpdate:info];
}

- (void)onError:(NSInteger)errCode {
    ERTCLog(@"发生错误:%ld",(long)errCode);
    if (errCode > ERTC_ERR_ROOM_BASE && errCode < ERTC_ERR_ROOM_EXIT_FAIL) {
        [self showAlert:@"进入房间失败" withAction:^{
            [[ERTCPlayerViewManager sharedInstance] exitRoom];
            [[ERTCVideoMeetingManager sharedInstance] exitRoom];
            [self.vcs_player exitMeeting];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    } else {
        [self showMessage:[NSString stringWithFormat:@"发生错误:%ld",errCode]];
    }
        
}

- (void)onFirstRemoteVideoFrame:(NSString *)userId width:(int)width height:(int)height {
    ERTCLog(@"显示视频[%@],[%d],[%d]",userId,width,height);
//    [self showMessage:[NSString stringWithFormat:@"显示[%@]视频[宽:%d][高:%d]",userId,width,height]];
}

- (void)onFirstLocalVideoFrameWithWidth:(int)width height:(int)height {
    ERTCLog(@"显示自己的视频,[%d],[%d]",width,height);
//    [self showMessage:[NSString stringWithFormat:@"显示自己的视频[宽:%d][高:%d]",width,height]];
}


- (void)onUserVoiceVolume:(NSArray<ERTCVolumeInfo *> *)userVolumes totalVolume:(NSInteger)totalVolume {
    [userVolumes enumerateObjectsUsingBlock:^(ERTCVolumeInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"音量大小:%ld",obj.volume);
    }];
    [self.vcs_player updateVolume:userVolumes];
}

- (void)onNetworkQuality:(ERTCNetworkQuality)quality ofUser:(NSString *)userId {
    NSLog(@"网络状态:%lu userId:%@",(unsigned long)quality,userId);
    if ([userId isEqualToString:self.vcs_player.mineModel.uid]) {
        ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:userId];
        info.netLevel = quality;
        self.vcs_player.mineModel.netLevel = quality;
        [self.vcs_player updateNetworkQuality:self.vcs_player.mineModel];
    } else {
        ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:userId];
        info.netLevel = quality;
        [self.vcs_player updateNetworkQuality:info];
    }
}

- (void)onUserCustomMsg:(NSString *)userId msg:(NSData *)msg {
    ERTCLog(@"收到消息 userid:%lu msg:%lu",(unsigned long)userId,(unsigned long)msg.length);
    NSString *result =[[ NSString alloc] initWithData:msg encoding:NSUTF8StringEncoding];
    [self showMessage:[NSString stringWithFormat:@"收到custom消息 userid:%@ msg:%@",userId,result]];
}

- (void)onLocalAudioStats:(ERTCLocalAudioStatistics *)audioStats {
    [[ERTCMembersNetworkManager sharedInstance] onLocalAudioStats:audioStats];
}

- (void)onLocalVideoStats:(ERTCLocalVideoStatistics *)videoStats {
    [[ERTCMembersNetworkManager sharedInstance] onLocalVideoStats:videoStats];
}

- (void)onRemoteVideoStats:(ERTCRemoteVideoStatistics *)videoStats {
    [[ERTCMembersNetworkManager sharedInstance] onRemoteVideoStats:videoStats];
}

- (void)onRemoteAudioStats:(ERTCRemoteAudioStatistics *)audioStats {
    [[ERTCMembersNetworkManager sharedInstance] onRemoteAudioStats:audioStats];
}

- (void)onDisplay:(NSString *)userId dataLength:(int)dataLength {
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:userId];
    [self.vcs_player onDisplay:info];
}

- (void)onFirstScreenShareStream:(int)width height:(int)height {
    NSLog(@"共享屏幕的宽:%d 共享屏幕的高:%d",width,height);
    CGSize size = CGSizeMake(width, height);
    [ERTCVideoMeetingManager sharedInstance].screenShareSize = size;
}




@end
