//
//  ERTCOtherMembersContentView.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/9/16.
//

#import "ERTCOtherMembersContentView.h"
#import "ERTCOtherMemberContentView.h"
#import "ERTCPlayerViewManager.h"
#import "ERTCVideoMeetingManager.h"

@interface ERTCOtherMembersContentView ()<ERTCOtherMemberContentViewProtocol>
@property (nonatomic, strong) ERTCOtherMemberContentView *memberOneView;
@property (nonatomic, strong) ERTCOtherMemberContentView *memberTwoView;
@property (nonatomic, strong) ERTCOtherMemberContentView *memberThreeView;
@property (nonatomic, assign) int index;
@property (nonatomic, assign) CGPoint offset;
@end

@implementation ERTCOtherMembersContentView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpSubViews];
        
        self.playerViewModels = [NSMutableArray array];
    }
    
    return self;
}

#pragma mark - UI Method
-(void)setUpSubViews {
    ERTCOtherMemberContentView *memberView = [ERTCOtherMemberContentView new];
    memberView.hidden = YES;
    memberView.delegate = self;
    [self addSubview:memberView];
    self.memberOneView = memberView;
    
    memberView = [ERTCOtherMemberContentView new];
    memberView.hidden = YES;
    memberView.delegate = self;
    [self addSubview:memberView];
    self.memberTwoView = memberView;
    
    memberView = [ERTCOtherMemberContentView new];
    memberView.hidden = YES;
    memberView.delegate = self;
    [self addSubview:memberView];
    self.memberThreeView = memberView;
    
    [self.memberOneView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.width.equalTo(self.memberTwoView);
        make.width.equalTo(self.memberThreeView);
    }];
    
    [self.memberTwoView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.equalTo(self.memberOneView.ysmas_right).with.offset(10);
    }];
    
    [self.memberThreeView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self);
        make.left.equalTo(self.memberTwoView.ysmas_right).with.offset(10);
    }];
}
#pragma mark - UIContentContainer
-(void)viewWillTransitionToSize:(CGSize)size {

}

-(void)viewDidTransitionToSize:(CGSize)size {

}

- (ERTCUserInfo *)getFirstUser {
    return self.memberOneView.playInfoModel;
}

- (void)reloadData {
    if (self.memberOneView.playInfoModel) {
        [self.memberOneView onMemberInfoUpdate:self.memberOneView.playInfoModel];
    }
    
    if (self.memberTwoView.playInfoModel) {
        [self.memberTwoView onMemberInfoUpdate:self.memberTwoView.playInfoModel];
    }
    
    if (self.memberThreeView.playInfoModel) {
        [self.memberThreeView onMemberInfoUpdate:self.memberThreeView.playInfoModel];
    }
}

#pragma mark - EZSaaSCollectionViewFlowLayoutDelegate
-(void)notificationPageChange {
    BOOL previewBtn = NO;
    BOOL nextBtn = NO;
    
    if (self.memberOneView.playInfoModel) {
        NSUInteger oneIndex = [self.playerViewModels indexOfObject:self.memberOneView.playInfoModel];
        if (oneIndex != NSNotFound) {
            if (oneIndex == 0) {
                ERTCLogI(@"第一个小窗口是数组中第一个元素,隐藏前一页按钮");
                previewBtn = NO;
            } else {
                previewBtn = YES;
            }
        }
    } else {
        ERTCLogI(@"第一个小窗口不存在,隐藏前一页按钮");
        previewBtn = NO;
    }
    if (self.memberThreeView.playInfoModel) {
        NSUInteger threeIndex = [self.playerViewModels indexOfObject:self.memberThreeView.playInfoModel];
        if (threeIndex != NSNotFound) {
            if (threeIndex + 1 >= self.playerViewModels.count) {
                ERTCLogI(@"第三个小窗口是最后一个,隐藏下一页按钮");
                nextBtn = NO;
            }
            else
            {
                nextBtn = YES;
            }
        }
    } else {
        ERTCLogI(@"第三个小窗口不存在,隐藏下一页按钮");
        nextBtn = NO;
    }
    if ([self.delegate respondsToSelector:@selector(pageSwitchingStatus:canShowNextBtn:)]) {
        [self.delegate pageSwitchingStatus:previewBtn canShowNextBtn:nextBtn];
    }
}

