//
//  EZSpeedTestViewController.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/4/13.
//

#import "EZSpeedTestViewController.h"
#import "UIView+Toast.h"
#import "EZConfGlobalConfig.h"
#import <ERTC/ERTC.h>

@interface EZSpeedTestViewController ()<ERTCDelegate>
@property (weak, nonatomic) IBOutlet UITextField *upBandWidthField;//上行带宽输入框
@property (weak, nonatomic) IBOutlet UITextField *downBandWidthField;//下行带宽输入框
@property (weak, nonatomic) IBOutlet UITextField *qualityIntervalField;

@property (weak, nonatomic) IBOutlet UILabel *netQualityLabel;//网络质量
@property (weak, nonatomic) IBOutlet UILabel *upLostRateLabel;//上行丢包率
@property (weak, nonatomic) IBOutlet UILabel *downLostRateLabel;//下行丢包率
@property (weak, nonatomic) IBOutlet UILabel *rttLabel; //延迟
@property (weak, nonatomic) IBOutlet UILabel *availableUpBandwidthLabel;//上行可用带宽
@property (weak, nonatomic) IBOutlet UILabel *availableDownBandwidthLab;//下行可用带宽
@property (weak, nonatomic) IBOutlet UIView *checkResultView;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@property (weak, nonatomic) IBOutlet UILabel *stsLabel;

@property (nonatomic, strong) ERTCEngine *engine;

@end

@implementation EZSpeedTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initConfig];
    self.title = @"网速测试";
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.engine stopSpeedTest];
}
#pragma mark -开始网络测试

- (IBAction)startSpeedTest:(id)sender {
    [self.upBandWidthField resignFirstResponder];
    [self.downBandWidthField resignFirstResponder];
    [self.qualityIntervalField resignFirstResponder];
    self.checkResultView.hidden = NO;
    
    [self.engine startSpeedTestWithUpBandwidth:[self.upBandWidthField.text intValue] downBandwidth:[self.downBandWidthField.text intValue] testInterval:[self.qualityIntervalField.text intValue]];
}

#pragma mark -停止网络测试

- (IBAction)stopSpeedTest:(id)sender {
    [self.engine stopSpeedTest];
}


///初始化配置
- (void)initConfig{
    ERTCEngineConfig *config = [ERTCEngineConfig new];
    config.appID = g_APPID;
    [ERTCEngine createWithConfig:config instanceBlock:^(ERTCEngine * _Nullable instance, NSError * _Nullable error) {
        if (instance) {
            self.engine = instance;
            self.engine.delegate = self;
            ERTCLog(@"onSDKEngineInitialized");
        }else {
            [self.view makeToast:[NSString stringWithFormat:@"初始化失败(%ld)",error.code] duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

///更新网络测试结果
- (void)updateSpeedTest:(ERTCSpeedTestResult *)result {
    self.netQualityLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)result.quality];
    self.upLostRateLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)result.upLostRate];
    self.downLostRateLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)result.downLostRate];
    self.availableDownBandwidthLab.text = [NSString stringWithFormat:@"%lu",(unsigned long)result.availableDownBandwidth];
    self.availableUpBandwidthLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)result.availableUpBandwidth];
    self.rttLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)result.rtt];
    self.dateLabel.text = [self currentDateString];
    self.stsLabel.text = result.stsConnect == 0 ? @"流媒体服务器连接失败": @"流媒体服务器连接成功";
}

- (NSString *)currentDateString {
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    return [dateFormatter stringFromDate:date];
}

#pragma mark-ERTCDelegate
- (void)onSpeedTestResult:(ERTCSpeedTestResult *)result {
    if (!result.success) {
        [self showMessage:@"测试失败"];
    }else {
        [self updateSpeedTest:result];
    }
}

- (void)showMessage:(NSString *)message
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view makeToast:message duration:1.2 position:CSToastPositionCenter];
    });
}

@end
