//
//  ZYJQNFixZone.m
//  ZYJQiniuSDK
//
//  Created by yangsen on 2020/4/16.
//  Copyright © 2020 Qiniu. All rights reserved.
//

#import "ZYJQNFixedZone.h"
#import "ZYJQNZoneInfo.h"

@interface ZYJQNFixedZone ()

@property (nonatomic, strong) ZYJQNZonesInfo *zonesInfo;

@end

@implementation ZYJQNFixedZone

- (instancetype)initWithupDomainList:(NSArray<NSString *> *)upList {
    if (self = [super init]) {
        self.zonesInfo = [self createZonesInfo:upList zoneRegion:ZYJQNZoneRegion_unknown];
    }
    return self;
}
- (instancetype)initWithupDomainList:(NSArray<NSString *> *)upList
                          zoneRegion:(ZYJQNZoneRegion)zoneRegion {
    if (self = [super init]) {
        self.zonesInfo = [self createZonesInfo:upList zoneRegion:zoneRegion];
    }
    return self;
}

+ (instancetype)createWithHost:(NSArray<NSString *> *)upList {
    return [[ZYJQNFixedZone alloc] initWithupDomainList:upList zoneRegion:ZYJQNZoneRegion_unknown];
}

+ (instancetype)zone0 {
    static ZYJQNFixedZone *z0 = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray<NSString *> *uplist = @[@"upload.qiniup.com", @"upload-nb.qiniup.com",
                                        @"upload-xs.qiniup.com", @"up.qiniup.com",
                                        @"up-nb.qiniup.com", @"up-xs.qiniup.com",
                                        @"upload.qbox.me", @"up.qbox.me"];
        z0 = [ZYJQNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
    });
    return z0;
}

+ (instancetype)zone1 {
    static ZYJQNFixedZone *z1 = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray<NSString *> *uplist = @[@"upload-z1.qiniup.com", @"up-z1.qiniup.com",
                                        @"upload-z1.qbox.me", @"up-z1.qbox.me"];
        z1 = [ZYJQNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
    });
    return z1;
}

+ (instancetype)zone2 {
    static ZYJQNFixedZone *z2 = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray<NSString *> *uplist = @[@"upload-z2.qiniup.com", @"upload-gz.qiniup.com",
                                        @"upload-fs.qiniup.com", @"up-z2.qiniup.com",
                                        @"up-gz.qiniup.com", @"up-fs.qiniup.com",
                                        @"upload-z2.qbox.me", @"up-z2.qbox.me"];
        z2 = [ZYJQNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
    });
    return z2;
}

+ (instancetype)zoneNa0 {
    static ZYJQNFixedZone *zNa0 = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray<NSString *> *uplist = @[@"upload-na0.qiniup.com", @"up-na0.qiniup.com",
                                        @"upload-na0.qbox.me", @"up-na0.qbox.me"];
        zNa0 = [ZYJQNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
    });
    return zNa0;
}

+ (instancetype)zoneAs0 {
    static ZYJQNFixedZone *zAs0 = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray<NSString *> *uplist = @[@"upload-as0.qiniup.com", @"up-as0.qiniup.com",
                                        @"upload-as0.qbox.me", @"up-as0.qbox.me"];
        zAs0 = [ZYJQNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
    });
    return zAs0;
}

+ (NSArray <ZYJQNFixedZone *> *)localsZoneInfo{

    NSArray *zonesInfo = @[[ZYJQNFixedZone zone0],
                           [ZYJQNFixedZone zone1],
                           [ZYJQNFixedZone zone2],
                           [ZYJQNFixedZone zoneNa0],
                           [ZYJQNFixedZone zoneAs0]];
    return zonesInfo;
}

- (ZYJQNZonesInfo *)createZonesInfo:(NSArray<NSString *> *)upDomainList
                      zoneRegion:(ZYJQNZoneRegion)zoneRegion {
    NSMutableDictionary *upDomainDic = [[NSMutableDictionary alloc] init];
    for (NSString *upDomain in upDomainList) {
        [upDomainDic setValue:[NSDate dateWithTimeIntervalSince1970:0] forKey:upDomain];
    }
    ZYJQNZoneInfo *zoneInfo = [[ZYJQNZoneInfo alloc] init:86400 upDomainsList:(NSMutableArray<NSString *> *)upDomainList upDomainsDic:upDomainDic zoneRegion:zoneRegion];
    ZYJQNZonesInfo *zonesInfo = [[ZYJQNZonesInfo alloc] initWithZonesInfo:@[zoneInfo]];
    return zonesInfo;
}

- (void)preQuery:(ZYJQNUpToken *)token
              on:(ZYJQNPrequeryReturn)ret {
    ret(0, nil);
}

- (ZYJQNZonesInfo *)getZonesInfoWithToken:(ZYJQNUpToken *)token {
    return self.zonesInfo;
}

- (NSString *)up:(ZYJQNUpToken *)token
    zoneInfoType:(ZYJQNZoneInfoType)zoneInfoType
         isHttps:(BOOL)isHttps
    frozenDomain:(NSString *)frozenDomain {

    if (self.zonesInfo == nil) {
        return nil;
    }
    return [super upHost:[self.zonesInfo getZoneInfoWithType:ZYJQNZoneInfoTypeMain] isHttps:isHttps lastUpHost:frozenDomain];
}

@end
