//
//  RDSBaseTableViewController.m
//  SmartLightForBigFish
//
//  Created by coderYK on 2017/8/7.
//  Copyright © 2017年 RD-iOS. All rights reserved.
//

#import "RDSBaseTableViewController.h"
#import "NSString+hanzi.h"

@interface RDSBaseTableViewController ()

@end

@implementation RDSBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = RDSViewBgColor;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    //[self p_addRefreshHeader];
}

- (void)p_addRefreshHeader {
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(rds_refresh)];
    
    header.automaticallyChangeAlpha = YES;
    
    header.lastUpdatedTimeLabel.hidden = YES;
    
    [header setTitle:@"" forState:MJRefreshStateIdle];
    [header setTitle:@"" forState:MJRefreshStatePulling];
    [header setTitle:@"" forState:MJRefreshStateRefreshing];
    [header setTitle:@"" forState:MJRefreshStateWillRefresh];
    
    self.tableView.mj_header = header;
}

- (void)rds_refresh {
    
    DDLog(@"请在子类中实现");
}

- (void)rds_showJuHuaOnWindow {
    
    if (self.juHua == nil) {
        UIActivityIndicatorView *juHua = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        self.juHua = juHua;
        self.juHua.hidesWhenStopped = YES;
        [[UIApplication sharedApplication].keyWindow addSubview:self.juHua];
        self.juHua.center = [UIApplication sharedApplication].keyWindow.center;
    }
    [self.juHua startAnimating];
}



- (void)rds_alertTitle:(NSString *)title message:(NSString *)message doneAction:(void(^)(void))doneActionBlock {
    
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:title
                                                                message:message
                                                         preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        
        if (doneActionBlock) {
            doneActionBlock();
        }
    }];

    [ac addAction:okAction];           // A
    
    [self presentViewController:ac animated:YES completion:nil];
}

- (void)rds_alertTitle:(NSString *)title message:(NSString *)message doneAction:(void (^)(void))doneActionBlock cancelAction:(void (^)(void))cancelActionBlock{
    
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:title
                                                                message:message
                                                         preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        
        if (doneActionBlock) {
            doneActionBlock();
        }
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
        if (cancelActionBlock) {
            cancelActionBlock();
        }
    }];

    [ac addAction:okAction];           // A
    [ac addAction:cancelAction];       // B
    
    [self presentViewController:ac animated:YES completion:nil];
}

- (void)rds_alertInputTitle:(NSString *)title msg:(NSString *)msg placeholder:(NSString *)placeholder doneAction:(void(^)(NSString *name))doneActionBlock{
    
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:title
                                                                message:msg
                                                         preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加文本框
    [ac addTextFieldWithConfigurationHandler:^(UITextField * textField) {
        //textField.placeholder = placeholder;
        textField.text = placeholder;
        [textField addTarget:self action:@selector(p_alertInputDidChange:) forControlEvents:UIControlEventEditingChanged];
    }];
    
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        NSString *string = ac.textFields.firstObject.text;
        if (doneActionBlock) {
            doneActionBlock(string);
        }
    }];
    
    okAction.enabled = NO;
    [ac addAction:cancelAction];
    [ac addAction:okAction];
    
    
    [self presentViewController:ac animated:YES completion:nil];
}

- (void)p_alertInputDidChange:(UITextField *)sender{
    
    UIAlertController *ac = (UIAlertController *)self.presentedViewController;
    if (ac) {
        UIAlertAction *okAction = ac.actions.lastObject;
        NSString *string = ac.textFields.firstObject.text;
        NSUInteger length = [string rds_countStringByteLength];
        
        if (length >0 && length <= 16) {
            okAction.enabled = YES;
            ac.message = @"";
        }
        else if (length == 0){
            okAction.enabled = NO;
            ac.message = @"";
        }
        else if (length > 16){
            okAction.enabled = NO;
            ac.message = @"名称过长";
        }
    }
}



#pragma mark - Nav
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.navigationController != nil) {
        [self.navigationController pushViewController:viewController animated:animated];
    }
    else{
        [self.tabBarController.navigationController pushViewController:viewController animated:animated];
    }
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
    
    if (self.navigationController != nil) {
        return [self.navigationController popViewControllerAnimated:animated];
    }
    else{
        return [self.tabBarController.navigationController popViewControllerAnimated:animated];
    }
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.navigationController != nil) {
        return [self.navigationController popToViewController:viewController animated:animated];
    }
    else{
        return [self.tabBarController.navigationController popToViewController:viewController animated:animated];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
