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

#import "ERTCBigPlayerView.h"
#import "ERTCSmallPlayerStatusView.h"
#import "ERTCPlayerViewManager.h"

#define INFOVIEW_WIDTH 108

@interface ERTCBigPlayerView ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *playerView;
@property (nonatomic, strong) ERTCSmallPlayerStatusView *playerStatusView;

@end

@implementation ERTCBigPlayerView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        self.playerView = [UIView new];
        self.playerView.backgroundColor = [UIColor blackColor];
        [self addSubview:self.playerView];
        [self.playerView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        [self addSubview:self.playerStatusView];
//        [self showTopBottomView];
//        [self addSubview:self.loadingView];
//        [self.loadingView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
//            make.edges.equalTo(self);
//        }];
//        self.loadingView.hidden = YES;
    }
    
    return self;
}

-(void)setTopView:(UIView *)topView {
    _topView = topView;
    [self.playerStatusView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.playerStatusView.topView = topView;
}

-(void)hiddenTopBottomView {
    [self.playerStatusView hiddenTopBottomView];
}

-(void)showTopBottomView {
    [self.playerStatusView showTopBottomView];
}

-(void)reloadCurrentView {
    [self onMemberInfoUpdate:self.playInfoModel];
}

-(void)setPlayInfoModel:(ERTCUserInfo *)playInfoModel {
    if (_playInfoModel != nil) {
        if ([_playInfoModel.uid isEqualToString:playInfoModel.uid]) {
            return;
        }
        
        //清除之前的视图
        if (playInfoModel.type == ERTCPlayerViewType_Mine) {
            //从别的用户切换到本地用户
            [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:nil withRegionID:1];
            [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:_playInfoModel.uid withRegionID:1];
            if ([[ERTCVideoMeetingManager sharedInstance] isFirstPageDic:_playInfoModel.uid]) {
                [[ERTCPlayerViewManager sharedInstance].engine subscribe:YES forUser:_playInfoModel.uid withStream:ERTCVideoStreamTypeSmall];
                ERTCLog(@"取消订阅大码流---切换为小码流:%@",_playInfoModel.uid);
            } else {
                [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:_playInfoModel.uid withStream:ERTCVideoStreamTypeBig];
                ERTCLog(@"取消订阅大码流:%@",_playInfoModel.uid);
            }
        } else {
            if (_playInfoModel.type == ERTCPlayerViewType_Mine) {
                //从本地用户切换到别人
                [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:nil withRegionID:1];
            } else {
                //从别的用户切换到另一个用户
                [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:nil forUser:_playInfoModel.uid withRegionID:1];
                if ([[ERTCVideoMeetingManager sharedInstance] isFirstPageDic:_playInfoModel.uid]) {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:YES forUser:_playInfoModel.uid withStream:ERTCVideoStreamTypeSmall];
                    ERTCLog(@"取消订阅大码流---切换为小码流:%@",_playInfoModel.uid);
                } else {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:NO forUser:_playInfoModel.uid withStream:ERTCVideoStreamTypeBig];
                    NSNumber *stateNumber = [NSNumber numberWithBool:NO];
                   [[ERTCVideoMeetingManager sharedInstance].subscribeStateDic setObject:stateNumber forKey:_playInfoModel.uid];
                    ERTCLog(@"取消订阅大码流:%@",_playInfoModel.uid);
                }
            }
        }
    }
//    if (playInfoModel.isSharingAccount == YES) {
//        self.userInteractionEnabled = YES;
//
//        NSInteger time = [[EZSaaSVideoMeetingDebugManager sharedInstance].screenStreamLoadingTime integerValue];
//        time = time > 0 ? time : 3;
//        self.loadingView.ti = time;
//    } else {
//        self.userInteractionEnabled = NO;
//
//        NSInteger time = [[EZSaaSVideoMeetingDebugManager sharedInstance].videoStreamLoadingTime integerValue];
//        time = time > 0 ? time : 1;
//        self.loadingView.ti = time;
//    }
    if (playInfoModel) {
            _playInfoModel = playInfoModel;
        if (_playInfoModel.type == ERTCPlayerViewType_Other) {
//                [self.loadingView beginLoadingView];
                if (![[ERTCVideoMeetingManager sharedInstance] isFirstPageDic:_playInfoModel.uid]) {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:YES forUser:_playInfoModel.uid withStream:ERTCVideoStreamTypeBig];
                    NSNumber *stateNumber = [NSNumber numberWithBool:YES];
                   [[ERTCVideoMeetingManager sharedInstance].subscribeStateDic setObject:stateNumber forKey:_playInfoModel.uid];
                    ERTCLog(@"订阅大码流---:%@",playInfoModel.uid);
                } else {
                    [[ERTCPlayerViewManager sharedInstance].engine subscribe:YES forUser:_playInfoModel.uid withStream:ERTCVideoStreamTypeBig];
                    ERTCLog(@"切换大码流---:%@",playInfoModel.uid);
                }
                [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:self.playerView forUser:playInfoModel.uid withRegionID:1];
               
            } else {
                [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:self.playerView withRegionID:1];
//                [self.loadingView endLoadingView];
            }
        [self onMemberInfoUpdate:playInfoModel];
    } else {
//        [self.loadingView endLoadingView];
        self.playerStatusView.isScreenSharing = NO;
        _playInfoModel = playInfoModel;
    }
}

#pragma mark -屏幕旋转
-(void)viewWillTransitionToSize:(CGSize)size {
    if (_playInfoModel.type == ERTCPlayerViewType_Mine) {
        if (size.width > size.height) {
            //横屏
            [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:nil withRegionID:1];
            self.frame = CGRectMake(0, 0, size.width, size.height);
            [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:self.playerView withRegionID:1];
        } else {
            //竖屏
            [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:nil withRegionID:1];
            self.frame = CGRectMake(0, 0, size.width, size.height);
            [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:self.playerView withRegionID:1];
        }
    }
}


-(void)onMemberInfoUpdate:(ERTCUserInfo *)notifyModel {
    if (notifyModel && [notifyModel.uid isEqualToString:self.playInfoModel.uid]) {
        if (self.playInfoModel.type == ERTCPlayerViewType_Mine) {
            if (self.playInfoModel.isVideoAvailable && !notifyModel.isVideoAvailable) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:nil withRegionID:1];
                });
            }
            
        }
        self.playInfoModel = notifyModel;
        [self.playerStatusView onMemberInfoUpdate:notifyModel isSmall:NO];
//        [self.loadingView onMemberInfoWithSharingUpdate:notifyModel isSharing:self.playInfoModel.isSharingAccount isSmall:NO];
    }
}

#pragma mark - 单击手势处理
- (void)handleSingleTapGestureRecognizer:(UIPanGestureRecognizer *)sender {
//    [self.vcsPlayerView tapView:sender];
}

#pragma mark - UI Getter Method
-(ERTCSmallPlayerStatusView *)playerStatusView {
    if (!_playerStatusView) {
        _playerStatusView = [ERTCSmallPlayerStatusView new];
        _playerStatusView.userInteractionEnabled = NO;
    }
    
    return _playerStatusView;
}

#pragma mark - dealloc
-(void)dealloc {
//    [self.loadingView endLoadingView];
    ERTCLog(@"ERTCBigPlayerView dealloc");
}



@end
