//
//  SLBaseViewController.m
//  SLAiELTS
//
//  Created by Gusont on 2023/2/27.
//

#import "SLBaseViewController.h"

@interface SLBaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation SLBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = SLBgColor;
    [self initNavigationBar];
    self.navigationController.navigationBarHidden = YES;
    self.navigationBar.leftBarButtonItem = [self backBarButtonItem];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    //导航的rootViewController关闭右滑返回功能
    if (self.navigationController.viewControllers.count <= 1){
        return NO;
    }
    return YES;
}

- (YMNavigationBar *)navigationBar
{
    if (!_navigationBar) {
        _navigationBar = [[YMNavigationBar alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kNavBarHeight)];
        _navigationBar.separatorColor = SLBgColor;
        _navigationBar.separatorHeight = 1.0f;
        [self.view addSubview:_navigationBar];
    }
    return _navigationBar;
}

- (void)initNavigationBar
{
    self.navigationBar.backgroundColor = [UIColor whiteColor];
    self.navigationBar.backgroundImage = nil;
    self.navigationBar.titleItem.font = [UIFont systemFontOfSize:17];
    self.navigationBar.titleItem.titleColor = [UIColor blackColor];
}

- (nullable YMBarButtonItem *)backBarButtonItem
{
    if (self.hideBackBtn) {
        return nil;
    }
    UIImage *backImage =[UIImage imageNamed:@"icon_nav_back"];
    YMBarButtonItem *backBarButtonItem =[[YMBarButtonItem alloc]initWithImage:backImage style:YMBarButtonItemPlain target:self action:@selector(backBarButtonPressed:)];
    backBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    return backBarButtonItem;
}

- (void)backBarButtonPressed:(id)pressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated
{
    [UIView animateWithDuration:0.3 animations:^{
        self.navigationBar.hidden = YES;
    }];
}

- (void)setTitle:(NSString *)title
{
    self.navigationBar.titleItem.title = title;
}

- (void)navPushViewController:(UIViewController *)vc animated:(BOOL)animated
{
    if (self.rdv_tabBarController) {
        [self.rdv_tabBarController.navigationController pushViewController:vc animated:YES];
    }else {
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)dealloc {
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    CheckRunWhere;
}

@end
