//
//  ZYJQNUploadOption.m
//  ZYJQiniuSDK
//
//  Created by bailong on 14/10/4.
//  Copyright (c) 2014年 Qiniu. All rights reserved.
//

#import "ZYJQNUploadOption+Private.h"
#import "ZYJQNUploadManager.h"

static NSString *mime(NSString *mimeType) {
    if (mimeType == nil || [mimeType isEqualToString:@""]) {
        return @"application/octet-stream";
    }
    return mimeType;
}

@implementation ZYJQNUploadOption

+ (NSDictionary *)filteParam:(NSDictionary *)params {
    NSMutableDictionary *ret = [NSMutableDictionary dictionary];
    if (params == nil) {
        return ret;
    }

    [params enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
        if ([key hasPrefix:@"x:"] && ![obj isEqualToString:@""]) {
            ret[key] = obj;
        }
    }];

    return ret;
}

- (instancetype)initWithProgessHandler:(ZYJQNUpProgressHandler)progress {
    return [self initWithMime:nil progressHandler:progress params:nil checkCrc:NO cancellationSignal:nil];
}

- (instancetype)initWithProgressHandler:(ZYJQNUpProgressHandler)progress {
    return [self initWithMime:nil progressHandler:progress params:nil checkCrc:NO cancellationSignal:nil];
}

- (instancetype)initWithMime:(NSString *)mimeType
             progressHandler:(ZYJQNUpProgressHandler)progress
                      params:(NSDictionary *)params
                    checkCrc:(BOOL)check
          cancellationSignal:(ZYJQNUpCancellationSignal)cancel {
    if (self = [super init]) {
        _mimeType = mime(mimeType);
        _progressHandler = progress != nil ? progress : ^(NSString *key, float percent) {
        };
        _params = [ZYJQNUploadOption filteParam:params];
        _checkCrc = check;
        _cancellationSignal = cancel != nil ? cancel : ^BOOL() {
            return NO;
        };
    }

    return self;
}

+ (instancetype)defaultOptions {
    return [[ZYJQNUploadOption alloc] initWithMime:nil progressHandler:nil params:nil checkCrc:NO cancellationSignal:nil];
}

@end
