//
//  ICDatePickerView.m
//  superCleaner
//
//  Created by Mazy on 2020/7/5.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICDatePickerView.h"

@implementation ICDatePickerView


- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.frame = [UIScreen mainScreen].bounds;
    
    [self.cancelButton setTitle:ICLocalized(@"Cancel") forState: UIControlStateNormal];
    [self.commitButton setTitle:ICLocalized(@"Sure") forState: UIControlStateNormal];
    self.datePickerTitleLabel.text = ICLocalized(@"DatePick");
    
    self.datePickerView.maximumDate = [[NSDate alloc] init];
    self.selectedDate = [NSDate date];
    
    // 设置DatePickView显示中文
//    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
//    self.datePickerView.locale = locale;
    
    self.dateContentView.transform = CGAffineTransformMakeTranslation(0, 250);

}

- (IBAction)cancelAction:(UIButton *)sender {
    [self dismiss];
}

- (IBAction)commitAction:(UIButton *)sender {
    
    if (_commitTrackDateClosre) {
        _commitTrackDateClosre(self.selectedDate);
    }
    [self dismiss];
}


- (IBAction)datePickerValueChanged:(UIDatePicker *)sender {
    NSLog(@"%@", sender.date);
    self.selectedDate = sender.date;
}

- (void)showWithDate: (NSDate *)date {
    self.selectedDate = date;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    
    if (date) {    
        [self.datePickerView setDate:date animated:true];
    }
    __weak typeof(self) wkSelf = self;
    [UIView animateWithDuration:0.25 animations:^{
        wkSelf.dateContentView.transform = CGAffineTransformIdentity;
    }];
    
}

- (void)dismiss {
    __weak typeof(self) wkSelf = self;
    [UIView  animateWithDuration:0.25 animations:^{
        wkSelf.dateContentView.transform = CGAffineTransformMakeTranslation(0, 250);
    } completion:^(BOOL finished) {
        [wkSelf removeFromSuperview];
    }];
}


@end
