//
//  ERTCMeetingMembersTableViewCell.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/11/21.
//

#import "ERTCMeetingMembersTableViewCell.h"

@interface ERTCMeetingMembersTableViewCell ()

@property (nonatomic, strong) UILabel *headerImageViewNameLabel;
@property (nonatomic, strong) UIImageView *memberHeaderImageView;
@property (nonatomic, strong) UILabel *memberNameLabel;

@property (nonatomic, strong) UIImageView *memberTypeImageView;
@property (nonatomic, strong) UIImageView *signImageView;

@property (nonatomic, strong) UIStackView *stackView;
@property (nonatomic, strong) UIImageView *audioImageView;
@property (nonatomic, strong) UIImageView *videoImageView;

@property (nonatomic, strong) UIView *lineView;

@end

@implementation ERTCMeetingMembersTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.headerImageViewNameLabel];
        [self.contentView addSubview:self.memberHeaderImageView];
        [self.contentView addSubview:self.memberNameLabel];
        
        [self.contentView addSubview:self.memberTypeImageView];
        [self.contentView addSubview:self.signImageView];
        [self.contentView addSubview:self.stackView];
        [self.contentView addSubview:self.lineView];
        
        [self.headerImageViewNameLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView).with.offset(16);
            make.width.height.ysmas_equalTo(40);
        }];
        
        [self.memberHeaderImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.center.equalTo(self.headerImageViewNameLabel);
            make.width.height.ysmas_equalTo(40);
        }];
        
        [self.memberNameLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.top.equalTo(self.headerImageViewNameLabel);
            make.left.equalTo(self.headerImageViewNameLabel.ysmas_right).with.offset(12);
        }];
        [self.memberTypeImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.equalTo(self.memberNameLabel);
            make.bottom.equalTo(self.headerImageViewNameLabel);
        }];
        
        [self.signImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.centerY.equalTo(self.memberTypeImageView);
            make.left.equalTo(self.memberTypeImageView.ysmas_right).with.offset(8);
        }];
    
        [self.stackView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.greaterThanOrEqualTo(self.memberNameLabel.ysmas_right).with.offset(5);
            make.right.equalTo(self.contentView).with.offset(-35);
            make.centerY.equalTo(self.contentView);
        }];
        
        [self.stackView addArrangedSubview:self.audioImageView];
        [self.stackView addArrangedSubview:self.videoImageView];
        [self.audioImageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.videoImageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.stackView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.stackView setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [self.lineView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.height.ysmas_equalTo(1);
            make.right.bottom.equalTo(self.contentView);
            make.left.equalTo(self.memberHeaderImageView);
        }];
    }
    return self;
}

- (void)setData:(ERTCUserInfo *)model {
    self.memberNameLabel.text = model.uid;
    if (model.uid.length > 0) {
        self.headerImageViewNameLabel.hidden = NO;
        self.headerImageViewNameLabel.text = [model.uid substringWithRange:NSMakeRange(0, 1)];
    } else {
        self.headerImageViewNameLabel.hidden = YES;
    }
    NSString *typeImageName = @"手机";
    [self.memberTypeImageView setImage:[UIImage imageNamed:typeImageName]];
    //网络延迟
    NSString *imageUrl = @"";
    switch (model.netLevel) {
        case ERTCNetworkQuality_Excellent:
        case ERTCNetworkQuality_Good:
            imageUrl = @"信号-1";
            break;
        case ERTCNetworkQuality_Poor:
            imageUrl = @"信号-2";
            break;
        case ERTCNetworkQuality_Bad:
            imageUrl = @"信号-3";
            break;
        case ERTCNetworkQuality_Vbad:
            imageUrl = @"信号-4";
            break;
        default:
            imageUrl = @"信号-1";
            break;
    }
    [self.signImageView setImage:[UIImage imageNamed:imageUrl]];
    //视频状态:0-正常；1-关闭；2-主持人禁用
    if (model.isVideoAvailable) {
        [self.videoImageView setImage:[UIImage imageNamed:@"open_video"]];
    }
    else  {
        [self.videoImageView setImage:[UIImage imageNamed:@"close_video"]];
    }
    if (model.isAudioAvailable) {
        [self.audioImageView setImage:[UIImage imageNamed:@"麦克风开"]];
    } else {
        [self.audioImageView setImage:[UIImage imageNamed:@"麦克风关"]];
    }
}

#pragma mark - UI Getter Method
-(UIImageView *)memberHeaderImageView {
    if (!_memberHeaderImageView) {
        _memberHeaderImageView = [[UIImageView alloc] init];
        _memberHeaderImageView.layer.cornerRadius = 20;
        _memberHeaderImageView.clipsToBounds = YES;
    }
    
    return _memberHeaderImageView;
}

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

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

-(UILabel *)memberNameLabel {
    if (!_memberNameLabel) {
        _memberNameLabel = [[UILabel alloc] init];
        _memberNameLabel.font = [UIFont systemFontOfSize:14];
        _memberNameLabel.textColor = [UIColor YSLabelColorPrimary];
        _memberNameLabel.text = @"";
    }
    
    return _memberNameLabel;
}


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

-(UIImageView *)audioImageView {
    if (!_audioImageView) {
        _audioImageView = [[UIImageView alloc] init];
        _audioImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    return _audioImageView;
}

-(UIImageView *)videoImageView {
    if (!_videoImageView) {
        _videoImageView = [[UIImageView alloc] init];
        _videoImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    return _videoImageView;
}

-(UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [[UIStackView alloc] init];
        _stackView.spacing = 8;
    }
    
    return _stackView;
}

-(UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor YSInputColor];
    }
    
    return _lineView;
}

+(NSString *)cellReuseId {
    return @"EZSaaSVideoCMeetingMembersTableViewCellCID";
}


@end
