//
//  UIViewController+DDCategory.m
//  DDCategoryKit_Private
//  Created by DDIsFriend on 2022/9/23.

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

@interface UIViewController ()

@end

@implementation UIViewController (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(viewDidLoad) swizzleSelector:@selector(dd_viewDidLoad)];
        [strongSelf dd_originSelector:@selector(viewWillAppear:) swizzleSelector:@selector(dd_viewWillAppear:)];
        [strongSelf dd_originSelector:@selector(viewDidAppear:) swizzleSelector:@selector(dd_viewDidAppear:)];
        [strongSelf dd_originSelector:@selector(viewWillDisappear:) swizzleSelector:@selector(dd_viewWillDisappear:)];
        [strongSelf dd_originSelector:@selector(viewDidDisappear:) swizzleSelector:@selector(dd_viewDidDisappear:)];
    });
}

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

- (void)dd_viewWillAppear:(BOOL)animated{
    // When the interface is displayed, the color or image of the navigationBar at the current controller needs to be updated.
    UIImage *navigationBarBackgroundImage;
    UIColor *navigationBarBackgroundColor;
    UIImage *navigationBarShadowImage;
    UIColor *navigationBarShadowColor;
    NSDictionary<NSAttributedStringKey,id> *navigationBarTitleTextAttributes;
    
    if (@available(iOS 13.0, *)){
        navigationBarBackgroundImage = self.navigationController.navigationBar.standardAppearance.backgroundImage;
        navigationBarBackgroundColor = self.navigationController.navigationBar.standardAppearance.backgroundColor;
        navigationBarShadowImage = self.navigationController.navigationBar.standardAppearance.shadowImage;
        navigationBarShadowColor = self.navigationController.navigationBar.standardAppearance.shadowColor;
        navigationBarTitleTextAttributes = self.navigationController.navigationBar.standardAppearance.titleTextAttributes;
    }else{
        navigationBarBackgroundImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
        navigationBarBackgroundColor = self.navigationController.navigationBar.barTintColor;
        navigationBarShadowImage = self.navigationController.navigationBar.shadowImage;
        navigationBarTitleTextAttributes = self.navigationController.navigationBar.titleTextAttributes;
    }
 
    if (@available(iOS 13.0, *)) {
        if (!(self.dd_navigationBarBackgroundImage == navigationBarBackgroundImage && CGColorEqualToColor(self.dd_navigationBarBackgroundColor.CGColor, navigationBarBackgroundColor.CGColor) && self.dd_navigationBarShadowImage == navigationBarShadowImage && self.dd_navigationBarShadowColor == navigationBarShadowColor && self.dd_navigationBarTitleTextAttributes == navigationBarTitleTextAttributes)){
            [self dd_changeNavigationBarbackgroundImage:self.dd_navigationBarBackgroundImage color:self.dd_navigationBarBackgroundColor shadowImage:self.dd_navigationBarShadowImage color:self.dd_navigationBarShadowColor titleTextAttributes:self.dd_navigationBarTitleTextAttributes];
        }
    }else{
        if (!(self.dd_navigationBarBackgroundImage == navigationBarBackgroundImage && CGColorEqualToColor(self.dd_navigationBarBackgroundColor.CGColor, navigationBarBackgroundColor.CGColor) && self.dd_navigationBarShadowImage == navigationBarShadowImage && self.dd_navigationBarTitleTextAttributes == navigationBarTitleTextAttributes)){
            [self dd_changeNavigationBarbackgroundImage:self.dd_navigationBarBackgroundImage color:self.dd_navigationBarBackgroundColor shadowImage:self.dd_navigationBarShadowImage color:nil titleTextAttributes:self.dd_navigationBarTitleTextAttributes];
        }
    }
  
    
    [self dd_changeNavigationBarBarButtonItemAttributes:self.dd_navigationBarBarButtonItemAttributes];
    
    [self dd_viewWillAppear:animated];
}

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

}

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

- (void)dd_viewDidDisappear:(BOOL)animated{

    [self dd_viewDidDisappear:animated];
   
}

// MARK: <NavigationBar Color,BackgroundImage,TextAttributes>
- (nullable UIColor *)dd_navigationBarBackgroundColor{
    return objc_getAssociatedObject(self, _cmd);
}

- (nullable UIImage *)dd_navigationBarBackgroundImage{
    return objc_getAssociatedObject(self, _cmd);
}

- (nullable UIColor *)dd_navigationBarShadowColor{
    return objc_getAssociatedObject(self, _cmd);
}

- (nullable UIImage *)dd_navigationBarShadowImage{
    return objc_getAssociatedObject(self, _cmd);
}

- (nullable NSDictionary<NSAttributedStringKey,id> *)dd_navigationBarTitleTextAttributes{
    return objc_getAssociatedObject(self, _cmd);
}