-(BOOL)previewPage {
    BOOL result = NO;
    ERTCUserInfo *playInfoModel = self.memberOneView.playInfoModel;
    if (playInfoModel) {
        NSUInteger oneIndex = [self.playerViewModels indexOfObject:playInfoModel];
        ERTCLog(@"RTCOtherMembersContentView previewPage oneIndex:%lu", (unsigned long)oneIndex);
        if (oneIndex != NSNotFound) {
            NSUInteger index = oneIndex;
            if (index > 0) {
                result = YES;
                ERTCUserInfo *memberOneModel = self.memberOneView.playInfoModel;
                ERTCUserInfo *memberTwoModel = self.memberTwoView.playInfoModel;
                ERTCUserInfo *memberThreeModel = self.memberThreeView.playInfoModel;
                if (memberOneModel != nil) {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberOneModel.uid withStream:ERTCVideoStreamTypeSmall];
                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberOneModel.uid withRegionID:0];
                    ERTCLog(@"切换前一页---取消订阅小码流:%@",memberOneModel.uid);
                }
                if (memberTwoModel != nil) {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberTwoModel.uid withStream:ERTCVideoStreamTypeSmall];
                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberTwoModel.uid withRegionID:0];
                    ERTCLog(@"切换前一页---取消订阅小码流:%@",memberTwoModel.uid);
                }
                if (memberThreeModel != nil) {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberThreeModel.uid withStream:ERTCVideoStreamTypeSmall];
                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberThreeModel.uid withRegionID:0];
                    ERTCLog(@"切换前一页---取消订阅小码流:%@",memberThreeModel.uid);
                }
                ERTCLog(@"RTCOtherMembersContentView previewPage memberOneModel:%@ memberTwoModel:%@ memberThreeModel:%@", memberOneModel, memberTwoModel, memberThreeModel);
                self.memberOneView.playInfoModel = nil;
                self.memberTwoView.playInfoModel = nil;
                self.memberThreeView.playInfoModel = nil;
                [[ERTCVideoMeetingManager sharedInstance].firstPageDic removeAllObjects];
                if (index > 2) {
                    NSRange range = NSMakeRange(index - 3, 3);
                    NSArray<ERTCUserInfo *> *array = [self.playerViewModels subarrayWithRange:range];
                    for (int i = 0; i<array.count; i++) {
                        ERTCUserInfo *mode = array[i];
                        ERTCLog(@"RTCOtherMembersContentView previewPage 1 mode:%@ i:%d", mode, i);
                        switch (i) {
                            case 0:
                                [self switchStreamTrackWithMemberView:self.memberOneView currentModel:memberOneModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:firstView];
                                break;
                            case 1:
                                [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:memberTwoModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:secondtView];
                                break;
                            case 2:
                                [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:memberThreeModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:thirdView];
                                break;
                            default:
                                break;
                        }
                    }
                }
                else
                {
                    NSUInteger len = index + 1;
                    NSRange range = NSMakeRange(0, len);
                    NSArray<ERTCUserInfo *> *array = [self.playerViewModels subarrayWithRange:range];
                    for (int i = 0; i<array.count; i++) {
                        ERTCUserInfo *mode = array[i];
                        ERTCLog(@"RTCOtherMembersContentView previewPage 2 mode:%@ i:%d", mode, i);
                        switch (i) {
                            case 0:
                                [self switchStreamTrackWithMemberView:self.memberOneView currentModel:memberOneModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:firstView];
                                break;
                            case 1:
                                [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:memberTwoModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:secondtView];
                                break;
                            case 2:
                                [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:memberThreeModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:thirdView];
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
    }

    [self notificationPageChange];
    return result;
}

-(void)nextPage
{
    ERTCUserInfo *playInfoModel = self.memberThreeView.playInfoModel;
    ERTCLog(@"RTCOtherMembersContentView nextPage playInfoModel:%@", playInfoModel);
    if (playInfoModel) {
        NSUInteger threeIndex = [self.playerViewModels indexOfObject:playInfoModel];
        ERTCLog(@"RTCOtherMembersContentView nextPage threeIndex:%lu", (unsigned long)threeIndex);
        if (threeIndex != NSNotFound) {
            NSUInteger index = threeIndex + 1;
            NSUInteger count = self.playerViewModels.count;
            ERTCLog(@"RTCOtherMembersContentView nextPage index:%lu count:%lu", (unsigned long)index, (unsigned long)count);
            if (index < count) {
                ERTCUserInfo *memberOneModel = self.memberOneView.playInfoModel;
                ERTCUserInfo *memberTwoModel = self.memberTwoView.playInfoModel;
                ERTCUserInfo *memberThreeModel = self.memberThreeView.playInfoModel;
                if (memberOneModel != nil) {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberOneModel.uid withStream:ERTCVideoStreamTypeSmall];
                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberOneModel.uid withRegionID:0];
                    ERTCLog(@"切换后一页---取消订阅小码流:%@",memberOneModel.uid);
                }
                if (memberTwoModel != nil) {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberTwoModel.uid withStream:ERTCVideoStreamTypeSmall];
                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberTwoModel.uid withRegionID:0];
                    ERTCLog(@"切换后一页---取消订阅小码流:%@",memberTwoModel.uid);
                }
                if (memberThreeModel != nil) {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberThreeModel.uid withStream:ERTCVideoStreamTypeSmall];
                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberThreeModel.uid withRegionID:0];
                    ERTCLog(@"切换后一页---取消订阅小码流:%@",memberThreeModel.uid);
                }
                ERTCLog(@"RTCOtherMembersContentView nextPage memberOneModel:%@ memberTwoModel:%@ memberThreeModel:%@", memberOneModel, memberTwoModel, memberThreeModel);
                
                self.memberOneView.playInfoModel = nil;
                self.memberTwoView.playInfoModel = nil;
                self.memberThreeView.playInfoModel = nil;
                [[ERTCVideoMeetingManager sharedInstance].firstPageDic removeAllObjects];
                if (index + 2 < count) {
                    NSRange range = NSMakeRange(index, 3);
                    NSArray<ERTCUserInfo *> *array = [self.playerViewModels subarrayWithRange:range];
                    for (int i = 0; i<array.count; i++) {
                        ERTCUserInfo *mode = array[i];
                        ERTCLog(@"RTCOtherMembersContentView nextPage 1 mode:%@ i:%d", mode, i);
                        switch (i) {
                            case 0:
                                [self switchStreamTrackWithMemberView:self.memberOneView currentModel:memberOneModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:firstView];
                                break;
                            case 1:
                                [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:memberTwoModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:secondtView];
                                break;
                            case 2:
                                [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:memberThreeModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:thirdView];
                                break;
                            default:
                                break;
                        }
                    }
                } else {
                    NSUInteger len = count - index;
                    NSRange range = NSMakeRange(index, len);
                    NSArray<ERTCUserInfo *> *array = [self.playerViewModels subarrayWithRange:range];
                    for (int i = 0; i<array.count; i++) {
                        ERTCUserInfo *mode = array[i];
                        ERTCLog(@"RTCOtherMembersContentView nextPage 2 mode:%@ i:%d", mode, i);
                        switch (i) {
                            case 0:
                                [self switchStreamTrackWithMemberView:self.memberOneView currentModel:memberOneModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:firstView];
                                break;
                            case 1:
                                [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:memberTwoModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:secondtView];
                                break;
                            case 2:
                                [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:memberThreeModel byModel:mode];
                                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:thirdView];
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
    }

    [self notificationPageChange];
}

