//
//  EMSBaseTabBarController.m
//  EquipmentMS
//
//  Created by gui_huan on 2018/7/23.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "SLBaseTabBarController.h"
#import "SLBaseNavigationController.h"
#import "SLBaseViewController.h"
#import "SLHomeViewController.h"
#import "SLLxViewController.h"
#import "SLMyViewController.h"
#import "SLMomentsVc.h"
#import "SLAddressBookVc.h"

@interface SLBaseTabBarController ()<RDVTabBarControllerDelegate,RDVTabBarDelegate>

@end

@implementation SLBaseTabBarController

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

- (void)setAllChildViewControllers
{

    UIViewController *vc1 = [SLHomeViewController loadViewControllewWithNib];
    SLBaseNavigationController *navVC1 = [[SLBaseNavigationController alloc]initWithRootViewController:vc1];
//    UIViewController *vc2 = [SLLxViewController loadViewControllewWithNib];
//    SLBaseNavigationController *navVC2 = [[SLBaseNavigationController alloc]initWithRootViewController:vc2];
    UIViewController *vc2 = [SLAddressBookVc loadViewControllewWithNib];
    SLBaseNavigationController *navVC2 = [[SLBaseNavigationController alloc]initWithRootViewController:vc2];
    
    UIViewController *vc3 = [SLMomentsVc loadViewControllewWithNib];
    SLBaseNavigationController *navVC3 = [[SLBaseNavigationController alloc]initWithRootViewController:vc3];
    UIViewController *vc4 = [SLMyViewController loadViewControllewWithNib];
    SLBaseNavigationController *navVC4 = [[SLBaseNavigationController alloc]initWithRootViewController:vc4];
    self.viewControllers = @[navVC1, navVC2, navVC3, navVC4];
    
    self.tabBar.backgroundColor = [UIColor whiteColor];
    
    NSArray<SLTabBarModel *> *tabBarItems = @[
        [SLTabBarModel tabBarItemWithTitle:@"聊天" image:@"home_tab_icon_lt_"],
        [SLTabBarModel tabBarItemWithTitle:@"联系" image:@"home_tab_icon_lxr_"],
        [SLTabBarModel tabBarItemWithTitle:@"朋友圈" image:@"home_tab_icon_pyq_"],
        [SLTabBarModel tabBarItemWithTitle:@"我的" image:@"home_tab_icon_my_"],];

    [[[self tabBar] items] enumerateObjectsUsingBlock:^(RDVTabBarItem *item , NSUInteger idx, BOOL * _Nonnull stop) {
        SLTabBarModel *model = [tabBarItems objectAtIndex:idx];
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@pre",
                                                      model.image]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@nor",
                                                        model.image]];
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
        item.title = model.title;
        item.unselectedTitleAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14], NSForegroundColorAttributeName: SLColor(@"#ABB7C5")};
        item.selectedTitleAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14], NSForegroundColorAttributeName: SLColor(@"#007AFF")};
        item.imagePositionAdjustment = UIOffsetMake(0, -3);
        item.backgroundColor = [UIColor whiteColor];
        item.accessibilityLabel = item.title;
        if (@available(iOS 10.0, *)) {
            item.accessibilityTraits = UIAccessibilityTraitTabBar;
        } else {
            // Fallback on earlier versions
        }
    }];
    self.selectedIndex = 0;
    self.tabBar.delegate = self;
    [self.tabBar setHeight:SLTabBarHeight + kSafeAreaBottom];
 
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
    if (index == 2) {
//        [viewController.tabBarItem clearBadge];
    }
    return YES;
}

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

@end


@implementation SLTabBarModel

+ (instancetype)tabBarItemWithTitle:(NSString *)title image:(NSString *)image
{
    SLTabBarModel *item = [[SLTabBarModel alloc] init];
    item.title = title;
    item.image = image;
    return item;
}

@end
