//
//  TheAlertCtrl.m
//  FornoWorld
//
//  Created by RD on 2021/12/10.
//

#import "TheAlertCtrl.h"
#import "SPAlertController.h"

@interface TheAlertCtrl ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, weak) UIAlertController *ac;
@property (nonatomic, strong) NSMutableArray *timerData;
@end
@implementation TheAlertCtrl

+ (instancetype)rds_alertWithTitle:(NSString *)title message:(NSString *)message{
    
    TheAlertCtrl *ctrl = [[TheAlertCtrl alloc] init];
    
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:title
                                                                message:message
                                                         preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        
    }];
    [okAction setValue:kDarkTextColor forKey:@"_titleTextColor"];
    
    [ac addAction:okAction];
    ctrl.ac = ac;
    
    // 修改标题字体颜色
//    if (!kNULLString(title)) {
//        NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title];
//        [titleAtt addAttribute:NSForegroundColorAttributeName value:kDarkTextColor range:NSMakeRange(0, title.length)];
//        [titleAtt addAttribute:NSFontAttributeName value:[UIFont rds_titiFontStyle:FontStyle_SemiBold size:18] range:NSMakeRange(0, title.length)];
//        [ac setValue:titleAtt forKey:@"attributedTitle"];
//    }
    

    
    // 修改msg字体颜色
//    if (!kNULLString(message)) {
//        NSMutableAttributedString *msgAtt = [[NSMutableAttributedString alloc] initWithString:message];
//        [msgAtt addAttribute:NSForegroundColorAttributeName value:kDarkTextColor range:NSMakeRange(0, message.length)];
//        [msgAtt addAttribute:NSFontAttributeName value:[UIFont rds_titiFontStyle:FontStyle_Regular size:16] range:NSMakeRange(0, message.length)];
//        
//        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
//        //paragraph.lineSpacing = 5;// 字体的行间距
//        //paragraph.firstLineHeadIndent = 25.0f;//首行缩进
//        paragraph.alignment = NSTextAlignmentLeft;
//        
//        [msgAtt addAttribute:NSParagraphStyleAttributeName
//                       value:paragraph
//                       range:NSMakeRange(0, message.length)];
//        
//        [ac setValue:msgAtt forKey:@"attributedMessage"];
//    }
    
    
    
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    // 更新UI要在主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [window.rootViewController presentViewController:ac animated:YES completion:nil];
    });
    return ctrl;
}

+ (instancetype)rds_alertWithTitle:(NSString *)title message:(NSString *)message cancel:(NSString *)calcel sure:(NSString *)sure cancelColor:(UIColor *)cancelColor sureColor:(UIColor *)sureColor cancelAction:(void (^)(void))cancelActionBlock sureAction:(void (^)(void))sureActionBlock{
    
//    if(!kNULLString(message)){
//        message = [NSString stringWithFormat:@"\n%@",message];
//    }
    
    TheAlertCtrl *ctrl = [[TheAlertCtrl alloc] init];
    
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:title
                                                                message:message
                                                         preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:calcel style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
        if(cancelActionBlock) {
            cancelActionBlock();
        }
    }];
    
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sure style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        if (sureActionBlock) {
            sureActionBlock();
        }
    }];
    
    // 修改按钮字体颜色
    if(cancelColor){
        [cancelAction setValue:cancelColor forKey:@"_titleTextColor"];
    }
    if(sureColor){
        [sureAction setValue:sureColor forKey:@"_titleTextColor"];
    }
    
    
    [ac addAction:cancelAction];
    [ac addAction:sureAction];
    ctrl.ac = ac;
//    ac.view.backgroundColor = UIColor.whiteColor;
    
    
    // 修改标题字体颜色
//    NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title];
//    [titleAtt addAttribute:NSForegroundColorAttributeName value:kDarkTextColor range:NSMakeRange(0, title.length)];
//    [titleAtt addAttribute:NSFontAttributeName value:[UIFont rds_titiFontStyle:FontStyle_SemiBold size:18] range:NSMakeRange(0, title.length)];
//    [ac setValue:titleAtt forKey:@"attributedTitle"];
    
    
    // 修改msg字体颜色
//    NSMutableAttributedString *msgAtt = [[NSMutableAttributedString alloc] initWithString:message];
//    [msgAtt addAttribute:NSForegroundColorAttributeName value:RDSGrayColor range:NSMakeRange(0, message.length)];
//    [msgAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, message.length)];
//    [ac setValue:msgAtt forKey:@"attributedMessage"];
    

    
    
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    // 更新UI要在主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [window.rootViewController presentViewController:ac animated:YES completion:nil];
    });
    return ctrl;
}



+ (instancetype)rds_timerAlertSureAction:(void (^)(void))sureActionBlock{
    
    TheAlertCtrl *ctrl = [[TheAlertCtrl alloc] init];
    SPAlertController *alertController = [SPAlertController alertControllerWithTitle:@"定时关闭" message:@"" preferredStyle:SPAlertControllerStyleAlert animationType:SPAlertAnimationTypeDefault];
    
    
    UIPickerView *timerPick = [[UIPickerView alloc] init];
    timerPick.delegate = ctrl;
    timerPick.dataSource = ctrl;
    NSMutableArray *data = [NSMutableArray array];
    for (CGFloat i = 0.5; i<24; i+=0.5) {
        [data addObject:@(i)];
    }
    ctrl.timerData = data;
    
    [timerPick selectRow:0 inComponent:0 animated:NO];
    
    // 插入一个view
    [alertController insertComponentView:timerPick];
    
    SPAlertAction *action1 = [SPAlertAction actionWithTitle:@"取消" style:SPAlertActionStyleDefault handler:nil];
    SPAlertAction *action2 = [SPAlertAction actionWithTitle:@"确定" style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
        
    }];
    
    [alertController addAction:action1];
    [alertController addAction:action2];
    
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    // 更新UI要在主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [timerPick reloadAllComponents];
        [window.rootViewController presentViewController:alertController animated:YES completion:nil];
    });
    return ctrl;
}

#pragma mark pickerView  delegate

//滚动选中的行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
 
}

//每行显示的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSString *str = [NSString stringWithFormat:@"%@",_timerData[row]];
    return str;
}

//列宽
//-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
//
//    return 100;
//}

#pragma mark pickerView  dataSource
// 多少组
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// 行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return _timerData.count;
}



- (void)rds_show {
    
    RDS_WEAKSELF(weakSelf)
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    // 更新UI要在主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [window.rootViewController presentViewController:weakSelf.ac animated:YES completion:nil];
    });
}

- (void)rds_dismiss {
    
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    // 更新UI要在主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [window.rootViewController dismissViewControllerAnimated:YES completion:nil];
    });
}
@end