///接收码流
-(void)switchStreamTrackWithMemberView:(ERTCOtherMemberContentView *)memberView
                          currentModel:(ERTCUserInfo *)currentModel
                               byModel:(ERTCUserInfo *)model {
    if (currentModel != model) {
        ERTCLog(@"更新前的用户:%@ 更新后的用户:%@",memberView.playInfoModel.uid,model.uid);
        memberView.playInfoModel = model;
        [memberView willDisplayCell];
    }
}

#pragma mark -decode
- (void)onDisplay:(ERTCUserInfo *)notifyModel {
    if ([self.memberOneView.playInfoModel.uid isEqualToString:notifyModel.uid]) {
        [self.memberOneView onDisPlay];
    } else if ([self.memberTwoView.playInfoModel.uid isEqualToString:notifyModel.uid]) {
        [self.memberTwoView onDisPlay];
    } else if ([self.memberThreeView.playInfoModel.uid isEqualToString:notifyModel.uid]) {
        [self.memberThreeView onDisPlay];
    }
}

#pragma mark - Member Event
-(void)onMemberInfoUpdate:(ERTCUserInfo *)notifyModel {
    __block BOOL isExist = NO;
    [self.playerViewModels enumerateObjectsUsingBlock:^(ERTCUserInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.uid isEqualToString:notifyModel.uid]) {
//            obj.account = notifyModel;
            isExist = YES;
            *stop = YES;
        }
    }];
    if (isExist) {
        [self displayCellsUpdateModel:notifyModel];
    }
    
}

