//
//  DDBaseViewController.m
//  DDBasicControlsKit_Private
//  Created by DDIsFriend on 2022/10/27.

#import "DDBaseViewController.h"
#import <DDCategoryKit_Private/UIImage+DDCategory.h>
#import <DDLogKit_Private/DDOCLog.h>

@interface DDBaseViewController ()

@end

@implementation DDBaseViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self configNavigationBar];
        [self configView];
    }
    return self;
}

- (nullable instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self configNavigationBar];
        [self configView];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
}

// MARK: <NavigationBar Config>
- (void)configNavigationBar{
    // 隐藏默认的返回按钮
    self.navigationItem.hidesBackButton = YES;
    
    // navigationBar Attributes.
    [self defaultNavigationBarBackgroundColor];
    [self defaultNavigationBarTitleTextAttributes];
    [self defaultNavigationBarBarButtonItemAttributes];
    [self defaultNavigationBarLeftBarButtonItems];
}

- (void)defaultNavigationBarBackgroundColor{
    self.dd_navigationBarBackgroundColor = UIColor.whiteColor;
}

- (void)defaultNavigationBarTitleTextAttributes{
    self.dd_navigationBarTitleTextAttributes = @{NSForegroundColorAttributeName : UIColor.blackColor,NSFontAttributeName : [UIFont systemFontOfSize:17 weight:UIFontWeightMedium]};
}

- (void)defaultNavigationBarBarButtonItemAttributes{
    self.dd_navigationBarBarButtonItemAttributes = @{NSForegroundColorAttributeName : UIColor.blackColor,NSFontAttributeName : [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]};
}

- (void)defaultNavigationBarLeftBarButtonItems{
    UIImage *backButtonImage = [UIImage dd_imageNamed:@"navigation_back_arrow" bundleName:@"DDBaseViewController" aClass:self.class];
    [self dd_backButtonWithImage:backButtonImage action:@selector(defaultBackActionPopViewController)];
}

- (void)defaultBackActionPopViewController{
    [self dd_backActionPopViewController:YES];
}

- (void)dd_backButtonWithImage:(nullable UIImage *)backButtonImage action:(SEL)sel{
    self.navigationItem.leftBarButtonItems = nil;
    self.navigationItem.leftBarButtonItems = [self dd_backBarButtonItem:[self barButtonItemWithImage:backButtonImage title:nil target:self action:sel] WithOtherItems:nil];
}

- (nullable UIBarButtonItem *)barButtonItemWithImage:(nullable UIImage *)image title:(nullable NSString *)title target:(nullable id)target action:(nullable SEL)action {
    
    if (image != nil){
        return [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:target action:action];
    }else if (title != nil){
        return [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];
    }
    
    return nil;
}

- (nullable NSArray<UIBarButtonItem *> *)dd_backBarButtonItem:(nonnull UIBarButtonItem *)backBarButtonItem WithOtherItems:(nullable NSArray<UIBarButtonItem *> *)items{
   
    _dd_backBarButtonItem = backBarButtonItem;
    UIBarButtonItem *flexibleSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    NSArray *backItems = [[NSArray arrayWithObjects:backBarButtonItem,flexibleSpaceItem, nil] arrayByAddingObjectsFromArray:items];
    
    return backItems;
}

- (void)dd_backActionPopViewController:(BOOL)isAnimated{
    [self.navigationController popViewControllerAnimated:isAnimated];
}

- (void)dd_backActionPopToRootViewController:(BOOL)isAnimated{
    [self.navigationController popToRootViewControllerAnimated:isAnimated];
}

// MARK: <View>
- (void)configView{
//    self.view.backgroundColor = [UIColor whiteColor];
}

// MARK: <NavigationItemTitle>
- (void)setDd_navigationItemTitle:(NSString *)navigationItemTitle{
    _dd_navigationItemTitle = navigationItemTitle;
    self.navigationItem.title = navigationItemTitle;
}

- (void)setDd_navigationItemTitleView:(UIView *)navigationItemTitleView{
    _dd_navigationItemTitleView = navigationItemTitleView;
    self.navigationItem.titleView = navigationItemTitleView;
}

// MARK: <UIStatusBarStyle>
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden{
    return NO;
}

// MARK: <UIOrientation>
- (void)dd_setInterfaceOrientation:(UIInterfaceOrientation)orientation {
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        UIInterfaceOrientation val = orientation;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

// MARK: <Dealloc>
- (void)dealloc{
    DDLog(@"Attention:控制器 ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----%@ dealloc",[self class]);
}
@end
