//
//  BaseViewController.m
//  TheoryNetwork
//
//  Created by tederen on 2019/9/20.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "BaseViewController.h"
#import "DrawerView.h"
#import "TabBarController.h"

@interface BaseViewController ()

@property (strong,nonatomic) DrawerView *drawerView;

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"首页-书籍详情-返回"] style:0 target:self action:@selector(back)];
    if (kBooliPhoneX) {
        self.customNavigationHeight.constant = 88.f;
    }else {
        self.customNavigationHeight.constant = 64.f;
    }
}

-(void)back{
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

-(IBAction)backAction:(id)sender{
    if(self.drawerView){
        [self.drawerView removeFromSuperview];
    }
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(IBAction)menuAction:(id)sender{
    [self.view endEditing:YES];
    [[UtilsTools getWindow] addSubview:self.drawerView];
    WS(weakSelf);
    self.drawerView.SelectDrawerBlock = ^(NSIndexPath * _Nonnull indexPath) {
        if (![weakSelf.tabBarController isKindOfClass:[TabBarController class]]) {
            [TabBarController sharedTabBarController].tabBar.hidden = NO;
            [weakSelf.tabBarController.navigationController popViewControllerAnimated:NO];
        }
        if ([weakSelf.navigationController.viewControllers count] > 1) {
            [weakSelf.navigationController popToRootViewControllerAnimated:NO];
        }
        switch (indexPath.section) {
            case 0:
            {
                [[TabBarController sharedTabBarController] setSelectedIndex:indexPath.row];
            }
                break;
            case 1:
            {
                switch (indexPath.row) {
                    case 0:
                    {
                        [[TabBarController sharedTabBarController] setSelectedIndex:2];
                    }
                        break;
                    case 1:
                    {
                        [[TabBarController sharedTabBarController] setSelectedIndex:2];
                    }
                        break;
                    default:
                    {
                        [[TabBarController sharedTabBarController] setSelectedIndex:3];
                    }
                        break;
                }
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(indexPath.row)}];
                });
            }
                break;
            default:
            {
                [[TabBarController sharedTabBarController] setSelectedIndex:3];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(4 + indexPath.row)}];
                });
            }
                break;
        }
    };
    self.drawerView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);// - kNavigationHeight
}
//- (BOOL)prefersStatusBarHidden{
//    return  YES;
//}
//给外部提供一个可调用打开菜单View的方法
-(void)openMenuViewWithDidMenuBtn{
    [self menuAction:nil];
}


//给外部提供一个可调用返回上一级的方法
-(void)backToUpVcWithDidBackBtn{
    [self backAction:nil];
}
 
-(DrawerView *)drawerView{
    if(!_drawerView){
        _drawerView = [[DrawerView alloc] init];
    }
    return _drawerView;
}

- (UILabel *)noDataMessageLbl {
    if (!_noDataMessageLbl) {
        _noDataMessageLbl = [[UILabel alloc] init];
        _noDataMessageLbl.font = [UIFont systemFontOfSize:15];
        _noDataMessageLbl.textColor = [UIColor colorWithHexString:@"BBBBBB"];
        _noDataMessageLbl.textAlignment = NSTextAlignmentCenter;
        _noDataMessageLbl.hidden = YES;
    }
    return _noDataMessageLbl;
}
- (void)addNoDataMessageToViewCenter:(UIView *)contentView message:(NSString *)message {
    [self.noDataMessageLbl removeFromSuperview];
    self.noDataMessageLbl.text = message;
    [contentView addSubview:self.noDataMessageLbl];
    [self.noDataMessageLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(contentView.mas_centerY);
        make.centerX.equalTo(contentView.mas_centerX);
        make.height.mas_equalTo(30);
    }];
}
- (void)changeMessageStatusWithNumber:(NSInteger)number {
    if (number > 0) {
        self.noDataMessageLbl.hidden = YES;
    }else {
        self.noDataMessageLbl.hidden = NO;
    }
}

@end