//
//  HomeWeatherBoard.m
//  HDMall
//
//  Created by Javen on 2017/7/28.
//  Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//

#import "HomeWeatherBoard.h"

@implementation HomeWeatherBoard

- (void)awakeFromNib {
    [super awakeFromNib];
    WS(weakSelf);
    [[LocationHelper shareInstance] getCityNameSuccess:^(NSString *string) {
        [weakSelf httpGetWeather:string];
    }
                                                failed:nil];

}

- (void)configWithModel:(WeatherModel *)model {
    if (model.city.length > 0) {
        self.city.text = model.city;
        [self.img sd_setImageWithURL:[NSURL URLWithString:model.dayPictureUrl]];
        self.detail.text = [NSString stringWithFormat:@"%@ %@ %@", model.weather, model.wind, model.temperature];
    }
}

/** 根据城市名称获取天气 */
- (void)httpGetWeather:(NSString *)cityName
{
    if (nil != self.weatherModel) {
        return;
    }
    
    NSDictionary *myDictionary = @{ @"ak" : BAIDU_AK, @"location" : cityName, @"output" : @"json" };
    WS(weakSelf);
    
    [ZJHttpManager GET:WEATHER_SERVER_URL parameters:myDictionary complete:^(id responseObject, NSError *error) {
        //更新天气信息
        weakSelf.weatherModel = [[WeatherModel alloc] init];
        weakSelf.weatherModel.city = myDictionary[@"location"];
        [weakSelf.weatherModel setValuesForKeysWithDictionary:responseObject[@"results"][0][@"weather_data"][0]];
        [weakSelf configWithModel:weakSelf.weatherModel];
    }];
    
}

@end