//
//  UIViewController+Additions.m
//  RealEstateManagement
//
//  Created by Z on 16/7/23.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "UIViewController+Additions.h"

@interface UIViewController (Addtions)

@end

@implementation UIViewController (Additions)
- (id)storyboardType:(STORYBOARD_TYPE_)type identifier:(NSString *)identifier
{
    UIStoryboard *storyboard;
    switch (type) {
        case STORYBOARD_TYPE_MAIN: {
            storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_REPAIR: {
            storyboard = [UIStoryboard storyboardWithName:@"Repair" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_INSPECT: {
            storyboard = [UIStoryboard storyboardWithName:@"Inspect" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_READING: {
            storyboard = [UIStoryboard storyboardWithName:@"Reading" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_SUGGEST: {
            storyboard = [UIStoryboard storyboardWithName:@"Suggest" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_SALEINPUT: {
            storyboard = [UIStoryboard storyboardWithName:@"SaleInput" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_OPERINSPECT:{
            storyboard = [UIStoryboard storyboardWithName:@"OperInspect" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_TENANT: {
            storyboard = [UIStoryboard storyboardWithName:@"TenantCommunicate" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_BRIEF_REPORT:{
            storyboard = [UIStoryboard storyboardWithName:@"BriefReport" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_AMMETER:{
            storyboard = [UIStoryboard storyboardWithName:@"Ammeter" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_TODO:{
            storyboard = [UIStoryboard storyboardWithName:@"ToDo" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_YUEXING:{
            storyboard = [UIStoryboard storyboardWithName:@"YueXing" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_TENNANT_MANAGEMENT:{
            storyboard = [UIStoryboard storyboardWithName:@"TenantManagement" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_BRAND_MANAGEMENT:{
            storyboard = [UIStoryboard storyboardWithName:@"BrandManagement" bundle:nil];
            break;
        }
        case STORYBOARD_TYPE_STATEMENT:{
            storyboard = [UIStoryboard storyboardWithName:@"Statement" bundle:nil];
            break;
        }
    }
    
    return [storyboard instantiateViewControllerWithIdentifier:identifier];
}

/**
 *  从不同storyBoard获取控制器（这里storyBoard的id必须和类名一致）
 *
 *  @param type storyBoard类型
 *
 *  @return 对应控制器
 */
+ (id __nonnull)viewControllerWithStoryBoardType:(STORYBOARD_TYPE_)type
{
    NSString *identifier = NSStringFromClass(self);
    UIViewController *vc = [[self alloc] init];
    return [vc storyboardType:type identifier:identifier];
}

- (void)alertTitle:(NSString *__nullable)title
               msg:(NSString *__nullable)msg
          okAction:(void (^__nullable)(UIAlertAction *__nullable action))okAction
      cancelAction:(void (^__nullable)(UIAlertAction *__nullable action))cancelAction
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:okAction];
    [alert addAction:ok];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:cancelAction];
    [alert addAction:cancel];
    if (self.navigationController) {
        [self.navigationController presentViewController:alert animated:YES completion:nil];
    }else{
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
}

- (BOOL)j_popToViewController:(Class _Nonnull)vc {
    for (id obj in self.navigationController.viewControllers) {
        if ([obj isKindOfClass:vc]) {
            [self.navigationController popToViewController:obj animated:YES];
            return YES;
            break;
        }
    }
    
    return NO;
}

- (void)hasPermission:(BOOL)permission yes:(void (^)(void))yes no:(void (^)(void))no
{
    if (permission) {
        if (yes) {
            yes();
        }
    } else {
        if (no) {
            no();
        }
    }
}

- (BOOL)regIsValidDecimalInputtextField:(UITextField *)textField Range:(NSRange)range replacementString:(NSString *)string
{
    //获取改变之后的字符串
    NSMutableString *futureString = [NSMutableString stringWithString:textField.text];
    [futureString insertString:string atIndex:range.location];
    //金额输入
    NSString *reg = @"^-?$|^-0$|^\\d?$|^-?[1-9]\\d*$|^-?0\\.\\d*$|^-?[1-9]\\d*\\.\\d{0,2}$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", reg];
    return [numberPre evaluateWithObject:futureString];
}

//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC
{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    
    return currentVC;
}

+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        
        currentVC = rootVC;
    }
    
    return currentVC;
}

@end
