//
//  ERTCMemberNetworkReceiveAudioContentCell.m
//  EZSaaSVideoConferenceSDK
//
//  Created by wangjinlong23 on 2022/6/23.
//  Copyright © 2022 EZSaaS. All rights reserved.
//

#import "ERTCMemberNetworkReceiveAudioContentCell.h"
#import <ERTC/ERTCStatistics.h>
#import "ERTCVideoMeetingManager.h"

@interface ERTCMemberNetworkReceiveAudioContentCell ()

@property (nonatomic, strong) UILabel *userIdLabel; //远端音频clientId
@property (nonatomic, strong) UILabel *audioReceivedBitrateLabel;//远端音频传输码率（下行）
@property (nonatomic, strong) UILabel *audioPacketLossRateLabel;//远端音频丢包率(下行)
@property (nonatomic, strong) UILabel *audioTotalFrozenTimeLabel;//远端音频总的卡顿时长
@property (nonatomic, strong) UILabel *audioFrozenRateLabel; //远端音频卡顿比
@property (nonatomic, strong) UILabel *audioPacketCountLabel;//接收音频总包数
@property (nonatomic, strong) UILabel *audioPlcPacketCountLabel;//音频帧补偿数量
@property (nonatomic, strong) UILabel *audioUncompensateLossLabel;//音频补偿前丢包率（％）

@property (nonatomic, strong) UIStackView *stackView;

@end

@implementation ERTCMemberNetworkReceiveAudioContentCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.stackView];

        UILabel *label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.userIdLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.audioReceivedBitrateLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.audioPacketLossRateLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.audioTotalFrozenTimeLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.audioFrozenRateLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.audioPacketCountLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.audioPlcPacketCountLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.audioUncompensateLossLabel = label;
   
        [self.stackView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.right.top.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView).with.offset(-1);
        }];
    }
    
    return self;
}


- (void)setRemoteAudioStats:(ERTCRemoteAudioStatistics *)remoteAudioStats {
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:remoteAudioStats.userId];
    self.userIdLabel.text = [NSString stringWithFormat:@"%@",info.uid];
    self.audioReceivedBitrateLabel.text = [NSString stringWithFormat:@"%d",remoteAudioStats.audioReceivedBitrate];
    self.audioPacketLossRateLabel.text = [NSString stringWithFormat:@"%d",remoteAudioStats.audioPacketLossRate];
    self.audioTotalFrozenTimeLabel.text = [NSString stringWithFormat:@"%d",remoteAudioStats.audioTotalFrozenTime];
    self.audioFrozenRateLabel.text = [NSString stringWithFormat:@"%d",remoteAudioStats.audioFrozenRate];
    self.audioPacketCountLabel.text = [NSString stringWithFormat:@"%d",remoteAudioStats.audioPacketCount];
    self.audioPlcPacketCountLabel.text = [NSString stringWithFormat:@"%d",remoteAudioStats.audioPlcPacketCount];
    self.audioUncompensateLossLabel.text = [NSString stringWithFormat:@"%d",remoteAudioStats.audioUncompensateLoss];
}

-(UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [[UIStackView alloc] init];
        _stackView.backgroundColor = [UIColor clearColor];
        _stackView.spacing = 1;
        _stackView.distribution = UIStackViewDistributionFillEqually;
        _stackView.layer.cornerRadius = 5;
    }
    
    return _stackView;
}

-(UILabel *)getLabel {
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor colorWithRed:131/255.f green:131/255.f blue:131/255.f alpha:0.55];//[UIColor whiteColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

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