//
//  UINavigationBar+DDCategory.m
//  DDCategoryKit_Private
//  Created by DDIsFriend on 2022/11/2.
    

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

@implementation UINavigationBar (DDCategory)

+ (void)load{

    static dispatch_once_t onceToken;

    __weak typeof(self) weakSelf = self;
    dispatch_once(&onceToken, ^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        
        [strongSelf dd_originSelector:@selector(setBarTintColor:) swizzleSelector:@selector(dd_setBarTintColor:)];
        [strongSelf dd_originSelector:@selector(setBackgroundImage:forBarMetrics:) swizzleSelector:@selector(dd_setBackgroundImage:forBarMetrics:)];
        [strongSelf dd_originSelector:@selector(setTitleTextAttributes:) swizzleSelector:@selector(dd_setTitleTextAttributes:)];
        if (@available(iOS 13.0, *)){
            [strongSelf dd_originSelector:@selector(setStandardAppearance:) swizzleSelector:@selector(dd_setStandardAppearance:)];
            [strongSelf dd_originSelector:@selector(setCompactAppearance:) swizzleSelector:@selector(dd_setCompactAppearance:)];
            [strongSelf dd_originSelector:@selector(setScrollEdgeAppearance:) swizzleSelector:@selector(dd_setScrollEdgeAppearance:)];
            if (@available(iOS 15.0, *)){
                [strongSelf dd_originSelector:@selector(setCompactScrollEdgeAppearance:) swizzleSelector:@selector(dd_setCompactScrollEdgeAppearance:)];
            }
        }

    });

}
// MARK: <Change NavigationBar Color And BackgroundImage>
- (void)dd_setBarTintColor:(nullable UIColor *)barTintColor{
    
    [self dd_saveBarBackgroundColor:barTintColor];
    
    [self dd_setBarTintColor:barTintColor];
}

- (void)dd_setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics{
    
    [self dd_saveBarBackgroundImage:backgroundImage];
    
    [self dd_setBackgroundImage:backgroundImage forBarMetrics:barMetrics];
}

- (void)dd_setTitleTextAttributes:(NSDictionary<NSAttributedStringKey,id> *)titleTextAttributes{
    
    [self dd_saveBarTitleTextAttributes:titleTextAttributes];
    
    [self dd_setTitleTextAttributes:titleTextAttributes];
}

- (void)dd_setStandardAppearance:(UINavigationBarAppearance *)standardAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0), tvos(13.0)){
    [self dd_saveBarBackgroundImageOrColorWithAppearance:standardAppearance];

    [self dd_setStandardAppearance:standardAppearance];
}

- (void)dd_setCompactAppearance:(UINavigationBarAppearance *)compactAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0)){
    [self dd_saveBarBackgroundImageOrColorWithAppearance:compactAppearance];

    [self dd_setCompactAppearance:compactAppearance];
}

- (void)dd_setScrollEdgeAppearance:(UINavigationBarAppearance *)scrollEdgeAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0)){
    [self dd_saveBarBackgroundImageOrColorWithAppearance:scrollEdgeAppearance];

    [self dd_setScrollEdgeAppearance:scrollEdgeAppearance];
}

- (void)dd_setCompactScrollEdgeAppearance:(UINavigationBarAppearance *)compactScrollEdgeAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(15.0)){
    [self dd_saveBarBackgroundImageOrColorWithAppearance:compactScrollEdgeAppearance];

    [self dd_setCompactScrollEdgeAppearance:compactScrollEdgeAppearance];
}

- (void)dd_saveBarBackgroundImageOrColorWithAppearance:(UINavigationBarAppearance *)navigationBarAppearance API_AVAILABLE(ios(13.0)) {
    
    [self dd_saveBarBackgroundImage:navigationBarAppearance.backgroundImage color:navigationBarAppearance.backgroundColor];
}

- (void)dd_saveBarBackgroundImage:(nullable UIImage *)dd_barBackgroundImage color:(nullable UIColor *)dd_barBackgroundColor{
    [self dd_saveBarBackgroundImage:dd_barBackgroundImage];
    [self dd_saveBarBackgroundColor:dd_barBackgroundColor];
}

- (void)dd_saveBarBackgroundColor:(nullable UIColor *)dd_barBackgroundColor{

    UINavigationController *navigationController = (UINavigationController *)self.delegate;
    UIViewController *viewController = [navigationController.viewControllers lastObject];
    [viewController dd_saveNavigationBarBackgroundColor:dd_barBackgroundColor];
}

- (void)dd_saveBarBackgroundImage:(nullable UIImage *)dd_barBackgroundImage{

    UINavigationController *navigationController = (UINavigationController *)self.delegate;
    UIViewController *viewController = [navigationController.viewControllers lastObject];
    [viewController dd_saveNavigationBarBackgroundImage:dd_barBackgroundImage];
}

- (void)dd_saveBarTitleTextAttributes:(NSDictionary<NSAttributedStringKey,id> *)dd_titleTextAttributes{
    UINavigationController *navigationController = (UINavigationController *)self.delegate;
    UIViewController *viewController = [navigationController.viewControllers lastObject];
    [viewController dd_saveNavigationBarTitleTextAttributes:dd_titleTextAttributes];
}

@end