-(void)displayCellsUpdateModel:(ERTCUserInfo *)updateModel {
    if (updateModel) {
        if ([self.memberOneView.playInfoModel.uid isEqualToString:updateModel.uid]) {
            self.memberOneView.playInfoModel = updateModel;
        }
        else if ([self.memberTwoView.playInfoModel.uid isEqualToString:updateModel.uid]) {
            self.memberTwoView.playInfoModel = updateModel;
        }
        else if ([self.memberThreeView.playInfoModel.uid isEqualToString:updateModel.uid]) {
            self.memberThreeView.playInfoModel = updateModel;
        }
    }
}

- (void)startAudioModel {
    ERTCUserInfo *memberOneModel = self.memberOneView.playInfoModel;
    ERTCUserInfo *memberTwoModel = self.memberTwoView.playInfoModel;
    ERTCUserInfo *memberThreeModel = self.memberThreeView.playInfoModel;
    if (memberOneModel != nil) {
        memberOneModel.isVideoAvailable = NO;
        [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberOneModel.uid withStream:ERTCVideoStreamTypeSmall];
        [self onMemberInfoUpdate:self.memberOneView.playInfoModel];
    }
    if (memberTwoModel != nil) {
        memberTwoModel.isVideoAvailable = NO;
        [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberTwoModel.uid withStream:ERTCVideoStreamTypeSmall];
        [self onMemberInfoUpdate:self.memberTwoView.playInfoModel];
    }
    if (memberThreeModel != nil) {
        memberThreeModel.isVideoAvailable = NO;
        [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberThreeModel.uid withStream:ERTCVideoStreamTypeSmall];
        [self onMemberInfoUpdate:self.memberThreeView.playInfoModel];
    }
}

