//
//  ERTCVideoMeetingManager.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/5/11.
//

#import "ERTCVideoMeetingManager.h"
#import "ERTCPlayerViewManager.h"

@implementation ERTCUserInfo

- (instancetype)initWithUID:(NSString *)uid {
    self = [super init];
    if (self) {
        self.uid = uid;
        self.isVideoAvailable = NO;
        self.isAudioAvailable = NO;
        self.isSmallStream = NO;
    }
    return self;
}

- (BOOL)isEqual:(id)other {
    if (other == self) {
        return YES;
    } else {
        if ([other isKindOfClass:[self class]]) {
            return self.uid == ((ERTCUserInfo *)other).uid;
        }
        return NO;
    }
}


@end


@interface ERTCVideoMeetingManager ()

@property (nonatomic, strong) NSMutableDictionary *remoterAccountDic;//远端用户


@end

@implementation ERTCVideoMeetingManager

+ (instancetype)sharedInstance {
    static ERTCVideoMeetingManager *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [ERTCVideoMeetingManager new];
        _sharedInstance.userVideoStateDic = [NSMutableDictionary dictionary];
        _sharedInstance.userNetLevelDic = [NSMutableDictionary dictionary];
        _sharedInstance.remoterAccountDic = [NSMutableDictionary dictionary];
        _sharedInstance.subscribeStateDic = [NSMutableDictionary dictionary];
        _sharedInstance.firstPageDic = [NSMutableDictionary dictionary];
    });
    
    return _sharedInstance;
}


- (void)onRemoteUserEnterRoom:(NSString *)userId {
    ERTCUserInfo *user = [[ERTCUserInfo alloc] initWithUID:userId];
    if (userId.length > 0) {
        [self.remoterAccountDic setObject:user forKey:userId];
    }
}

- (void)onRemoteUserLeaveRoom:(NSString *)userId {
    ERTCUserInfo *leaveObj = [self.remoterAccountDic objectForKey:userId];
   
    if (leaveObj != nil) {
        [self.remoterAccountDic removeObjectForKey:userId];
        [[ERTCVideoMeetingManager sharedInstance].userVideoStateDic removeObjectForKey:userId];
    }
}

- (void)setLocalUserInfo:(ERTCUserInfo *)localUserInfo {
    _localUserInfo = localUserInfo;
    if (localUserInfo.uid.length > 0) {
        [self.remoterAccountDic setObject:localUserInfo forKey:localUserInfo.uid];
    }
}

- (ERTCUserInfo *)getMemberInfoByUserId:(NSString *)userId {
     ERTCUserInfo *info =  [self.remoterAccountDic objectForKey:userId];
    return info;
}

- (void)removeAllRemoteUser {
    
    [self.remoterAccountDic removeAllObjects];
    
}


- (NSArray<ERTCUserInfo *> *)getRemoteUserList {
    return [self.remoterAccountDic.allValues mutableCopy];
}


- (void)exitRoom {
    [self.userVideoStateDic removeAllObjects];
    [self.userNetLevelDic removeAllObjects];
    [self.remoterAccountDic removeAllObjects];
    [self.firstPageDic removeAllObjects];
}


- (void)cancelAudioModel {
    [self.remoterAccountDic enumerateKeysAndObjectsUsingBlock:^(NSString *key, ERTCUserInfo *obj, BOOL * _Nonnull stop) {
        NSNumber * boolNum = [[ERTCVideoMeetingManager sharedInstance].userVideoStateDic objectForKey:obj.uid];
        BOOL videoState = [boolNum boolValue];
        if (videoState) {
            obj.isVideoAvailable = YES;
        } else {
            obj.isVideoAvailable = NO;
        }
    }];
}

- (BOOL)isFirstPageDic:(NSString *)account {
    BOOL result = NO;
    for (NSString *userId in self.firstPageDic.allValues) {
        if ([userId isEqualToString:account]) {
            result = YES;
            break;
        }
    }
    return result;
}
@end
