//
//  ZYJQNConfiguration.m
//  ZYJQiniuSDK
//
//  Created by bailong on 15/5/21.
//  Copyright (c) 2015年 Qiniu. All rights reserved.
//

#import "ZYJQNConfiguration.h"
#import "ZYJQNHttpResponseInfo.h"
#import "ZYJQNResponseInfo.h"
#import "ZYJQNSessionManager.h"
#import "ZYJQNUpToken.h"
#import "ZYJQNUploadInfoReporter.h"
#import "ZYJQNAutoZone.h"

const UInt32 kZYJQNBlockSize = 4 * 1024 * 1024;
const UInt32 kZYJQNDefaultDnsCacheTime = 2 * 60;


@implementation ZYJQNConfiguration

+ (instancetype)build:(ZYJQNConfigurationBuilderBlock)block {
    ZYJQNConfigurationBuilder *builder = [[ZYJQNConfigurationBuilder alloc] init];
    block(builder);
    return [[ZYJQNConfiguration alloc] initWithBuilder:builder];
}

- (instancetype)initWithBuilder:(ZYJQNConfigurationBuilder *)builder {
    if (self = [super init]) {

        _chunkSize = builder.chunkSize;
        _putThreshold = builder.putThreshold;
        _retryMax = builder.retryMax;
        _retryInterval = builder.retryInterval;
        _timeoutInterval = builder.timeoutInterval;

        _recorder = builder.recorder;
        _recorderKeyGen = builder.recorderKeyGen;

        _proxy = builder.proxy;

        _converter = builder.converter;
        
        _zone = builder.zone;

        _useHttps = builder.useHttps;

        _allowBackupHost = builder.allowBackupHost;
        
        _reportConfig = builder.reportConfig;

        _useConcurrentResumeUpload = builder.useConcurrentResumeUpload;
        
        _concurrentTaskCount = builder.concurrentTaskCount;
    }
    return self;
}

@end

@interface ZYJQNGlobalConfiguration()
@end
@implementation ZYJQNGlobalConfiguration
+ (instancetype)shared{
    static ZYJQNGlobalConfiguration *config = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        config = [[ZYJQNGlobalConfiguration alloc] init];
        [config setupData];
    });
    return config;
}
- (void)setupData{
    _isDnsOpen = YES;
    _dnscacheDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/Dns"];
    _dnsRepreHostNum = 2;
    _dnsCacheTime = kZYJQNDefaultDnsCacheTime;
}
@end

@implementation ZYJQNConfigurationBuilder

- (instancetype)init {
    if (self = [super init]) {
        _zone = [[ZYJQNAutoZone alloc] init];
        _chunkSize = 2 * 1024 * 1024;
        _putThreshold = 4 * 1024 * 1024;
        _retryMax = 3;
        _timeoutInterval = 60;
        _retryInterval = 0.5;
        _reportConfig = [ZYJQNReportConfig sharedInstance];

        _recorder = nil;
        _recorderKeyGen = nil;

        _proxy = nil;
        _converter = nil;

        _useHttps = YES;
        _allowBackupHost = YES;
        _useConcurrentResumeUpload = NO;
        _concurrentTaskCount = 3;
    }
    return self;
}

@end

