//
//  weatherObject.m
//  socialSecurity
//
//  Created by 看看智能 on 2017/5/9.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "weatherObject.h"
#import "weather.h"
#import "weatherNetModel.h"
@implementation weatherObject

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray new];
    }
    return _dataArr;
}


-(void)weathermessage:(CompletionHandle)completionHandle{
    __weak __typeof(self)weakSelf = self;
    [weatherNetModel weather:nil andCity:[getPeopleID getProvince]  Handle:^(weather_2 *model, NSError *error) {
       
            if (error) {
                return ;
            }
            NSString *city_ = nil;
            if(model.dataa.city.pname  == nil)
                city_ = [getPeopleID getProvince];
            else
                city_=      model.dataa.city.pname;
        
            
            self.strWeather  =[NSString stringWithFormat:@"%@    %@°C     %@ ",city_ , model.dataa.condition.temp,model.dataa.condition.condition ];
            NSLog(@"%@",self.strWeather);
            if (model.dataa.condition.temp == nil) {
                weakSelf.haveSomeThing = nil;
            }else{
             weakSelf.haveSomeThing = @"have";
            }
            
            
            if ([model.dataa.condition.icon isEqualToString:@""])
                weakSelf.imageWeather =nil;
            
            else
           weakSelf.imageWeather =[NSString stringWithFormat:@"W%@",model.dataa.condition.icon];
           
            completionHandle(error);
 
    }];


}

-(void)weathermessage2:(CompletionHandle2)completionHandle{


    
    NSString *appcode = @"e9b3308e6c544a95984643debebf5643e";
    NSString *host = @"http://aliv2.data.moji.com";
    NSString *path = @"/whapi/json/aliweather/briefcondition";
    NSString *method = @"POST";
    NSString *querys = @"";
    NSString *url = [NSString stringWithFormat:@"%@%@%@",  host,  path , querys];
    NSString *bodys = @"lat=39.91488908&lon=116.40387397";
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData  timeoutInterval:  5];
    request.HTTPMethod  =  method;
    [request addValue:  [NSString  stringWithFormat:@"APPCODE %@",appcode]  forHTTPHeaderField:@"Authorization"];
    [request addValue: @"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    NSData *data = [bodys dataUsingEncoding: NSUTF8StringEncoding];
    [request setHTTPBody: data];
    NSURLSession *requestSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionDataTask *task = [requestSession dataTaskWithRequest:request
                                                   completionHandler:^(NSData * _Nullable body , NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                                       NSLog(@"Response object: %@" , response);
                                                       NSString *bodyString = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
                                                       //打印应答中的body
                                                       NSLog(@"Response body: %@" , bodyString);
//                                                 weather_2 *obj = [weather_2 mj_objectWithKeyValues:response];
                  
//                                                       NSLog(@"%@",obj.data.);
//                                                       if ([city isEqualToString:@"上海市"]) {
//                                                           city =@"上海";
//                                                       }
//                                                       if ([city isEqualToString:@"天津市"]) {
//                                                           city =@"天津";
//                                                       }
//                                                       if ([city isEqualToString:@"重庆市"]) {
//                                                           city =@"重庆";
//                                                       }
//                                                       if ([city isEqualToString:@"北京市"]) {
//                                                           city =@"北京";
//                                                       }
//                                                       //打印应答中的body
//                                                       NSLog(@"Response body: %@" , bodyString);
                                                   }];
    
    [task resume];
    
 




}
//block函数有问题 ,这里我就单独写了一个方法
-(void)weathermessage3:(CompletionWeather)completionHandle{
    __weak __typeof(self)weakSelf = self;
    
    
    
    
    
    [weatherNetModel weather:nil andCity:[getPeopleID getProvince]  Handle:^(weather_2 *model, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error) {
                return ;
            }
            NSString *city_ = nil;
            if(model.dataa.city.pname  == nil)
                city_ = [getPeopleID getProvince];
            else
                city_=      model.dataa.city.pname;
        
            if (model.dataa.condition.temp == nil) {
                weakSelf.haveSomeThing = nil;
            }else{
                weakSelf.haveSomeThing = @"have";
            }
            
            
            if ([model.dataa.condition.icon isEqualToString:@""])
                weakSelf.imageWeather =nil;
            
            else
                weakSelf.imageWeather =[NSString stringWithFormat:@"W%@",model.dataa.condition.icon];
            
            completionHandle(model,error);
        });
    }];
    
    
}




@end
