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

#import "ERTCSmallPlayerStatusView.h"
#import <ERTC/ERTCDef.h>
#define INFOVIEW_WIDTH 108

@interface ERTCSmallPlayerStatusView ()
@property (nonatomic, strong) UIView *videoStatusBGView;

/* -------------------------------------------- */
//底部状态
@property (nonatomic, strong) UIView *statusBGView;
@property (nonatomic, strong) UIImageView *audioImageView;
@property (nonatomic, strong) UILabel *nickNameLabel;
@property (nonatomic, strong) UIImageView *signImageView;

/* -------------------------------------------- */
//关闭视频后，中间的状态
@property (nonatomic, strong) UIView *centerAudioInfoView;

//中间的头像
@property (nonatomic, strong) UIImageView *centerAudioHeaderImageView;
@property (nonatomic, strong) UILabel *centerHeaderImageViewNameLabel;
//中间的昵称
@property (nonatomic, strong) UILabel *centerAudioNameLabel;
//中间的麦克风、信号图标
@property (nonatomic, strong) UIImageView *centerAudioMircImageView;
@property (nonatomic, strong) UIImageView *centerAudioSignImageView;

/* -------------------------------------------- */
//右上角状态
@property (nonatomic, strong) UIView *topInfoView;
@property (nonatomic, strong) UILabel *topNameLabel;
@property (nonatomic, strong) UIImageView *topSignImageView;
@property (nonatomic, strong) UIImageView *topVideoMircImageView;
@end

@implementation ERTCSmallPlayerStatusView
-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        [self addSubview:self.videoStatusBGView];
        
        //底部状态
        [self addSubview:self.statusBGView];
        [self.statusBGView addSubview:self.audioImageView];
        [self.statusBGView addSubview:self.nickNameLabel];
        [self.statusBGView addSubview:self.signImageView];
    
        
        //中间状态（视频关闭）
        [self addSubview:self.centerAudioHeaderImageView];
        
        [self addSubview:self.centerAudioInfoView];
        [self.centerAudioInfoView addSubview:self.centerAudioNameLabel];
        [self.centerAudioInfoView addSubview:self.centerAudioMircImageView];
        [self.centerAudioInfoView addSubview:self.centerAudioSignImageView];
        
        //顶部状态
        [self addSubview:self.topInfoView];
        [self.topInfoView addSubview:self.topNameLabel];
        [self.topInfoView addSubview:self.topSignImageView];
        [self.topInfoView addSubview:self.topVideoMircImageView];

        
        [self.videoStatusBGView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        //底部状态图标
        [self.statusBGView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.ysmas_equalTo(22);
        }];
        
        [self.audioImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.equalTo(self.statusBGView).with.offset(2);
            make.centerY.equalTo(self.statusBGView);
            make.right.equalTo(self.nickNameLabel.ysmas_left);
            make.width.height.ysmas_equalTo(20);
        }];
        
        [self.nickNameLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.center.equalTo(self.statusBGView);
        }];
        
        [self.signImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.right.centerY.equalTo(self.statusBGView).with.offset(-2);
            make.left.equalTo(self.nickNameLabel.ysmas_right);
            make.width.height.ysmas_equalTo(16);
        }];
        
        [self.signImageView setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.audioImageView setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.signImageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.audioImageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        
        //中间头像
        [self.centerAudioHeaderImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.center.equalTo(self);
            make.width.height.ysmas_equalTo(80);
        }];
        
        self.centerAudioHeaderImageView.layer.cornerRadius = 40;
        self.centerAudioHeaderImageView.clipsToBounds = YES;
        self.centerAudioHeaderImageView.hidden = YES;
        
        [self.centerAudioInfoView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.greaterThanOrEqualTo(kSafeMasLeft(self)).with.offset(5);
            make.right.lessThanOrEqualTo(kSafeMasRight(self)).with.offset(-5);
            make.top.equalTo(self.centerAudioHeaderImageView.ysmas_bottom).with.offset(11);
            make.centerX.equalTo(self);
        }];
        
        [self.centerAudioNameLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self.centerAudioInfoView);
        }];
        
        [self.centerAudioSignImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.equalTo(self.centerAudioNameLabel.ysmas_right).with.offset(3);
            make.centerY.equalTo(self.centerAudioInfoView);
            make.width.height.ysmas_equalTo(16);
        }];
        
        [self.centerAudioMircImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.equalTo(self.centerAudioSignImageView.ysmas_right).with.offset(3);
            make.right.equalTo(self.centerAudioInfoView);
            make.centerY.equalTo(self.centerAudioInfoView);
            make.width.height.ysmas_equalTo(20);
        }];
        
        [self.centerAudioSignImageView setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.centerAudioMircImageView setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.centerAudioSignImageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.centerAudioMircImageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        
        //顶部的音频、信号、昵称
        [self.topNameLabel ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
            make.left.equalTo(self.topInfoView).with.offset(3);
            make.right.equalTo(self.topSignImageView.ysmas_left).with.offset(-3);
            make.centerY.equalTo(self.topInfoView);
        }];
        
        [self.topSignImageView ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
            make.right.equalTo(self.topVideoMircImageView.ysmas_left).with.offset(-5);
            make.centerY.equalTo(self.topInfoView);
            make.width.height.ysmas_equalTo(16);
        }];
        
        [self.topVideoMircImageView ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
            make.right.equalTo(self.topInfoView).with.offset(-3);
            make.centerY.equalTo(self.topInfoView);
            make.width.height.ysmas_equalTo(20);
        }];
        
        [self.topSignImageView setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.topVideoMircImageView setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.topSignImageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.topVideoMircImageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        self.clipsToBounds = YES;
    }
    
    return self;
}

