//
//  ICPhotoManageHeaderView.m
//  superCleaner
//
//  Created by Mazy on 2020/7/3.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICPhotoManageHeaderView.h"
#import "ICDatePickerView.h"

@implementation ICPhotoManageHeaderView

+ (ICPhotoManageHeaderView *)loadFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:@"ICPhotoManageHeaderView" owner:self options:nil] lastObject];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.startTimeButton.layer.cornerRadius = 10;
    self.endTimeButton.layer.cornerRadius = 10;
    
    self.endDate = [NSDate date];
    
    [self.endTimeButton setTitle:[self getDateStringFromDate: [[NSDate alloc]init]] forState:UIControlStateNormal];
    
    self.titleLabel.text = ICLocalized(@"PeriodCategory");
}

- (IBAction)startTimeButtonAction:(UIButton *)sender {
    
    ICDatePickerView *datePciker = [[[NSBundle mainBundle] loadNibNamed:@"ICDatePickerView" owner:self options:nil] lastObject];
    [datePciker showWithDate: self.stratDate];
    
    __weak typeof(self) weakSelf = self;
    datePciker.commitTrackDateClosre = ^(NSDate *date) {
        NSLog(@"%@", date);
        if ([date compare:weakSelf.endDate] == NSOrderedDescending) {
             [MBProgressHUD showError: ICLocalized(@"StartTimeThanEndTime")];
             return;
         }
        [weakSelf.startTimeButton setTitle:[self getDateStringFromDate:date] forState:UIControlStateNormal];
        weakSelf.stratDate = date;
        if (weakSelf.dateSelectFinishedHandle) {
            weakSelf.dateSelectFinishedHandle(date, self.endDate);
        }
    };
}

- (IBAction)endTimeButtonAction:(UIButton *)sender {
    ICDatePickerView *datePciker = [[[NSBundle mainBundle] loadNibNamed:@"ICDatePickerView" owner:self options:nil] lastObject];
    [datePciker showWithDate: self.endDate];
    
    __weak typeof(self) weakSelf = self;
    datePciker.commitTrackDateClosre = ^(NSDate *date) {
        NSLog(@"%@", date);
         if ([date compare:weakSelf.stratDate] == NSOrderedAscending) {
             [MBProgressHUD showError:  ICLocalized(@"EndTimeThanStartTime")];//@"结束时间必须大于开始时间"];
            return;
        }
        [weakSelf.endTimeButton setTitle:[self getDateStringFromDate:date] forState:UIControlStateNormal];
        weakSelf.endDate = date;
        if (weakSelf.dateSelectFinishedHandle) {
            weakSelf.dateSelectFinishedHandle(weakSelf.stratDate, date);
        }
    };
}

-(NSString *)getDateStringFromDate: (NSDate *)date {
    //功能： 将NSdate转变为表示时间的NSString
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    //设置格式,表示方式：年yyyy 月：MM 日：dd  小时：hh 分钟：mm 秒：ss
    NSString *language = [[NSLocale preferredLanguages] firstObject];
    if ([language containsString: @"zh"]) {
        [formatter setDateFormat: @"MM月, yyyy"];
    } else {
        [formatter setDateFormat: @"MM, yyyy"];
    }
//    [formatter setDateFormat: @"MM月, yyyy"];

    NSString *dateStr = [formatter stringFromDate: date];
    return dateStr;
}

//传入今天的时间，返回之前的的时间
- (NSString *)getPreHours: (int)hour Date:(NSDate *)aDate {
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:aDate];
    [components setHour:([components hour] - hour)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
    [dateday setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [dateday stringFromDate: beginningOfWeek];
}


@end
