//
//  CYYearMonthDaySelectedView.m
//  Open
//
//  Created by 雷俊博 on 2019/11/14.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "CYYearMonthDaySelectedView.h"


@interface CYYearMonthDaySelectedView ()
@property (strong, nonatomic) UIDatePicker *datePicker;    //日期选择器
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *minDateStr;
@property (copy, nonatomic) BRDateResultBlock resultBlock;
@property (copy, nonatomic) NSString *selectValue;   //选择的值
@property (strong, nonatomic) NSMutableArray<NSString *> *data;

@end

@implementation CYYearMonthDaySelectedView
#pragma mark - life cycle
- (instancetype)initWithTitle:(NSString *)title minDateStr:(NSString *)minDateStr resultBlock:(BRDateResultBlock)resultBlock
{
    self = [super init];
    if (self) {
        _title = title;
        _minDateStr = minDateStr;
        _resultBlock = resultBlock;
        
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    [super initUI];
    self.titleLabel.text = _title;
    // 添加时间选择器
    [self.alertView addSubview:self.datePicker];
    
}


#pragma mark - public methods
//对外开放的类方法
+ (void)showDatePickerWithTitle:(NSString *)title minDateStr:(NSString *)minDateStr resultBlock:(BRDateResultBlock)resultBlock
{
    CYYearMonthDaySelectedView *datePicker = [[CYYearMonthDaySelectedView alloc]initWithTitle:title minDateStr:minDateStr resultBlock:resultBlock];
    [datePicker showWithAnimation:YES];
}


#pragma mark - event response
- (void)dateChange:(UIDatePicker *)datePicker {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //设置时间格式
    formatter.dateFormat = @"yyyy.MM.dd";
    NSString *dateStr = [formatter  stringFromDate:datePicker.date];
    
    _selectValue = dateStr;
}

#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender
{
    //    [self dismissWithAnimation:NO];
}

#pragma mark - 取消按钮的点击事件
- (void)clickLeftBtn
{
    [self dismissWithAnimation:YES];
}
#pragma mark - 确定按钮的点击事件
- (void)clickRightBtn
{
    NSLog(@"点击确定按钮后，执行block回调");
    [self dismissWithAnimation:YES];
    if (_resultBlock){
        _resultBlock(_selectValue);
    }
}

#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation
{
    //1. 获取当前应用的主窗口
     UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    if (animation){
        // 动画前初始位置
        CGRect rect = self.alertView.frame;
        rect.origin.y = SCREEN_HEIGHT;
        self.alertView.frame = rect;
        // 浮现动画
        [UIView animateWithDuration:0.3 animations:^{
            CGRect rect = self.alertView.frame;
            rect.origin.y -= kDatePicHeight + kTopViewHeight;
            self.alertView.frame = rect;
            [self dateChange:self.datePicker];
        }];
    }
    
}

#pragma mark - 关闭视图方法
- (void)dismissWithAnimation:(BOOL)animation
{
    // 关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y += kDatePicHeight + kTopViewHeight;
        self.alertView.frame = rect;
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.leftBtn removeFromSuperview];
        [self.rightBtn removeFromSuperview];
        [self.titleLabel removeFromSuperview];
        [self.lineView removeFromSuperview];
        [self.topView removeFromSuperview];
        [self.datePicker removeFromSuperview];
        [self.alertView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        [self removeFromSuperview];
        self.leftBtn = nil;
        self.rightBtn = nil;
        self.titleLabel = nil;
        self.lineView = nil;
        self.topView = nil;
        self.datePicker = nil;
        self.alertView = nil;
        self.backgroundView = nil;
    }];
}


#pragma mark - getters and setters
- (UIDatePicker *)datePicker
{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc]init];
        if (@available(iOS 13.4, *)) {
            _datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
        } else {
            // Fallback on earlier versions
        }
        _datePicker.frame = CGRectMake(0, kTopViewHeight + 0.5, SCREEN_WIDTH, kDatePicHeight);
        
        //设置地区: zh-中国
        _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
        //设置日期模式(Displays month, day, and year depending on the locale setting)
        _datePicker.datePickerMode = UIDatePickerModeDate;
        // 设置当前显示时间
        [_datePicker setDate:[NSDate date] animated:YES];
        // 设置显示最大时间（此处为当前时间）
        //[_datePicker setMaximumDate:[NSDate date]];
        
        //设置时间格式
        
        //监听DataPicker的滚动
        [_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    }
    
    return _datePicker;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
