//
//  UIViewController+General.m
//  AIIM
//
//  Created by qitewei on 2025/5/5.
//

#import "UIViewController+General.h"
#import <objc/runtime.h>

// 关联对象Key
static const char *kBackButtonTitleKey = "kBackButtonTitleKey";
static const char *kBackButtonImageKey = "kBackButtonImageKey";
static const char *kBackButtonColorKey = "kBackButtonColorKey";
static const char *kDisablePopGestureKey = "kDisablePopGestureKey";

@implementation UIViewController (General)
#pragma mark - Navigation Bar 基本配置
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:hidden animated:animated];
}

- (void)setNavigationBarTransparent:(BOOL)transparent {
    if (transparent) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage new];
        self.navigationController.navigationBar.translucent = YES;
    } else {
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = nil;
        self.navigationController.navigationBar.translucent = NO;
    }
}

- (void)setNavigationBarBackgroundColor:(UIColor *)color {
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];
        appearance.backgroundColor = color;
        appearance.shadowColor = [UIColor clearColor];
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    } else {
        self.navigationController.navigationBar.barTintColor = color;
        self.navigationController.navigationBar.translucent = NO;
    }
}

- (void)setNavigationTitle:(NSString *)title {
    self.navigationItem.title = title;
}

- (void)setNavigationTitleColor:(UIColor *)color font:(UIFont *)font {
    NSDictionary *attributes = @{
        NSForegroundColorAttributeName: color,
        NSFontAttributeName: font
    };
    
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appearance = self.navigationController.navigationBar.standardAppearance ?: [[UINavigationBarAppearance alloc] init];
        appearance.titleTextAttributes = attributes;
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    } else {
        [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    }
}

#pragma mark - 返回按钮配置
- (void)setBackButtonTitle:(NSString *)title {
    objc_setAssociatedObject(self, kBackButtonTitleKey, title, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self configureBackButton];
}

- (void)setBackButtonImage:(UIImage *)image {
    objc_setAssociatedObject(self, kBackButtonImageKey, image, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self configureBackButton];
}

- (void)setBackButtonColor:(UIColor *)color {
    objc_setAssociatedObject(self, kBackButtonColorKey, color, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self configureBackButton];
}

- (void)disableInteractivePopGesture:(BOOL)disable {
    objc_setAssociatedObject(self, kDisablePopGestureKey, @(disable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.navigationController.interactivePopGestureRecognizer.enabled = !disable;
}

- (void)configureBackButton {
    NSString *title = objc_getAssociatedObject(self, kBackButtonTitleKey) ?: @"";
    UIImage *image = objc_getAssociatedObject(self, kBackButtonImageKey) ?: [UIImage imageNamed:@"fanhui"];
    UIColor *color = objc_getAssociatedObject(self, kBackButtonColorKey) ?: [UIColor whiteColor];
    
    UIBarButtonItem *backItem;
    if (image) {
        backItem = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                   style:UIBarButtonItemStylePlain
                                                  target:self
                                                  action:@selector(popViewController)];
    } else {
        backItem = [[UIBarButtonItem alloc] initWithTitle:title
                                                    style:UIBarButtonItemStylePlain
                                                   target:self
                                                   action:@selector(popViewController)];
    }
    
    [backItem setTitleTextAttributes:@{NSForegroundColorAttributeName: color} forState:UIControlStateNormal];
    self.navigationItem.backBarButtonItem = nil;
    self.navigationItem.leftBarButtonItem = backItem;
}

#pragma mark - 添加自定义按钮
- (void)addBarButtonWithTitle:(NSString *)title position:(BarButtonItemPosition)position action:(SEL)action {
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:title
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                              action:action];
    [self addBarButtonItem:button atPosition:position];
}

- (void)addBarButtonWithImage:(UIImage *)image position:(BarButtonItemPosition)position action:(SEL)action {
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                              action:action];
    [self addBarButtonItem:button atPosition:position];
}

- (void)addBarButtonWithCustomView:(UIView *)view position:(BarButtonItemPosition)position {
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:view];
    [self addBarButtonItem:button atPosition:position];
}

- (void)addBarButtonItem:(UIBarButtonItem *)item atPosition:(BarButtonItemPosition)position {
    if (position == BarButtonItemPositionLeft) {
        NSMutableArray *leftItems = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems ?: @[]];
        [leftItems addObject:item];
        self.navigationItem.leftBarButtonItems = leftItems;
    } else {
        NSMutableArray *rightItems = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems ?: @[]];
        [rightItems addObject:item];
        self.navigationItem.rightBarButtonItems = rightItems;
    }
}

- (void)removeBarButtonAtPosition:(BarButtonItemPosition)position {
    if (position == BarButtonItemPositionLeft) {
        self.navigationItem.leftBarButtonItems = nil;
    } else {
        self.navigationItem.rightBarButtonItems = nil;
    }
}

#pragma mark - 工具方法
- (void)popViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)popToRootViewController {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end