- (nullable NSDictionary<NSAttributedStringKey,id> *)dd_navigationBarBarButtonItemAttributes{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setDd_navigationBarBackgroundImage:(nullable UIImage *)dd_navigationBarBackgroundImage{

    [self dd_saveNavigationBarBackgroundImage:dd_navigationBarBackgroundImage];
    
    if (@available(iOS 13.0, *)) {
        [self dd_changeNavigationBarbackgroundImage:dd_navigationBarBackgroundImage color:self.dd_navigationBarBackgroundColor shadowImage:self.dd_navigationBarShadowImage color:self.dd_navigationBarShadowColor titleTextAttributes:self.dd_navigationBarTitleTextAttributes];
    } else {
        // Fallback on earlier versions
        [self dd_changeNavigationBarbackgroundImage:dd_navigationBarBackgroundImage color:self.dd_navigationBarBackgroundColor shadowImage:self.dd_navigationBarShadowImage color:nil titleTextAttributes:self.dd_navigationBarTitleTextAttributes];
    }
}

- (void)setDd_navigationBarBackgroundColor:(nullable UIColor *)dd_navigationBarBackgroundColor{

    [self dd_saveNavigationBarBackgroundColor:dd_navigationBarBackgroundColor];
    
    if (@available(iOS 13.0, *)) {
        [self dd_changeNavigationBarbackgroundImage:self.dd_navigationBarBackgroundImage color:dd_navigationBarBackgroundColor shadowImage:self.dd_navigationBarShadowImage color:self.dd_navigationBarShadowColor titleTextAttributes:self.dd_navigationBarTitleTextAttributes];
    } else {
        // Fallback on earlier versions
        [self dd_changeNavigationBarbackgroundImage:self.dd_navigationBarBackgroundImage color:dd_navigationBarBackgroundColor shadowImage:self.dd_navigationBarShadowImage color:nil titleTextAttributes:self.dd_navigationBarTitleTextAttributes];
    }
}

- (void)setDd_navigationBarShadowColor:(nullable UIColor *)dd_navigationBarShadowColor{
    [self dd_saveNavigationBarShadowColor:dd_navigationBarShadowColor];
    
    [self dd_changeNavigationBarbackgroundImage:self.dd_navigationBarBackgroundImage color:self.dd_navigationBarBackgroundColor shadowImage:self.dd_navigationBarShadowImage color:dd_navigationBarShadowColor titleTextAttributes:self.dd_navigationBarTitleTextAttributes];
}

- (void)setDd_navigationBarShadowImage:(nullable UIImage *)dd_navigationBarShadowImage{
    [self dd_saveNavigationBarShadowImage:dd_navigationBarShadowImage];
    
    if (@available(iOS 13.0, *)) {
        [self dd_changeNavigationBarbackgroundImage:self.dd_navigationBarBackgroundImage color:self.dd_navigationBarBackgroundColor shadowImage:dd_navigationBarShadowImage color:self.dd_navigationBarShadowColor titleTextAttributes:self.dd_navigationBarTitleTextAttributes];
    } else {
        // Fallback on earlier versions
        [self dd_changeNavigationBarbackgroundImage:self.dd_navigationBarBackgroundImage color:self.dd_navigationBarBackgroundColor shadowImage:dd_navigationBarShadowImage color:nil titleTextAttributes:self.dd_navigationBarTitleTextAttributes];
    }
}

- (void)setDd_navigationBarTitleTextAttributes:(nullable NSDictionary<NSAttributedStringKey,id> *)dd_navigationBarTitleTextAttributes{
    [self dd_saveNavigationBarTitleTextAttributes:dd_navigationBarTitleTextAttributes];
    
    if (@available(iOS 13.0, *)) {
        [self dd_changeNavigationBarbackgroundImage:self.dd_navigationBarBackgroundImage color:self.dd_navigationBarBackgroundColor shadowImage:self.dd_navigationBarShadowImage color:self.dd_navigationBarShadowColor titleTextAttributes:dd_navigationBarTitleTextAttributes];
    } else {
        // Fallback on earlier versions
        [self dd_changeNavigationBarbackgroundImage:self.dd_navigationBarBackgroundImage color:self.dd_navigationBarBackgroundColor shadowImage:self.dd_navigationBarShadowImage color:nil titleTextAttributes:dd_navigationBarTitleTextAttributes];
    }
}

- (void)setDd_navigationBarBarButtonItemAttributes:(nullable NSDictionary<NSAttributedStringKey,id> *)dd_navigationBarBarButtonItemAttributes{
    [self dd_saveNavigationBarBarButtonItemAttributes:dd_navigationBarBarButtonItemAttributes];
    
    [self dd_changeNavigationBarBarButtonItemAttributes:dd_navigationBarBarButtonItemAttributes];
}

- (void)dd_saveNavigationBarBackgroundImage:(nullable UIImage *)dd_navigationBarBackgroundImage{
    objc_setAssociatedObject(self, @selector(dd_navigationBarBackgroundImage), dd_navigationBarBackgroundImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)dd_saveNavigationBarBackgroundColor:(nullable UIColor *)dd_navigationBarBackgroundColor{
    objc_setAssociatedObject(self, @selector(dd_navigationBarBackgroundColor), dd_navigationBarBackgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)dd_saveNavigationBarShadowColor:(nullable UIColor *)dd_navigationBarShadowColor {
    objc_setAssociatedObject(self, @selector(dd_navigationBarShadowColor), dd_navigationBarShadowColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)dd_saveNavigationBarShadowImage:(nullable UIImage *)dd_navigationBarShadowImage {
    objc_setAssociatedObject(self, @selector(dd_navigationBarShadowImage), dd_navigationBarShadowImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)dd_saveNavigationBarTitleTextAttributes:(NSDictionary<NSAttributedStringKey,id> *)dd_navigationBarTitleTextAttributes{
    objc_setAssociatedObject(self, @selector(dd_navigationBarTitleTextAttributes), dd_navigationBarTitleTextAttributes, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)dd_saveNavigationBarBarButtonItemAttributes:(NSDictionary<NSAttributedStringKey,id> *)dd_navigationBarBarButtonItemAttributes{
    objc_setAssociatedObject(self, @selector(dd_navigationBarBarButtonItemAttributes), dd_navigationBarBarButtonItemAttributes, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)dd_changeNavigationBarbackgroundImage:(nullable  UIImage *)dd_navigationBarBackgroundImage color:(nullable UIColor *)dd_navigationBarBackgroundColor shadowImage:(nullable UIImage *)dd_navigationBarShadowImage color:(nullable UIColor *)dd_navigationBarShadowColor titleTextAttributes:(NSDictionary<NSAttributedStringKey,id> *)dd_navigationBarTitleTextAttributes{
    if (@available(iOS 13.0,*)){
        UINavigationBarAppearance *navigationBarAppearance = [[UINavigationBarAppearance alloc] init];
        [navigationBarAppearance configureWithTransparentBackground];
        
        navigationBarAppearance.backgroundImage = dd_navigationBarBackgroundImage;
        navigationBarAppearance.backgroundColor = dd_navigationBarBackgroundColor;
        navigationBarAppearance.shadowImage = dd_navigationBarShadowImage;
        navigationBarAppearance.shadowColor = dd_navigationBarShadowColor;
        navigationBarAppearance.titleTextAttributes = dd_navigationBarTitleTextAttributes;
        self.navigationController.navigationBar.standardAppearance = navigationBarAppearance;
        self.navigationController.navigationBar.compactAppearance = navigationBarAppearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = navigationBarAppearance;
        if (@available(iOS 15.0, *)){
            self.navigationController.navigationBar.compactScrollEdgeAppearance = navigationBarAppearance;
        }
        
    }else{
        
        self.navigationController.navigationBar.barTintColor = dd_navigationBarBackgroundColor;
        
        [self.navigationController.navigationBar setBackgroundImage:dd_navigationBarBackgroundImage forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setBackgroundImage:dd_navigationBarBackgroundImage forBarMetrics:UIBarMetricsCompact];
        [self.navigationController.navigationBar setBackgroundImage:dd_navigationBarBackgroundImage forBarMetrics:UIBarMetricsDefaultPrompt];
        [self.navigationController.navigationBar setBackgroundImage:dd_navigationBarBackgroundImage forBarMetrics:UIBarMetricsCompactPrompt];
        
        [self.navigationController.navigationBar setShadowImage:dd_navigationBarShadowImage];
        
        self.navigationController.navigationBar.titleTextAttributes = dd_navigationBarTitleTextAttributes;
        
    }
}

- (void)dd_changeNavigationBarBarButtonItemAttributes:(NSDictionary<NSAttributedStringKey,id> *)dd_navigationBarBarButtonItemAttributes{
    
    if (self.navigationItem.leftBarButtonItems.count > 0){
        for (int i = 0; i < self.navigationItem.leftBarButtonItems.count; i++) {
            UIBarButtonItem *barButtonItem = self.navigationItem.leftBarButtonItems[i];
            [barButtonItem setTitleTextAttributes:dd_navigationBarBarButtonItemAttributes forState:UIControlStateNormal];
            [barButtonItem setTitleTextAttributes:dd_navigationBarBarButtonItemAttributes forState:UIControlStateHighlighted];
        }
    }

    if (self.navigationItem.rightBarButtonItems.count > 0){
        for (int i = 0; i < self.navigationItem.rightBarButtonItems.count; i++) {
            UIBarButtonItem *barButtonItem = self.navigationItem.rightBarButtonItems[i];
            [barButtonItem setTitleTextAttributes:dd_navigationBarBarButtonItemAttributes forState:UIControlStateNormal];
            [barButtonItem setTitleTextAttributes:dd_navigationBarBarButtonItemAttributes forState:UIControlStateHighlighted];
        }
    }
    // update system backBarButtonItem's color, but this item's font we didn't modify it,usually we will replace this item by our custom items,so the problem of font is unimportant.
    UIColor *tintColor = [dd_navigationBarBarButtonItemAttributes objectForKey:NSForegroundColorAttributeName];
    if (tintColor != nil && tintColor != self.navigationController.navigationBar.tintColor) {
        self.navigationController.navigationBar.tintColor = dd_navigationBarBarButtonItemAttributes[NSForegroundColorAttributeName];
        // self.navigationController.navigationBar.backItem's title font didn't modified.
    }

}
@end
