//
//  ZYJQNDnspodFree.m
//  ZYJHappyDNS
//
//  Created by bailong on 15/6/23.
//  Copyright (c) 2015年 Qiniu Cloud Storage. All rights reserved.
//

#import "ZYJQNDnspodFree.h"
#import "ZYJQNDomain.h"
#import "ZYJQNIP.h"
#import "ZYJQNRecord.h"

@interface ZYJQNDnspodFree ()
@property (readonly, nonatomic, strong) NSString *server;
@property (readonly, nonatomic) NSUInteger timeout;
@end

@implementation ZYJQNDnspodFree

- (instancetype)init {
    return [self initWithServer:@"119.29.29.29"];
}
- (instancetype)initWithServer:(NSString *)server {
    return [self initWithServer:@"119.29.29.29" timeout:ZYJQN_DNS_DEFAULT_TIMEOUT];
}

- (instancetype)initWithServer:(NSString *)server
                       timeout:(NSUInteger)time {
    if (self = [super init]) {
        _server = server;
        _timeout = time;
    }
    return self;
}

- (NSArray *)query:(ZYJQNDomain *)domain networkInfo:(ZYJQNNetworkInfo *)netInfo error:(NSError *__autoreleasing *)error {
    NSString *url = [NSString stringWithFormat:@"http://%@/d?ttl=1&dn=%@", [ZYJQNIP ipHost:_server], domain.domain];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:_timeout];
    NSHTTPURLResponse *response = nil;
    NSError *httpError = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest
                                         returningResponse:&response
                                                     error:&httpError];

    if (httpError != nil) {
        if (error != nil) {
            *error = httpError;
        }
        return nil;
    }
    if (response.statusCode != 200) {
        return nil;
    }
    NSString *raw = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSArray *ip1 = [raw componentsSeparatedByString:@","];
    if (ip1.count != 2) {
        return nil;
    }
    NSString *ttlStr = [ip1 objectAtIndex:1];
    int ttl = [ttlStr intValue];
    if (ttl <= 0) {
        return nil;
    }
    NSString *ips = [ip1 objectAtIndex:0];
    NSArray *ipArray = [ips componentsSeparatedByString:@";"];
    NSMutableArray *ret = [[NSMutableArray alloc] initWithCapacity:ipArray.count];
    for (int i = 0; i < ipArray.count; i++) {
        ZYJQNRecord *record = [[ZYJQNRecord alloc] init:[ipArray objectAtIndex:i] ttl:ttl type:kZYJQNTypeA source:ZYJQNRecordSourceDnspodFree];
        [ret addObject:record];
    }
    return ret;
}

@end
