//
//  MeasurNetTools.m
//  
//
//  Created by jordan on 16/7/25.
//  Copyright © 2016年 MD313. All rights reserved.
//
#define start1 -0.33

//#define urlString @"http://cdn2.ime.sogou.com/4ecd0732b804944f2939ac4aeaff5198/5f2270d9/dl/index/1588947491/sogou_mac_58a.zip"//3M
//#define urlString @"https://ucan.25pp.com/PPAssistant_PP_103.apk"//3M
#import "MeasurNetTools.h"

@interface MeasurNetTools ()
{
    measureBlock   infoBlock;
    finishMeasureBlock   fmeasureBlock;
    int                           _second;
    NSMutableData*                _connectData;
    NSMutableData*                _oneMinData;
    NSURLConnection *             _connect;
    NSTimer*                      _timer;
}

@property (copy, nonatomic) void (^faildBlock) (NSError *error);

@end

@implementation MeasurNetTools

/**
 *  初始化测速方法
 *
 *  @param measureBlock       实时返回测速信息
 *  @param finishMeasureBlock 最后完成时候返回平均测速信息
 *
 *  @return MeasurNetTools对象
 */
- (instancetype)initWithblock:(measureBlock)measureBlock finishMeasureBlock:(finishMeasureBlock)finishMeasureBlock failedBlock:(void (^) (NSError *error))failedBlock
{
    self = [super init];
    if (self) {
        infoBlock = measureBlock;
        fmeasureBlock = finishMeasureBlock;
        _faildBlock = failedBlock;
        _connectData = [[NSMutableData alloc] init];
        _oneMinData = [[NSMutableData alloc] init];
    }
    return self;
}

/**
 *  开始测速
 */
-(void)startMeasur
{
    [self meausurNet];
}

/**
 *  停止测速，会通过block立即返回测试信息
 */
-(void)stopMeasur
{
    [self finishMeasure];
}

-(void)meausurNet
{
    NSArray *urlArray = @[@"https://wap.pp.cn/app/dl/fs08/2020/07/29/9/106_d25a9ecceb45514ad2583bb440740704.apk",
                          @"https://wap.pp.cn/app/dl/fs08/2020/07/15/7/110_b4a157dc5f4aaaedc9351a4acc72d967.apk",
                          @"https://wap.pp.cn/app/dl/fs08/2020/07/23/4/1_fd63e3d0d7161c0671e385b8ac7d5967.apk",
                          @"https://ucan.25pp.com/PPAssistant_PP_103.apk",
                          @"https://wap.pp.cn/app/dl/fs08/2020/07/28/9/123_88a8613b9cdacc254f028e9041339de2.apk"];
    
    int value = arc4random() % urlArray.count;
    
    NSString *urlString = urlArray[value];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
    NSURL    *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    _connect = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    [_timer fire];
    _second = 0;

}

-(void)countTime{
    ++_second;
    if (_second == 15) {
        [self finishMeasure];
        return;
    }
    float speed = _oneMinData.length;
//    if(speed==0.0f){
//        NSLog(@"acde");
//    }
    infoBlock(speed);
    
    //清空数据
    [_oneMinData resetBytesInRange:NSMakeRange(0, _oneMinData.length)];
    [_oneMinData setLength:0];
}

/**
 * 测速完成
 */
-(void)finishMeasure{
    [_timer invalidate];
    _timer = nil;
    if(_second!=0){
        float finishSpeed = _connectData.length / _second;
        fmeasureBlock(finishSpeed);
    }
    
    [_connect cancel];
    _connectData = nil;
    _connect = nil;
}



#pragma mark - urlconnect delegate methods
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    if (self.faildBlock) {
        self.faildBlock(error);
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [_connectData appendData:data];
    [_oneMinData appendData:data];
//    NSLog(@"_oneMinData:%lu  data:%lu",(unsigned long)_oneMinData.length,(unsigned long)data.length);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"connectionDidFinishLoading");
    [self finishMeasure];
}


- (void)dealloc {
    NSLog(@"MeasurNetTools dealloc");
}


@end
