//
//  UINavigationController+DDCategory.m
//  DDCategoryKit_Private
//  Created by DDIsFriend on 2022/9/22.

#import "UINavigationController+DDCategory.h"
#import <objc/runtime.h>
#import <DDCategoryKit_Private/NSObject+DDCategory.h>

@interface UINavigationController ()

@end

@implementation UINavigationController (DDCategory)
@dynamic dd_navigationBarHeight;

+ (void)load{

    static dispatch_once_t onceToken;
    __weak typeof(self) weakSelf = self;
    dispatch_once(&onceToken, ^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        
        [strongSelf dd_originSelector:@selector(viewDidLoad) swizzleSelector:@selector(dd_navViewDidLoad)];
        [strongSelf dd_originSelector:@selector(viewWillAppear:) swizzleSelector:@selector(dd_navViewWillAppear:)];
        [strongSelf dd_originSelector:@selector(viewDidAppear:) swizzleSelector:@selector(dd_navViewDidAppear:)];
        [strongSelf dd_originSelector:@selector(pushViewController:animated:) swizzleSelector:@selector(dd_pushViewController:animated:)];
        [strongSelf dd_originSelector:@selector(popToRootViewControllerAnimated:) swizzleSelector:@selector(dd_popToRootViewControllerAnimated:)];
        [strongSelf dd_originSelector:@selector(popViewControllerAnimated:) swizzleSelector:@selector(dd_popViewControllerAnimated:)];
    });
    
}

- (void)dd_navViewDidLoad{
    [self dd_navViewDidLoad];
}

- (void)dd_navViewWillAppear:(BOOL)animated{
    [self dd_navViewWillAppear:animated];
}

- (void)dd_navViewDidAppear:(BOOL)animated{
    [self dd_navViewDidAppear:animated];
}

// MARK: <Push>
- (void)dd_pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [self dd_endEditingAtController:self.topViewController];
    if (self.dd_hidesBottomBarWhenPushed == YES) {
        [self dd_hidesBottomBarWhenPushedAtChildController:viewController];
    }
    [self dd_pushViewController:viewController animated:animated];
}

// MARK: <Pop>
- (nullable NSArray<__kindof UIViewController *> *)dd_popToRootViewControllerAnimated:(BOOL)animated{
    NSArray *vcs = [self dd_popToRootViewControllerAnimated:animated];
    [self dd_endEditingAtController:(UIViewController *)(vcs.lastObject)];
    return vcs;
}

- (UIViewController *)dd_popViewControllerAnimated:(BOOL)animated{
    UIViewController *vc = [self dd_popViewControllerAnimated:animated];
    [self dd_endEditingAtController:vc];
    return vc;
}

// MARK: <Custom Property>
- (CGFloat)dd_navigationBarHeight{
    return (self.navigationBar.frame.size.height + (self.navigationItem.prompt != nil ? 34 : 0) + (self.navigationBar.prefersLargeTitles == YES ? 34 : 0));
}

- (BOOL)dd_hidesBottomBarWhenPushed{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)setDd_hidesBottomBarWhenPushed:(BOOL)dd_hidesBottomBarWhenPushed{
    objc_setAssociatedObject(self, @selector(dd_hidesBottomBarWhenPushed), @(dd_hidesBottomBarWhenPushed), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

// MARK: <Custom Function>
/// push控制器时，需要将当前view的键盘去掉
- (void)dd_endEditingAtController:(UIViewController *)controller{
    [controller.view endEditing:YES];
}

/// push控制器时，需要将子控制器的tabbar去掉
- (void)dd_hidesBottomBarWhenPushedAtChildController:(UIViewController *)viewController{
    if (self.viewControllers.count >= 1){
        viewController.hidesBottomBarWhenPushed = YES;
    }
}

@end
