//
//  RDSNavController.m
//  LampRibbon
//
//  Created by coderYK on 2017/8/7.
//  Copyright © 2017年 RD-iOS. All rights reserved.
//

#import "RDSNavController.h"
#import "UIImage+Extension.h"
#import "RDSRootControl.h"
#import "RDSTabbarController.h"

@interface RDSNavController ()

@property (nonatomic, strong) UIBarButtonItem *backItem;
@property (nonatomic, strong) UIBarButtonItem *space;

@property (nonatomic, strong) UIImage *navFillImg; // ** <#注释#>*/
@property (nonatomic, strong) UIImage *navCirImg; // ** <#注释#>*/
@end

@implementation RDSNavController

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.childViewControllers.count > 0) {
        
        
        
        viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
        //viewController.navigationItem.leftBarButtonItems = @[self.space, self.backItem];
        viewController.navigationItem.leftBarButtonItems = @[self.backItem];
        
        if ([viewController isKindOfClass:[RDSTabbarController class]]) {
            if (self.childViewControllers.count < 2) {
                viewController.navigationItem.leftBarButtonItems = nil;
                viewController.navigationItem.hidesBackButton = YES;
            }
        }
    }
    
    [super pushViewController:viewController animated:animated];
}


- (void)p_popViewController {
    
    [self popViewControllerAnimated:YES];
}


- (UIBarButtonItem *)backItem {
    
    if (_backItem == nil) {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
        backBtn.frame = CGRectMake(0, 0, 30, 30);
        [backBtn addTarget:self action:@selector(p_popViewController) forControlEvents:UIControlEventTouchUpInside];
        _backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    }
    return _backItem;
}

- (UIBarButtonItem *)space {
    
    if (_space == nil) {
        _space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                               target:nil action:nil];
        _space.width = -15;
    }
    return _space;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航栏背景图片
//    [self.navigationBar setBackgroundImage:[UIImage rds_imageWithColor:RDSNavBarColor] forBarMetrics:UIBarMetricsDefault];
//
//    // 去掉导航栏下边的黑边
//    [self.navigationBar setShadowImage:[UIImage new]];
//
//    // 设置标题字体、颜色
//    UIFont *font = [UIFont rds_titiFontStyle:FontStyle_SemiBold size:22];
//    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:font}];
    //
    
    
    [self navBarColor:RDSNavBarColor];
//    [self updateNavigationBarColor:RDSNavBarColor];
    
    //    [self p_setupNavBar];
    
}

- (void)navBarColor:(UIColor *)color{
    //navigation标题文字颜色
    NSDictionary *dic = @{NSForegroundColorAttributeName:RGB(39, 46, 59),
                          NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
//        [barApp configureWithOpaqueBackground];
        barApp.backgroundColor = color;
        barApp.shadowColor = UIColor.clearColor;
        barApp.titleTextAttributes = dic;
        self.navigationBar.scrollEdgeAppearance = barApp;
        self.navigationBar.standardAppearance = barApp;
    }else{
        //背景色
        self.navigationBar.barTintColor = color;
        self.navigationBar.titleTextAttributes = dic;
        [self.navigationBar setShadowImage:[UIImage new]];
        [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    }
    //不透明
    self.navigationBar.translucent = NO;
    //navigation控件颜色
    self.navigationBar.tintColor = [UIColor whiteColor];
}

- (void)updateNavigationBarColor:(UIColor *)color {
    UINavigationBar *bar = self.navigationBar;// 重置背景和阴影颜色
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *barAppearance = [UINavigationBarAppearance new];
        [barAppearance configureWithOpaqueBackground];
        barAppearance.backgroundColor = color; // 设置背景颜色
        barAppearance.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor],
                                              NSFontAttributeName : [UIFont systemFontOfSize:24]}; // 设置导航栏字体颜色和大小
        barAppearance.shadowColor = [UIColor clearColor]; // 设置导航栏底部的分割线不显示
        
        bar.scrollEdgeAppearance = bar.standardAppearance = barAppearance;
        [bar setShadowImage:[UIImage new]];
    } else {
        // Fallback on earlier versions
    }
    [bar setBackgroundImage:[UIImage rds_imageWithColor:color] forBarMetrics:UIBarMetricsDefault];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    UIViewController *topVC = self.topViewController;
    return [topVC preferredStatusBarStyle];
}

-(void) p_setupNavBar{
    
    UIImage *cirImg = [UIImage imageNamed:@"nav_mine_top"];
    // 四个数值对应图片中距离上、左、下、右边界的不拉伸部分的范围宽度
    cirImg = [cirImg resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingModeStretch];
    self.navCirImg = cirImg;
    
    UIImage *fillImg = [UIImage imageNamed:@"nav_top_fill"];
    // 四个数值对应图片中距离上、左、下、右边界的不拉伸部分的范围宽度
    fillImg = [fillImg resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingModeStretch];
    self.navFillImg = fillImg;
    
    [self.navigationBar setBackgroundImage:cirImg forBarMetrics:UIBarMetricsDefault];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