- (void)cancelAudioModel {
    ERTCUserInfo *memberOneModel = self.memberOneView.playInfoModel;
    ERTCUserInfo *memberTwoModel = self.memberTwoView.playInfoModel;
    ERTCUserInfo *memberThreeModel = self.memberThreeView.playInfoModel;
    self.memberOneView.playInfoModel = nil;
    self.memberTwoView.playInfoModel = nil;
    self.memberThreeView.playInfoModel = nil;
    [[ERTCVideoMeetingManager sharedInstance].firstPageDic removeAllObjects];
    [[ERTCVideoMeetingManager sharedInstance] cancelAudioModel];
    NSArray *remoteArray = [[ERTCVideoMeetingManager sharedInstance] getRemoteUserList];
    ERTCLogI(@"取消语音模式:%ld",remoteArray.count);
    [remoteArray enumerateObjectsUsingBlock:^(ERTCUserInfo *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ERTCUserInfo *model = [[ERTCUserInfo alloc] initWithUID:obj.uid];
        model.isVideoAvailable = obj.isVideoAvailable;
        model.type = ERTCPlayerViewType_Other;
        if ([memberOneModel.uid isEqualToString:model.uid]) {
            [self switchStreamTrackWithMemberView:self.memberOneView currentModel:nil byModel:model];
            [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:model.uid forKey:firstView];
            [self onMemberInfoUpdate:model];
        }
        else if ([memberTwoModel.uid isEqualToString:model.uid]) {
            [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:nil byModel:model];
            [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:model.uid forKey:secondtView];
            [self onMemberInfoUpdate:model];
        }
        else if ([memberThreeModel.uid isEqualToString:model.uid]) {
            [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:nil byModel:model];
            [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:model.uid forKey:thirdView];
            [self onMemberInfoUpdate:model];
        }
    }];
}

-(void)onMemberEnterRoom:(ERTCUserInfo *)notifyModel {
    __block ERTCUserInfo *enterModel = nil;
    //处理同一个人多次入会消息
    [self.playerViewModels enumerateObjectsUsingBlock:^(ERTCUserInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.uid isEqualToString:notifyModel.uid]) {
            enterModel = obj;
            *stop = YES;
        }
    }];
    
    if (!enterModel) {
        ERTCUserInfo *model = [[ERTCUserInfo alloc] initWithUID:notifyModel.uid];
        model.type = ERTCPlayerViewType_Other;
        [self.playerViewModels addObject:model];
        if (self.memberOneView.playInfoModel == nil) {
            ERTCLog(@"展示第一个窗口");
            [self switchStreamTrackWithMemberView:self.memberOneView currentModel:nil byModel:model];
            [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:model.uid forKey:firstView];
        }
        else if (self.memberTwoView.playInfoModel == nil) {
            ERTCLog(@"展示第二个窗口");
            [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:nil byModel:model];
            [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:model.uid forKey:secondtView];
        }
        else if (self.memberThreeView.playInfoModel == nil) {
            ERTCLog(@"展示第三个窗口");
            [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:nil byModel:model];
            [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:model.uid forKey:thirdView];
        }
        else
        {
            
        }
    } else {
        ERTCLog(@"异常----房间内用户重复进入");
    }
    
    [self notificationPageChange];
}

-(void)onMemberExitRoom:(ERTCUserInfo *)notifyModel {
    if (self.playerViewModels.count > 0) {
        __block ERTCUserInfo *removeModel = nil;
        [self.playerViewModels enumerateObjectsUsingBlock:^(ERTCUserInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.uid isEqualToString:notifyModel.uid]) {
                removeModel = obj;
                *stop = YES;
            }
        }];
        
        if (removeModel) {
            ERTCUserInfo *memberOneModel = self.memberOneView.playInfoModel;
            ERTCUserInfo *memberTwoModel = self.memberTwoView.playInfoModel;
            ERTCUserInfo *memberThreeModel = self.memberThreeView.playInfoModel;
            
            ERTCLog(@"RTCOtherMembersContentView onMemberExitRoom memberOneModel:%@ memberTwoModel:%@ memberThreeModel:%@ removeModel:%@", memberOneModel, memberTwoModel, memberThreeModel, removeModel);
            
            ERTCOtherMemberContentView *memberView = nil;
            if (self.memberOneView.playInfoModel == removeModel) {
                self.memberOneView.playInfoModel = nil;
                memberView = self.memberOneView;
                [[ERTCVideoMeetingManager sharedInstance].firstPageDic removeObjectForKey:firstView];
            } else if (self.memberTwoView.playInfoModel == removeModel) {
                self.memberTwoView.playInfoModel = nil;
                memberView = self.memberTwoView;
                [[ERTCVideoMeetingManager sharedInstance].firstPageDic removeObjectForKey:secondtView];
            } else if (self.memberThreeView.playInfoModel == removeModel) {
                self.memberThreeView.playInfoModel = nil;
                memberView = self.memberThreeView;
                [[ERTCVideoMeetingManager sharedInstance].firstPageDic removeObjectForKey:thirdView];
            }
            if (memberView) {
                [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:removeModel.uid withStream:ERTCVideoStreamTypeSmall];
                [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:removeModel.uid withRegionID:0];
                ERTCLog(@"取消订阅小码流:%@",removeModel.uid);
                [memberView didEndDisplayingCell];
            }
            
            [self updateMembersViewByDisplayMembersExitByMemberView:memberView memberOneModel:memberOneModel memberTwoModel:memberTwoModel memberThreeModel:memberThreeModel exitModel:removeModel];
        } else {
            ERTCLog(@"onMemberExitRoom--未找到房间内存在的用户");
        }
    }
    
    [self notificationPageChange];
    if (self.playerViewModels.count == 0) {
        ERTCLog(@"RTCOtherMembersContentView onMemberExitRoom 异常处理");
        self.memberOneView.playInfoModel = nil;
        self.memberTwoView.playInfoModel = nil;
        self.memberThreeView.playInfoModel = nil;
    }
}

