//
//  BaseNetManager.m
//  BaseProject
//
//  Created by jiyingxin on 15/10/21.
//  Copyright © 2015年 Tarena. All rights reserved.
//

#import "BaseNetManager.h"
#import "AFNetworking.h"
#import "UIKit+AFNetworking.h"
#import "NSString+Timestamp.h"
@implementation BaseNetManager
//static AFHTTPSessionManager *manager = nil;
+ (id)Get:(NSString *)path parameters:(NSDictionary *)params completionHandler:(void (^)(id, NSError *))complete{
    AFHTTPSessionManager  *manager = [AFHTTPSessionManager  manager];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
     
  
    
    return   [manager GET:path parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
          //responseObject 是已经使用JSON序列化完毕的数据
          complete(responseObject, nil);
          NSLog(@"%@",responseObject);
      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
          complete(nil, error);
      }];
}




+ (AFHTTPSessionManager *)sharedAFManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript", @"text/plain", nil];

    });
    return manager;
}
+(void)runNow{
 
}
+ (id)POST:(NSString *)path parameters:(NSDictionary *)params completionHandler:(void(^)(id responseObj, NSError *error))complete{
    NSMutableDictionary* newParams = [NSMutableDictionary dictionaryWithDictionary:params];
    AFHTTPSessionManager *mgr = nil;
    if ([path containsString:@"https://socialsecurity.weface.com.cn:8066"]) {
        mgr = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:@"https://socialsecurity.weface.com.cn:8066"]];
        //[mgr setSecurityPolicy:[BaseNetManager customSecurityPolicy]] ;
    }else{
        mgr = [AFHTTPSessionManager manager];
        
    }
    mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
    mgr.requestSerializer.timeoutInterval = 8.f;
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSLog(@">>>>>url:%@",path);
    return  [mgr POST:path parameters:newParams headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        if ([responseObject isKindOfClass:[NSData class]]) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            NSLog(@"  url:%@\n\n\n>>>responseObject:%@",path,dic);
            
            if ([dic isKindOfClass:[NSDictionary class]] && [path containsString:IPDIZHI]) {
                NSString *code = [NSString stringWithFormat:@"%@",dic[@"code"]];
                if ([code isEqualToString:@"3000"]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:KKSBLoginTokenOutTimeNotificationName object:nil];
                }
                
            }
            complete(dic, nil);
        }else {
            if (responseObject) {
                NSLog(@"  url:%@\n\n\n>>>responseObject:%@",path,responseObject);
                
                if ([responseObject isKindOfClass:[NSDictionary class]] && [path containsString:IPDIZHI]) {
                    NSString *code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
                    if ([code isEqualToString:@"3000"]) {
                        [[NSNotificationCenter defaultCenter] postNotificationName:KKSBLoginTokenOutTimeNotificationName object:nil];
                    }
                    
                }
                
                 complete(responseObject, nil);
             }
        }
       
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            complete(nil, error);
        }
    }];
}







+ (id)POST2:(NSString *)path parameters:(NSDictionary *)params completionHandler:(void(^)(id responseObj, NSError *error))complete{
  
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/xml",@"text/html", @"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
  
    
    
    return  [manager POST:path parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       NSLog(@"");
        
        if (responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
            complete(dic,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"~~~~~~~~%@",error);
        complete(nil, error);
    }];
 
}

+ (id)POST3:(NSString *)path parameters:(NSDictionary *)params completionHandler:(void(^)(id responseObj, NSError *error))complete
{
    
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
      
    
        
        return  [manager POST:path parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"");
            
            if (responseObject) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
                complete(dic,nil);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"~~~~~~~~%@",error);
            complete(nil, error);
        }];
}

+ (id)POST4:(NSString *)path parameters:(NSDictionary *)params completionHandler:(void(^)(id responseObj, NSError *error))complete{
  
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/xml",@"text/html", @"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    return [manager POST:path parameters:params headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *data= [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
        
        [formData appendPartWithFormData:data name:@"Data"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
            complete(dic,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        complete(nil, error);
    }];
    
//    return [manager POST:path parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        if (responseObject) {
//            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
//            complete(dic,nil);
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        complete(nil, error);
//    }];
 
}
+(id)POSTNews:(NSString *)path parameters:(NSDictionary *)params completionHandler:(void (^)(id, NSError *))complete{

        [manager.requestSerializer setValue:@"APPCODE  e9b3308e6c544a95984643debebf5643" forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
    
    
        return [[self sharedAFManager]POST:path parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            complete(responseObject, nil);
               
            NSLog(@"---------respense:%@",responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            complete(nil, error);
            NSLog(@"error:%@",error);
        }];


}







+ (AFSecurityPolicy*)customSecurityPolicy
{
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"public" ofType:@"cer"];
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    NSSet <NSData *>* certSet = [[NSSet alloc] initWithObjects:certData, nil];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    securityPolicy.allowInvalidCertificates = YES;//是否信任一个失效或过期的SSL证书的服务器。默认为NO。
    securityPolicy.validatesDomainName = YES;//验证域名是否在证书的CN字段中。默认是YES。
    [securityPolicy setPinnedCertificates:certSet];
    return securityPolicy;
}



@end