-(void)hiddenTopBottomView
{
    [self.topInfoView ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
        make.width.ysmas_equalTo(INFOVIEW_WIDTH);
        make.height.ysmas_equalTo(35);
        make.right.equalTo(self).with.offset(-13.5);
        make.top.equalTo(kSafeMasTop(self)).with.offset(7.5);
    }];
}

-(void)showTopBottomView
{
    [self.topInfoView ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
        make.width.ysmas_equalTo(INFOVIEW_WIDTH);
        make.height.ysmas_equalTo(35);
        make.right.equalTo(self).with.offset(-13.5);
        make.top.equalTo(self.topView.ysmas_bottom).with.offset(7.5);
    }];
}

-(void)setTopView:(UIView *)topView
{
    _topView = topView;
    [self.topInfoView ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
        make.width.ysmas_equalTo(INFOVIEW_WIDTH);
        make.height.ysmas_equalTo(35);
        make.right.equalTo(self).with.offset(-13.5);
        make.top.equalTo(topView.ysmas_bottom).with.offset(7.5);
    }];
}

-(void)onMemberInfoUpdate:(ERTCUserInfo *)user isSmall:(BOOL)isSmall {
    if (user) {
        
        //语音状态
        NSString *imageUrl = @"";
        if (user.isAudioAvailable) {
            imageUrl = @"小0";
        } else {
            imageUrl = @"小麦克风关闭";
        }
        
        [self.audioImageView setImage:[UIImage imageNamed:imageUrl]];
        [self.topVideoMircImageView setImage:[UIImage imageNamed:imageUrl]];
        [self.centerAudioMircImageView setImage:[UIImage imageNamed:imageUrl]];
        
        //视频状态
        //对方共享中，忽略视频状态
        if (user.isVideoAvailable) {
            self.centerAudioHeaderImageView.hidden = YES;
            self.videoStatusBGView.hidden = YES;
            
            self.centerAudioInfoView.hidden = YES;
            self.topInfoView.hidden = NO;
            self.statusBGView.hidden = NO;
        } else {
            self.centerAudioHeaderImageView.hidden = NO;
            self.videoStatusBGView.hidden = NO;
            
            self.centerAudioInfoView.hidden = NO;
            self.topInfoView.hidden = YES;
            self.statusBGView.hidden = YES;
        }
       
        //信号
        imageUrl = @"";
        switch (user.netLevel) {
            case ERTCNetworkQuality_Excellent:
                imageUrl = @"信号-1";
                break;
            case ERTCNetworkQuality_Good:
                imageUrl = @"信号-2";
                break;
            case ERTCNetworkQuality_Poor:
                imageUrl = @"信号-3";
                break;
            case ERTCNetworkQuality_Bad:
                imageUrl = @"信号-4";
                break;
            case ERTCNetworkQuality_Vbad:
                imageUrl = @"信号-4";
                break;
            default:
                imageUrl = @"信号-4";
                break;
        }

        [self.signImageView setImage:[UIImage imageNamed:imageUrl]];
        [self.topSignImageView setImage:[UIImage imageNamed:imageUrl]];
        [self.centerAudioSignImageView setImage:[UIImage imageNamed:imageUrl]];
        
        self.nickNameLabel.text = user.uid;
        self.topNameLabel.text = user.uid;
        self.centerAudioNameLabel.text = user.uid;

        if (isSmall == YES) {
            self.statusBGView.hidden = NO;
            self.topInfoView.hidden = YES;
            self.centerAudioInfoView.hidden = YES;
            self.videoStatusBGView.backgroundColor = [UIColor YSLabelColorPrimary];
            
            //中间头像
            [self.centerAudioHeaderImageView ysmas_updateConstraints:^(YSMASConstraintMaker *make) {
                make.width.height.ysmas_equalTo(50);
            }];
            
            self.centerAudioHeaderImageView.layer.cornerRadius = 25;
        } else {
            self.statusBGView.hidden = YES;
            self.videoStatusBGView.backgroundColor = EZSaaSRTCUIColorHex(0x4D4D4D);
            
            //中间头像
            [self.centerAudioHeaderImageView ysmas_updateConstraints:^(YSMASConstraintMaker *make) {
                make.width.height.ysmas_equalTo(80);
            }];
            
            self.centerAudioHeaderImageView.layer.cornerRadius = 40;
        }
    }
}

