//
//  ERTCMembersNetworkManager.m
//  EZSaaSVideoConferenceSDK
//
//  Created by wangjinlong23 on 2022/6/24.
//  Copyright © 2022 EZSaaS. All rights reserved.
//

#import "ERTCMembersNetworkManager.h"
#import "ERTCVideoMeetingManager.h"
#import "ERTCVideoMeetingManager.h"
#import <ERTC/ERTCStatistics.h>

@interface ERTCMembersNetworkManager ()

@property (nonatomic, strong) NSMutableDictionary *localVideoDic;//本地视频数据
@property (nonatomic, strong) NSMutableDictionary *remoteAudioDic;//远端音频数据
@property (nonatomic, strong) NSMutableDictionary *remoteVideoDic;//远端视频数据
@end

@implementation ERTCMembersNetworkManager

+ (instancetype)sharedInstance {
    static ERTCMembersNetworkManager *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [ERTCMembersNetworkManager new];
        _sharedInstance.localVideoDic = [NSMutableDictionary dictionaryWithCapacity:3];
        _sharedInstance.remoteAudioDic = [NSMutableDictionary dictionaryWithCapacity:5];
        _sharedInstance.remoteVideoDic = [NSMutableDictionary dictionaryWithCapacity:5];
    });
    
    return _sharedInstance;
}


- (void)onLocalAudioStats:(ERTCLocalAudioStatistics *)audioStats {
    if ([self.delegate respondsToSelector:@selector(onLocalAudioStats:)]) {
        [self.delegate onLocalAudioStats:audioStats];
    }
}

- (void)onLocalVideoStats:(ERTCLocalVideoStatistics *)videoStats {
    NSString *streamType = [NSString stringWithFormat:@"%d",videoStats.videoStreamType];
    [self.localVideoDic setObject:videoStats forKey:streamType];
    if ([self.delegate respondsToSelector:@selector(onLocalVideoStats:)]) {
        [self.delegate onLocalVideoStats:self.localVideoDic];
    }
}

- (void)onRemoteVideoStats:(ERTCRemoteVideoStatistics *)videoStats {
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:videoStats.userId];

    if (info !=nil) {
        [self.remoteVideoDic setObject:videoStats forKey:videoStats.userId];
        
    } else {
        [self.remoteVideoDic removeObjectForKey:videoStats.userId];
    }
    if ([self.delegate respondsToSelector:@selector(onRemoteVideoUpdate:)]) {
        [self.delegate onRemoteVideoUpdate:self.remoteVideoDic];
    }
    
}

- (void)onRemoteAudioStats:(ERTCRemoteAudioStatistics *)audioStats {
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:audioStats.userId];
    if (info !=nil) {
        if ( info.isAudioAvailable) {
            [self.remoteAudioDic setObject:audioStats forKey:audioStats.userId];
        } else {
            [self.remoteAudioDic removeObjectForKey:audioStats.userId];
        }
    } else {
        [self.remoteAudioDic removeObjectForKey:audioStats.userId];
    }
    if ([self.delegate respondsToSelector:@selector(onRemoteAudioUpdate:)]) {
        [self.delegate onRemoteAudioUpdate:self.remoteAudioDic];
    }
}

- (void)deleteRemoteData:(NSString *)userId {
    [self.remoteAudioDic removeObjectForKey:userId];
    [self.remoteVideoDic removeObjectForKey:userId];
    if ([self.delegate respondsToSelector:@selector(onRemoteAudioUpdate:)]) {
        [self.delegate onRemoteAudioUpdate:self.remoteAudioDic];
    }
    if ([self.delegate respondsToSelector:@selector(onRemoteVideoUpdate:)]) {
        [self.delegate onRemoteVideoUpdate:self.remoteVideoDic];
    }
}

@end