-(void)updateMembersViewByDisplayMembersExitByMemberView:(ERTCOtherMemberContentView *)memberView
                                          memberOneModel:(ERTCUserInfo *)memberOneModel
                                          memberTwoModel:(ERTCUserInfo *)memberTwoModel
                                          memberThreeModel:(ERTCUserInfo *)memberThreeModel
                                               exitModel:(ERTCUserInfo *)exitModel {
    NSUInteger oneIndex = [self.playerViewModels indexOfObject:memberOneModel];
    NSUInteger exitIndex = [self.playerViewModels indexOfObject:exitModel];
    
    if (memberView) {
        //如果离开的人是在当前显示的三个人中
        if (self.memberOneView.playInfoModel == nil) {
            if (self.memberTwoView.playInfoModel == nil) {
                //第一个、第二个小图都是空，说明当前页面已经没有人了，之前调用前一页方法
                self.memberOneView.playInfoModel = memberOneModel;
                [self previewPage];
            } else {
                [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:self.memberTwoView.playInfoModel.uid withRegionID:0];
                [self switchStreamTrackWithMemberView:self.memberOneView currentModel:nil byModel:self.memberTwoView.playInfoModel];
                [self setBigWindow:self.memberTwoView.playInfoModel];
                if (self.memberThreeView.playInfoModel == nil) {
                    self.memberTwoView.playInfoModel = nil;
                } else {
                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:self.memberThreeView.playInfoModel.uid withRegionID:0];
                    [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:nil byModel:self.memberThreeView.playInfoModel];
                    [self setBigWindow:self.memberThreeView.playInfoModel];
                    NSUInteger threeIndex = [self.playerViewModels indexOfObject:self.memberThreeView.playInfoModel];
                    if (threeIndex != NSNotFound) {
                        if (threeIndex + 1 >= self.playerViewModels.count) {
                            self.memberThreeView.playInfoModel = nil;
                        } else {
                            ERTCUserInfo *playInfoModel = self.playerViewModels[threeIndex + 1];
                            [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:nil byModel:playInfoModel];
                            [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:playInfoModel.uid forKey:thirdView];
                        }
                    }
                }
            }
        }else if (self.memberTwoView.playInfoModel == nil) {
            if (self.memberThreeView.playInfoModel == nil) {

            }
            else
            {
                [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:self.memberThreeView.playInfoModel.uid withRegionID:0];
                [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:nil byModel:self.memberThreeView.playInfoModel];
                [self setBigWindow:self.memberThreeView.playInfoModel];
                [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:self.memberThreeView.playInfoModel.uid forKey:secondtView];
                NSUInteger threeIndex = [self.playerViewModels indexOfObject:self.memberThreeView.playInfoModel];
                if (threeIndex != NSNotFound) {
                    if (threeIndex + 1 >= self.playerViewModels.count) {
                        self.memberThreeView.playInfoModel = nil;
                    }
                    else
                    {
                        ERTCUserInfo *playInfoModel = self.playerViewModels[threeIndex + 1];
                        [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:nil byModel:playInfoModel];
                        [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:playInfoModel.uid forKey:thirdView];
                    }
                }
            }
        }
        else if (self.memberThreeView.playInfoModel == nil) {
            NSUInteger threeIndex = [self.playerViewModels indexOfObject:memberThreeModel];
            if (threeIndex != NSNotFound) {
                if (threeIndex + 1 >= self.playerViewModels.count) {
                    
                }
                else
                {
                    ERTCUserInfo *playInfoModel = self.playerViewModels[threeIndex + 1];
                    [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:nil byModel:playInfoModel];
                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:playInfoModel.uid forKey:thirdView];
                }
            }
        }
        [self.playerViewModels removeObject:exitModel];
    } else {
        [self.playerViewModels removeObject:exitModel];
        //如果离开的人不在当前显示的三个人中
        if (oneIndex != NSNotFound && exitIndex != NSNotFound) {
            if (exitIndex < oneIndex) {
                //离开的人在当前页面之前
                oneIndex = [self.playerViewModels indexOfObject:memberOneModel];
                NSUInteger index = oneIndex + 1;
                if (index == self.playerViewModels.count) {
                    //当前页面只有一个小图
                    if (index > 2) {
                        ERTCUserInfo *memberOneModel = self.memberOneView.playInfoModel;
                        if (memberOneModel != nil) {
                            [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberOneModel.uid withStream:ERTCVideoStreamTypeSmall];
                            [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberOneModel.uid withRegionID:0];
                            ERTCLogI(@"当前页面用户往前移动----小窗口重置RemoteView:%@",memberOneModel.uid);
                        }
                        [self resetBigWindow];
                        NSRange range = NSMakeRange(index - 3, 3);
                        NSArray<ERTCUserInfo *> *array = [self.playerViewModels subarrayWithRange:range];
                        for (int i = 0; i<array.count; i++) {
                            ERTCUserInfo *mode = array[i];
                            switch (i) {
                                case 0:
                                    [self switchStreamTrackWithMemberView:self.memberOneView currentModel:nil byModel:mode];
                                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:firstView];
                                    break;
                                case 1:
                                    [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:nil byModel:mode];
                                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:secondtView];
                                    break;
                                case 2:
                                    [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:nil byModel:mode];
                                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:thirdView];
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    else
                    {
                        [self previewPage];
                    }
                }
                else
                {
                    ERTCUserInfo *memberOneModel = self.memberOneView.playInfoModel;
                    ERTCUserInfo *memberTwoModel = self.memberTwoView.playInfoModel;
                    ERTCUserInfo *memberThreeModel = self.memberThreeView.playInfoModel;
                    if (memberOneModel != nil) {
                        [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberOneModel.uid withStream:ERTCVideoStreamTypeSmall];
                        [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberOneModel.uid withRegionID:0];
                        ERTCLogI(@"当前页面用户往前移动3----小窗口重置RemoteView:%@",memberOneModel.uid);
                    }
                    if (memberTwoModel != nil) {
                        [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberTwoModel.uid withStream:ERTCVideoStreamTypeSmall];
                        [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberTwoModel.uid withRegionID:0];
                        ERTCLogI(@"当前页面用户往前移动3----小窗口重置RemoteView:%@",memberTwoModel.uid);
                    }
                    if (memberThreeModel != nil) {
                        [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:memberThreeModel.uid withStream:ERTCVideoStreamTypeSmall];
                        [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberThreeModel.uid withRegionID:0];
                        ERTCLogI(@"当前页面用户往前移动3----小窗口重置RemoteView:%@",memberThreeModel.uid);
                    }
                    [self resetBigWindow];
                    self.memberOneView.playInfoModel = nil;
                    self.memberTwoView.playInfoModel = nil;
                    self.memberThreeView.playInfoModel = nil;
                    
                    NSUInteger count = self.playerViewModels.count;
                    if (index + 2 < count) {
                        NSRange range = NSMakeRange(index, 3);
                        NSArray<ERTCUserInfo *> *array = [self.playerViewModels subarrayWithRange:range];
                        for (int i = 0; i<array.count; i++) {
                            ERTCUserInfo *mode = array[i];
                            switch (i) {
                                case 0:
                                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberOneModel.uid withRegionID:0];
                                    ERTCLogI(@"当前页面用户往前移动2----小窗口重置RemoteView:%@",mode.uid);
                                    [self switchStreamTrackWithMemberView:self.memberOneView currentModel:memberOneModel byModel:mode];
                                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:firstView];
                                    break;
                                case 1:
                                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberTwoModel.uid withRegionID:0];
                                    ERTCLogI(@"当前页面用户往前移动2----小窗口重置RemoteView:%@",mode.uid);
                                    [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:memberTwoModel byModel:mode];
                                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:secondtView];
                                    break;
                                case 2:
                                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberThreeModel.uid withRegionID:0];
                                    ERTCLogI(@"当前页面用户往前移动2----小窗口重置RemoteView:%@",mode.uid);
                                    [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:memberThreeModel byModel:mode];
                                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:thirdView];
                                    break;
                                default:
                                    break;
                            }
                        }
                    } else {
                        NSUInteger len = count - index;
                        NSRange range = NSMakeRange(index, len);
                        NSArray<ERTCUserInfo *> *array = [self.playerViewModels subarrayWithRange:range];
                        for (int i = 0; i<array.count; i++) {
                            ERTCUserInfo *mode = array[i];
                            switch (i) {
                                case 0:
                                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberOneModel.uid withRegionID:0];
                                    ERTCLogI(@"当前页面用户往前移动2----小窗口重置RemoteView:%@",mode.uid);
                                    [self switchStreamTrackWithMemberView:self.memberOneView currentModel:memberOneModel byModel:mode];
                                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:firstView];
                                    break;
                                case 1:
                                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberTwoModel.uid withRegionID:0];
                                    ERTCLogI(@"当前页面用户往前移动2----小窗口重置RemoteView:%@",mode.uid);
                                    [self switchStreamTrackWithMemberView:self.memberTwoView currentModel:memberTwoModel byModel:mode];
                                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:secondtView];
                                    break;
                                case 2:
                                    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:memberThreeModel.uid withRegionID:0];
                                    ERTCLogI(@"当前页面用户往前移动2----小窗口重置RemoteView:%@",mode.uid);
                                    [self switchStreamTrackWithMemberView:self.memberThreeView currentModel:memberThreeModel byModel:mode];
                                    [[ERTCVideoMeetingManager sharedInstance].firstPageDic setObject:mode.uid forKey:thirdView];
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
            } else
            {
                //离开的人在当前页面之后，不做处理
            }
        }
        else
        {
            //异常处理，直接上一页
            if([self previewPage] == NO)
            {
                [self nextPage];
            }
        }
    }
}

- (void)resetBigWindow {
    if ([self.delegate respondsToSelector:@selector(resetBigWindow)]) {
        [self.delegate resetBigWindow];
    }
}

-(void)setBigWindow:(ERTCUserInfo *)model {
    if ([self.delegate respondsToSelector:@selector(setBigWindow:)]) {
        [self.delegate setBigWindow:model];
    }
}

#pragma mark - RTCOtherMemberContentViewProtocol
-(void)otherMemberContentViewClicked:(ERTCOtherMemberContentView *)contentView {
    ERTCUserInfo *model = contentView.playInfoModel;
    if ([self.delegate respondsToSelector:@selector(otherMembersContentViewClickWithModel:)]) {
        [self.delegate otherMembersContentViewClickWithModel:model];
    }
}


@end
