//
//  waterViewController.m
//  Open
//
//  Created by gao on 2017/1/16.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "waterViewController.h"
#import "WeatherACell.h"

@interface waterViewController ()<UICollectionViewDelegateFlowLayout>

@end

@implementation waterViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    weatherObject *weaher =[weatherObject new];
    __weak typeof(self) weakSelf = self;

    [weaher weathermessage3:^(weather_2 *mod, NSError *error) {
        if (error) {
            return;
        }
        if (![weaher.haveSomeThing isEqualToString:@"have"]) {
            return;
        }
        weakSelf.WeatherModel = mod;
        weakSelf.weatherAddress.text =MHSafeStrings(mod.dataa.city.pname);
        weakSelf.weatherTime.text =MHSafeStrings(mod.dataa.condition.updatetime);
        //pic
        weakSelf.weatherNowImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"W%@",mod.dataa.condition.icon]];
        weakSelf.weatherTemperature.text = MHSafeStrings(mod.dataa.condition.temp);
        weakSelf.weatherLabel.text = MHSafeStrings(mod.dataa.condition.condition);
        [self.weatherCollectView reloadData];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.weatherCollectView.alwaysBounceVertical = YES;
    [self.weatherCollectView registerNib:[UINib nibWithNibName:@"WeatherACell" bundle:nil] forCellWithReuseIdentifier:@"WeatherACell"];
     [self.weatherCollectView registerNib:[UINib nibWithNibName:@"WeatherBCell" bundle:nil] forCellWithReuseIdentifier:@"WeatherBCell"];
    
    
}
#pragma mark <UICollectionViewDataSource>

//1.多少个分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
//2.每个分区多少项
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    }
    if (section == 1) {
        return 6;//瞎写的条数，假设有动态25条数据
    }
    return 0;
}
//3.每个项什么样
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        WeatherACell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WeatherACell" forIndexPath:indexPath];
        
        if (indexPath.item == 0) {
            cell.label_1.text =MHSafeStrings(self.WeatherModel.dataa.condition.windDir);
            cell.label2.text = MHSafeStrings(self.WeatherModel.dataa.condition.windLevel);
        }else {
            if ([self.WeatherModel.code isEqualToString:@"0"] ) {
                NSArray *labelOneARRay =@[@"大气压",@"相对湿度",@"PM2.5"];
                NSLog(@"%@",self.WeatherModel.dataa.condition.humidity);
                NSLog(@"%@",self.WeatherModel.dataa.aqi.pm25);
                NSArray *labelTwoArray =@[@"1024",self.WeatherModel.dataa.condition.humidity,self.WeatherModel.dataa.aqi.pm25];
                
                NSLog(@"%ld",(long)indexPath.item -1);
                
                cell.label_1.text = labelOneARRay[indexPath.item -1];
                cell.label2.text  =  labelTwoArray[indexPath.item -1];
            }
            
            
            //            }
            
            
        }
        return cell;
        
        
        
        
        

    }
    if (indexPath.section == 1) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WeatherBCell" forIndexPath:indexPath];
        if (![self.WeatherModel.dataa.city.pname isEqualToString:@""]) {
            UILabel *celllabel_1 =  [cell viewWithTag:1994];
            UILabel *celllabel_2 =  [cell viewWithTag:1996];
            UIImageView *iv = (UIImageView *)[cell viewWithTag:1995];
            UILabel *celllabel_4 =  [cell viewWithTag:1997];
            NSArray *labelOneARRay =@[@"今天",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",@"星期天"];
            celllabel_1.text = labelOneARRay[indexPath.item];
            if (self.WeatherModel.dataa.forecast.count > 0 ) {
                NSLog(@"%ld",(long)indexPath.item);
                iv.image =  [UIImage imageNamed:[NSString stringWithFormat:@"W%@",self.WeatherModel.dataa.forecast[indexPath.item].conditionIdDay]];
                celllabel_2.text =self.WeatherModel.dataa.forecast[indexPath.item].conditionDay;
                celllabel_4.text = [NSString stringWithFormat:@"%@°C - %@°C",self.WeatherModel.dataa.forecast[indexPath.item].tempDay,self.WeatherModel.dataa.forecast[indexPath.item].tempNight];
            }
        }
        
        return cell;
    }
    return nil;
}
#pragma  mark - UICollectionViewDelegateFlowLayout协议
//5.每个项的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            // 375 / 174  = kSrceenWith / ???
            CGFloat height = [UIScreen mainScreen].bounds.size.width * 50 / 375;
            return CGSizeMake([UIScreen mainScreen].bounds.size.width/5, height);
        }
        case 1:
        {
            return CGSizeMake([UIScreen mainScreen].bounds.size.width/3, 214);
        }

        default:
            break;
    }
    return CGSizeZero;
}
//6.每个分区内边距是多少
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(20, 30, 20, 30);
    }else if (section == 1){
         return UIEdgeInsetsMake(20, 30, 20, 30);
    }
    return UIEdgeInsetsZero;
}

//7.最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }else if(section == 1){
        return 0;
    }
    return 0;
}
//8.最小项间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 1) {
        return 0;
    }
    return 0;
}

//9.头视图的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
  
    return CGSizeZero;
}

//10.脚视图的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{

    return CGSizeMake(0, 5);
}

- (IBAction)closeButton:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}




@end
