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

#import "ERTCOtherMemberContentView.h"
#import "ERTCSmallPlayerStatusView.h"
#import "ERTCPlayerViewManager.h"
#import "EZSaaSStreamLoadingView.h"

@interface ERTCOtherMemberContentView ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *playerView;
@property (nonatomic, strong) ERTCSmallPlayerStatusView *statusView;
@property (nonatomic, strong) EZSaaSStreamLoadingView *loadingView;

@end

@implementation ERTCOtherMemberContentView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.clipsToBounds = YES;
        
        self.bgView = [UIView new];
        self.bgView.backgroundColor = [UIColor clearColor];
        
        [self addSubview:self.bgView];
       
        
        [self.bgView addSubview:self.statusView];
        
        [self.bgView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.left.right.equalTo(self);
        }];
        
        [self.statusView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.edges.equalTo(self.bgView);
        }];
        
        [self.bgView addSubview:self.loadingView];
        [self.loadingView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMine)];
        [self addGestureRecognizer:tapGes];
    }
    
    return self;
}

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

-(void)willDisplayCell {
    [self didEndDisplayingCell];
    self.playerView = [UIView new];
    self.playerView.backgroundColor = [UIColor blackColor];
    [self.bgView insertSubview: self.playerView belowSubview:self.statusView];
    [self.playerView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.edges.equalTo(self.bgView);
    }];
    NSNumber * boolNum = [[ERTCVideoMeetingManager sharedInstance].subscribeStateDic objectForKey:_playInfoModel.uid];
    BOOL subscribeState = [boolNum boolValue];
    if (!subscribeState) {
        [[ERTCPlayerViewManager sharedInstance].engine subscribe:YES forUser:_playInfoModel.uid withStream:ERTCVideoStreamTypeSmall];
        ERTCLog(@"订阅小码流:%@",_playInfoModel.uid);
    } else {
        [[ERTCPlayerViewManager sharedInstance].engine subscribe:YES forUser:_playInfoModel.uid withStream:ERTCVideoStreamTypeSmall];
        ERTCLog(@"已经订阅大码流---切换为小码流:%@",_playInfoModel.uid);
    }
   
    [[ERTCPlayerViewManager sharedInstance].engine setRemoteView:self.playerView forUser:_playInfoModel.uid withRegionID:0];
    [self.loadingView beginLoadingView];
    
}

-(void)didEndDisplayingCell {
    if (self.playerView != nil && _playInfoModel.uid.length > 0) {
        [self.playerView removeFromSuperview];
        self.playerView = nil;
        
    }
    [self.loadingView endLoadingView];
}

-(void)onMemberInfoUpdate:(ERTCUserInfo *)notifyModel {
    [self.statusView onMemberInfoUpdate:notifyModel isSmall:YES];
    [self.loadingView onMemberInfoUpdate:notifyModel isSmall:YES];
}

- (void)setPlayInfoModel:(ERTCUserInfo *)playInfoModel {
    if (playInfoModel == nil) {
        self.hidden = YES;

    } else {
        self.hidden = NO;
       
    }
    _playInfoModel = playInfoModel;
    [self.statusView onMemberInfoUpdate:playInfoModel isSmall:YES];

    self.loadingView.ti = 1;
    [self.loadingView onMemberInfoUpdate:_playInfoModel isSmall:YES];
}

- (void)onDisPlay {
    [self.loadingView updateLoadingView];
}

#pragma mark - UI Getter Method
-(ERTCSmallPlayerStatusView *)statusView {
    if (!_statusView) {
        _statusView = [ERTCSmallPlayerStatusView new];
    }
    
    return _statusView;
}

-(EZSaaSStreamLoadingView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[EZSaaSStreamLoadingView alloc] init];
    }

    return _loadingView;
}

-(void)dealloc {
    ERTCLog(@"ERTCOtherMemberContentView dealloc");
    [self.loadingView endLoadingView];
}

@end