#pragma mark - Getter Method
-(UIView *)videoStatusBGView {
    if (!_videoStatusBGView) {
        _videoStatusBGView = [UIView new];
        _videoStatusBGView.backgroundColor = [UIColor YSLabelColorPrimary];
        _videoStatusBGView.accessibilityIdentifier = @"videoStatusBGView";
    }
    
    return _videoStatusBGView;
}

/* -------------------------------------------- */
//底部状态
-(UIView *)statusBGView {
    if (!_statusBGView) {
        _statusBGView = [UIView new];
        _statusBGView.backgroundColor = [UIColor YSLabelColorPrimary];
    }
    
    return _statusBGView;
}

-(UIImageView *)audioImageView {
    if (!_audioImageView) {
        _audioImageView = [[UIImageView alloc] init];
        _audioImageView.clipsToBounds = YES;
        _audioImageView.backgroundColor = [UIColor clearColor];
        _audioImageView.contentMode = UIViewContentModeScaleToFill;
    }
    
    return _audioImageView;
}

-(UILabel *)nickNameLabel
{
    if (!_nickNameLabel) {
        _nickNameLabel = [[UILabel alloc] init];
        _nickNameLabel.font = [UIFont systemFontOfSize:10];
        _nickNameLabel.textColor = [UIColor YSSystemWhiteColor];
        _nickNameLabel.textAlignment = NSTextAlignmentLeft;
    }
    
    return _nickNameLabel;
}

-(UIImageView *)signImageView
{
    if (!_signImageView) {
        _signImageView = [[UIImageView alloc] init];
    }
    
    return _signImageView;
}

/* -------------------------------------------- */
//关闭视频后，中间的状态
-(UIView *)centerAudioInfoView
{
    if (!_centerAudioInfoView) {
        _centerAudioInfoView = [UIView new];
        _centerAudioInfoView.backgroundColor = [UIColor clearColor];
    }
    
    return _centerAudioInfoView;
}

-(UIImageView *)centerAudioHeaderImageView {
    if (!_centerAudioHeaderImageView) {
        _centerAudioHeaderImageView = [[UIImageView alloc] init];
        _centerAudioHeaderImageView.clipsToBounds = YES;
        _centerAudioHeaderImageView.backgroundColor = [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1];
    }
    
    return _centerAudioHeaderImageView;
}

-(UILabel *)centerHeaderImageViewNameLabel {
    if (!_centerHeaderImageViewNameLabel) {
        _centerHeaderImageViewNameLabel = [[UILabel alloc] init];
        _centerHeaderImageViewNameLabel.font = [UIFont systemFontOfSize:14];
        _centerHeaderImageViewNameLabel.backgroundColor = [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1];
        _centerHeaderImageViewNameLabel.textColor = [UIColor YSSystemWhiteColor];
        _centerHeaderImageViewNameLabel.clipsToBounds = YES;
        _centerHeaderImageViewNameLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _centerHeaderImageViewNameLabel;
}

-(UILabel *)centerAudioNameLabel {
    if (!_centerAudioNameLabel) {
        _centerAudioNameLabel = [[UILabel alloc] init];
        _centerAudioNameLabel.font = [UIFont systemFontOfSize:12];
        _centerAudioNameLabel.textColor = [UIColor YSSystemWhiteColor];
        _centerAudioNameLabel.text = @"";
        _centerAudioNameLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _centerAudioNameLabel;
}

-(UIImageView *)centerAudioMircImageView {
    if (!_centerAudioMircImageView) {
        _centerAudioMircImageView = [[UIImageView alloc] init];
    }
    
    return _centerAudioMircImageView;
}


-(UIImageView *)centerAudioSignImageView {
    if (!_centerAudioSignImageView) {
        _centerAudioSignImageView = [[UIImageView alloc] init];
    }
    
    return _centerAudioSignImageView;
}

///* -------------------------------------------- */
//视频开启时，右上角状态视图
-(UIView *)topInfoView {
    if (!_topInfoView) {
        _topInfoView = [[UIView alloc] init];
        _topInfoView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        _topInfoView.layer.cornerRadius = 35.f/2;
    }
    
    return _topInfoView;
}

-(UILabel *)topNameLabel {
    if (!_topNameLabel) {
        _topNameLabel = [[UILabel alloc] init];
        _topNameLabel.font = [UIFont systemFontOfSize:12];
        _topNameLabel.textColor = [UIColor whiteColor];
        _topNameLabel.text = @"";
        _topNameLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _topNameLabel;
}

-(UIImageView *)topSignImageView {
    if (!_topSignImageView) {
        _topSignImageView = [[UIImageView alloc] init];
    }
    
    return _topSignImageView;
}

-(UIImageView *)topVideoMircImageView {
    if (!_topVideoMircImageView) {
        _topVideoMircImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"麦克风小"]];
        _topVideoMircImageView.clipsToBounds = YES;
        _topVideoMircImageView.contentMode = UIViewContentModeScaleToFill;
        _topVideoMircImageView.backgroundColor = [UIColor clearColor];
    }
    
    return _topVideoMircImageView;
}

@end
