//
//  TabBarViewController.m
//  Runner
//
//  Created by zhengdu on 2021/6/28.
//

#import "TabBarViewController.h"
#import "UICommon.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "ScanCodeViewController.h"
#import "JCManager.h"
#import "TabMessageViewController.h"

@interface TabBarViewController ()<UITabBarControllerDelegate>




@end

@implementation TabBarViewController

+ (void)initialize
{
    //Tabbar不透明
    [UITabBar appearance].translucent = NO;
    //Tabbar黑色
    [UITabBar appearance].backgroundImage = [UICommon imageWithColor:KKWhiteColorColor];
    //隐藏Tabbar分割线
    UIImage *clearImage = [UICommon imageWithColor:KKWhiteColorColor];
    [[UITabBar appearance] setShadowImage:clearImage];
    //TableView适配
    [UITableView appearance].estimatedRowHeight = 0;
    [UITableView appearance].estimatedSectionHeaderHeight = 0;
    [UITableView appearance].estimatedSectionFooterHeight = 0;
    //ScrollView适配
    if (@available(iOS 11.0, *)){
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }
}


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (@available(iOS 15.0, *))
    {
        UITabBarAppearance *barAppearance = [[UITabBarAppearance alloc] init];
        barAppearance.backgroundEffect = nil;
        self.tabBar.scrollEdgeAppearance = barAppearance;
        self.tabBar.standardAppearance = barAppearance;
    }
    else
    {
        // Fallback on earlier versions
        [UITabBar appearance].translucent = NO;
    }
    self.delegate = self;
    [self addChildViewControllers];
    [self addTabarItems];
    [self bindingEvent];
}


- (void)addChildViewControllers
{
 
    ZXNavigationBarNavigationController *homeVC = [[ZXNavigationBarNavigationController alloc] initWithRootViewController:[HomeViewController new]];

    ZXNavigationBarNavigationController *mapVC = [[ZXNavigationBarNavigationController alloc] initWithRootViewController:[MapViewController new]];
    
    ZXNavigationBarNavigationController *msgVC = [[ZXNavigationBarNavigationController alloc] initWithRootViewController:[TabMessageViewController new]];

    ZXNavigationBarNavigationController *myVC =  [[ZXNavigationBarNavigationController alloc] initWithRootViewController:[MyViewController new]];
   

    self.viewControllers = @[homeVC,
                             mapVC,
                             msgVC,
                             myVC];

    self.tabBar.tintColor = KKMainColor;
}

- (void)addTabarItems
{
    NSArray *titleNames = @[GJText(@"首页"),
                            GJText(@"定位"),
                            GJText(@"消息"),
                            GJText(@"我的")];
    
    NSArray *normalImages = @[@"tabBar_home_normal",
                              @"tabBar_map_normal",
                              @"tabBar_msg_normal",
                              @"tabBar_my_normal"];
    
    NSArray *selectedImages = @[@"tabBar_home_selected",
                                @"tabBar_map_selected",
                                @"tabBar_msg_selected",
                                @"tabBar_my_selected"];
    
    //SNJJSELF
    [self.childViewControllers enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL *stop) {
        
        obj.tabBarItem.title = [titleNames objectAtIndex:idx];
        obj.tabBarItem.image = [[UIImage imageNamed:normalImages[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        obj.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImages[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
    }];
    
    if (@available(iOS 13.0, *))
    {
        self.tabBar.unselectedItemTintColor = KKTextBlackColor;
        self.tabBar.tintColor = KKMainColor;
        self.tabBar.backgroundColor = KKWhiteColorColor;
    }
    else
    {
        self.tabBar.backgroundColor = KKWhiteColorColor;
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: KKTextBlackColor} forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: KKMainColor} forState:UIControlStateSelected];
    }
}



- (void)bindingEvent
{
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:sRelogin object:nil] subscribeNext:^(NSNotification *x) {
        if (![self.navigationController.viewControllers.firstObject isKindOfClass:LoginViewController.class])
        {
            NSMutableArray *controllers = [self.navigationController.viewControllers mutableCopy];
            LoginViewController *loginController = [[LoginViewController alloc] init];
            [controllers insertObject:loginController atIndex:0];
            self.navigationController.viewControllers = controllers;
        }
        //退出登录清除用户信息
        [[APIManager sharedManager] clearUserInfo];
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
        [[JCManager shared] uninitialize];
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [app showRootViewController];
    }];
}


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    UINavigationController *selectNav = (UINavigationController*)viewController;
    if (APIManager.sharedManager.deviceModel.imei.length <= 0 && [NSStringFromClass([selectNav.topViewController class]) isEqualToString:@"MapViewController"])
    {
        self.selectedIndex = 0;
        EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:GJText(@"提示") subtitle:GJText(@"您还未绑定设备，请先绑定设备！") AlertViewType:AlertViewTypeSystemAlert config:nil];
        [alertV addAlertItem:^EasyAlertItem *{
            return [EasyAlertItem itemWithTitle:GJText(@"取消") type:AlertItemTypeSystemCancel callback:^(EasyAlertView *showview, long index) {
  
            }];
        }];
        [alertV addAlertItem:^EasyAlertItem *{
            return [EasyAlertItem itemWithTitle:GJText(@"前往") type:AlertItemTypeSystemDefault callback:^(EasyAlertView *showview, long index) {
                ScanCodeViewController *vc = [ScanCodeViewController new];
                [[UICommon currentVC].navigationController pushViewController:vc animated:YES];
            }];
        }];
        [alertV showAlertView];
        return NO;
    }
    return YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
