//
//  ZCTimerPicker.m
//  UniversalApp
//
//  Created by 凯朱 on 2019/6/7.
//  Copyright © 2019 徐阳. All rights reserved.
//

#import "ZCTimerPicker.h"
@interface ZCTimerPicker()
{
    NSDate *_date;
}
/**时间选择器*/
@property (nonatomic, strong)UIDatePicker *datePicker;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *confirmBtn;
@end

@implementation ZCTimerPicker
- (instancetype)init{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight);
        [self setupChildViews];
    }
    return self;
}

-(void)setupChildViews{
    
    UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    grayView.backgroundColor = [KBlackColor colorWithAlphaComponent:0.5];
    [kAppWindow addSubview:self];
    [self addSubview:grayView];
    
    //布局
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, KScreenHeight-300, KScreenWidth, 300)];
    bgView .backgroundColor = KWhiteColor;
    
    //选择器
    [bgView addSubview:self.datePicker];
    _datePicker.frame = CGRectMake(0, 40, KScreenWidth, 260);
    
    //按钮
    [bgView addSubview:self.cancelBtn];
    [bgView addSubview:self.confirmBtn];
    self.cancelBtn.frame = CGRectMake(0, 0, 62, 39);
    self.confirmBtn.frame = CGRectMake(KScreenWidth-62, 0, 63, 39);
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 39, KScreenWidth, 1)];
    line.backgroundColor = KColor_Line;
    [bgView addSubview:line];
    
    [grayView addSubview:bgView];
}

- (void)cancelAction:(UIButton *)btn {
    [self dismiss];
}

- (void)confirmAction:(UIButton *)btn {
    [self dismiss];
    if ([self.delegate respondsToSelector:@selector(zcTimerPickerSelectFinish:timePicker:indexPath:)]) {
        [self.delegate zcTimerPickerSelectFinish:_date timePicker:self indexPath:_indexPath];
    }
}

-(void)dateChange:(UIDatePicker *)datePicker{
    NSDate *date = datePicker.date;
    [self updateDateStrWithDate:date];
}

/**
 更新值
 */
-(void)updateDateStrWithDate:(NSDate *)date{
    _date = date;
}

- (void)show {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.top = 0;
    }];
}

- (void)dismiss {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.top = KScreenHeight;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)setType:(ZCTimerPickerType)type{
    _type = type;
    if (_date == nil) {
        _date = [NSDate date];
    }
    switch (_type) {
        case ZCTimerPickerTypeDateAndTime:{
            _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
            break;
        }
        case ZCTimerPickerTypeTime:{
            _datePicker.datePickerMode = UIDatePickerModeTime;
            break;
        }
        default:{
            _datePicker.datePickerMode = UIDatePickerModeDate;
            break;
        }
    }
}
                                                              
- (UIDatePicker *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] init];
        //设置地区: zh-中国
        _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
     
        // 设置当前显示时间
        [_datePicker setDate:[NSDate date] animated:YES];
        
        //设置时间格式
        _datePicker.datePickerMode = UIDatePickerModeDate;
        
        //监听DataPicker的滚动
        [_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _datePicker;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc] init];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:KColor_2 forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.titleLabel.font = kFontMedium(15);
        [_cancelBtn sizeToFit];
    }
    return _cancelBtn;
}

- (UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [[UIButton alloc] init];
        [_confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:KColor_O forState:UIControlStateNormal];
        [_confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
        _confirmBtn.titleLabel.font = kFontMedium(15);
        [_confirmBtn sizeToFit];
    }
    return _confirmBtn;
}

@end