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

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

@interface ERTCMemberNetworkReceiveVideoContentCell()
@property (nonatomic, strong) UILabel *userIdLabel; //远端视频clientId
@property (nonatomic, strong) UILabel *videoReceivedBitrateLabel;//远端视频传输码率（下行）
@property (nonatomic, strong) UILabel *videoReceivedFrameRateLabel;//远端帧率大小
@property (nonatomic, strong) UILabel *videoPacketLossRateLabel;//远端视频丢包率(下行)
@property (nonatomic, strong) UILabel *videoTotalFrozenTimeLabel;//远端视频总的卡顿时长
@property (nonatomic, strong) UILabel *videoFrozenRateLabel; //远端视频卡顿比
@property (nonatomic, strong) UILabel *streamTypeLabel; //远端流类型
@property (nonatomic, strong) UILabel *sizeLabel;//视频宽高
@property (nonatomic, strong) UILabel *videoPacketCountLabel;//接收视频总包数
@property (nonatomic, strong) UILabel *videoUncompensateLossLabel;//视频补偿前丢包率（％）

@property (nonatomic, strong) UIStackView *stackView;
@end

@implementation ERTCMemberNetworkReceiveVideoContentCell

-(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.videoReceivedBitrateLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.videoPacketLossRateLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.videoTotalFrozenTimeLabel = label;
        
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.videoFrozenRateLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.videoPacketCountLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.videoUncompensateLossLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.videoReceivedFrameRateLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.streamTypeLabel = label;
        
        label = [self getLabel];
        [self.stackView addArrangedSubview:label];
        self.sizeLabel = 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)setRemoteVideoStats:(ERTCRemoteVideoStatistics *)remoteVideoStats {
    ERTCUserInfo *info = [[ERTCVideoMeetingManager sharedInstance] getMemberInfoByUserId:remoteVideoStats.userId];
    self.userIdLabel.text = [NSString stringWithFormat:@"%@",info.uid];
    self.videoReceivedBitrateLabel.text = [NSString stringWithFormat:@"%d",remoteVideoStats.videoReceivedBitrate];
    self.videoPacketLossRateLabel.text = [NSString stringWithFormat:@"%d",remoteVideoStats.videoPacketLossRate];
    self.videoTotalFrozenTimeLabel.text = [NSString stringWithFormat:@"%d",remoteVideoStats.videoTotalFrozenTime];
    self.videoFrozenRateLabel.text = [NSString stringWithFormat:@"%d",remoteVideoStats.videoFrozenRate];
    self.videoPacketCountLabel.text = [NSString stringWithFormat:@"%d",remoteVideoStats.videoPacketCount];
    self.videoUncompensateLossLabel.text = [NSString stringWithFormat:@"%d",remoteVideoStats.videoUncompensateLoss];
    self.videoReceivedFrameRateLabel.text = [NSString stringWithFormat:@"%d",remoteVideoStats.videoReceivedFrameRate];
    self.streamTypeLabel.text = [NSString stringWithFormat:@"%d",remoteVideoStats.streamType];
    self.sizeLabel.text = [NSString stringWithFormat:@"%d \n %d",remoteVideoStats.width,remoteVideoStats.heigth];
}

-(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 @"ERTCMemberNetworkReceiveVideoContentCell";
}
@end
