//
//  WorkFlowTabbarController.m
//  smartRhino
//
//  Created by Android on 2019/12/17.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "WorkFlowTabbarController.h"
#import "WorkFlowHomeListController.h"
#import "MyWorkFlowListController.h"
#import "WorkFlowFecordController.h"
#import "NavigationController.h"
#import "HomeViewController.h"
#import "MyViewController.h"
#import "AppDelegate.h"
#import "My_CenterVC.h"
#import "ChatMsgListVC.h"
#import "HomeTestViewController.h"
#import "TabBarController.h"

@interface WorkFlowTabbarController ()

@property (nonatomic, strong) UIView * dotImage;

@end

@implementation WorkFlowTabbarController

- (instancetype)init {
    if (self = [super init]) {
     
    }
    return self;
}
- (void)didMoveToParentViewController:(UIViewController*)parent
{
    [super didMoveToParentViewController:parent];
    if(!parent){
        [TabBarController sharedTabBarController].tabBar.hidden = NO;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [TabBarController sharedTabBarController].tabBar.hidden = YES;
    self.fd_prefersNavigationBarHidden = YES;
    [self addchlidVcs];
    [self setTabBarItemStyle];
    //池塘红点
    // 设置一个自定义 View,大小等于 tabBar 的大小
    UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    // 给自定义 View 设置颜色
    bgView.backgroundColor = [UIColor whiteColor];
    // 将自定义 View 添加到 tabBar 上
    [self.tabBar insertSubview:bgView atIndex:0];
}

- (BOOL)hidesBottomBarWhenPushed
{
    return NO;
}

/**
 *  根据参数, 创建并添加对应的子控制器
 *
 *  @param vc                需要添加的控制器(会自动包装导航控制器)
 *  @param isRequired             标题
 *  @param normalImageName   一般图片名称
 *  @param selectedImageName 选中图片名称
 */
- (void)addChildVC: (UIViewController *)vc  title:(NSString *)title normalImageName: (NSString *)normalImageName selectedImageName:(NSString *)selectedImageName isRequiredNavController: (BOOL)isRequired {
    //    vc.view.backgroundColor = [UIColor whiteColor];
    if (isRequired) {
        NavigationController *nav = [[NavigationController alloc] initWithRootViewController:vc];
        UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[self originImageWithName:normalImageName] selectedImage:[self originImageWithName:selectedImageName]];
        nav.tabBarItem = tabBarItem;
        vc.title = title;
        [self addChildViewController:nav];
    }else {
        UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[self originImageWithName:normalImageName] selectedImage:[self originImageWithName:selectedImageName]];
        vc.tabBarItem = tabBarItem;
        vc.title = title;
        [self addChildViewController:vc];
    }
}

- (void)addchlidVcs {
    [self addChildVC:[[WorkFlowHomeListController alloc] init] title:@"会议室预订" normalImageName:@"workFlowHome" selectedImageName:@"workFlowHomeHigh" isRequiredNavController:YES];
    [self addChildVC:[[MyWorkFlowListController alloc] init] title:@"我的会议" normalImageName:@"myFlow" selectedImageName:@"myFlowHigh" isRequiredNavController:YES];
    [self addChildVC:[[WorkFlowFecordController alloc] init]  title:@"会议纪要" normalImageName:@"workFlowFecord" selectedImageName:@"workFlowFecordHigh" isRequiredNavController:YES];
}

- (void)setTabBarItemStyle{
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:k6} forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kMainColor} forState:UIControlStateSelected];
}

- (instancetype)className:(NSString *)className {
    id controller = [NSClassFromString(className) new];
    return controller;
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    [super setSelectedIndex:selectedIndex];
}

- (UIImage *)originImageWithName:(NSString *)imageName{
    return [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

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



#pragma mark 池塘红点
- (void)chatMsgRedPoint:(NSInteger)number {
    [self.dotImage removeFromSuperview];
    if(number == 0) {
        return;
    }else{
        CGFloat redPointX = (SCREEN_WIDTH / 4) * 3 - (SCREEN_WIDTH / 4)/2;
        CGFloat x = ceilf(redPointX) + 5;
        CGFloat y = 5;
        self.dotImage = [[UIView alloc] initWithFrame:CGRM(x, y, 10, 10)];
        UILabel *numLabel = [[UILabel alloc] init];
        self.dotImage.width = 16;
        self.dotImage.height = 16;
        self.dotImage.layer.cornerRadius = 8;
        [self.dotImage addSubview:numLabel];
        numLabel.frame = CGRectMake(0, 0, self.dotImage.width, self.dotImage.height);
        numLabel.font = [UIFont systemFontOfSize:11.0];
        numLabel.textAlignment = NSTextAlignmentCenter;
        numLabel.textColor = RGB(255, 255, 255);
        numLabel.text = [NSString stringWithFormat:@"%ld",(long)number];
        
        self.dotImage.backgroundColor = RGB(255, 82, 82);
        [self.tabBar addSubview:self.dotImage];
    }
}

@end