//
//  CustomTabBarController.m
//  gaotingshan
//
//  Created by MacBook Dq01 on 16/4/18.
//  Copyright © 2016年 Mac Book DQ02. All rights reserved.
//

#import "CustomTabBarController.h"
#import "HomePageViewController.h"
#import "CompeteViewController.h"
#import "MyViewController.h"
#import "CustomTabBar.h"
#import "GuideMapController.h"
#import "CultureGameViewController.h"
#import "CultureOriginVC.h"


@interface CustomTabBarController () <CustomTabBarDelegate>

@end

@implementation CustomTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.初始化子控制器
    HomePageViewController *homeVC = [[HomePageViewController alloc] init];
    [self addChildVc:homeVC title:@"首页" image:@"shouye" selectedImage:@"shouye-lv"];

    CompeteViewController *competeVC = [[CompeteViewController alloc] init];
    [self addChildVc:competeVC title:@"竞赛" image:@"jingsai" selectedImage:@"jingsai-lv"];

    
    CultureOriginVC *cultureVC = [[CultureOriginVC alloc] init];
    cultureVC.tabBarItem.title = @"文创游";
    // 设置子控制器的图片
    cultureVC.tabBarItem.image = [[UIImage imageNamed:@"wenchuangyou"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    cultureVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"wenchuangyou-lv"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:58/255.0 green:198/255.0 blue:100/255.0 alpha:1];
    [cultureVC.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [cultureVC.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    // 添加为子控制器
    [self addChildViewController:cultureVC];
    
    
    MyViewController *MyVC = [[MyViewController alloc] init];
    [self addChildVc:MyVC title:@"我的" image:@"wode" selectedImage:@"wode-lv"];

    
    // 2.更换系统自带的tabbar
    //    self.tabBar = [[HWTabBar alloc] init];
    CustomTabBar *tabBar = [[CustomTabBar alloc] init];
    tabBar.barTintColor = [UIColor blackColor];
    [tabBar setTintColor:[UIColor greenColor]];
    tabBar.clickDelegate = self;
    
    [self setValue:tabBar forKeyPath:@"tabBar"];
    
}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title; // 同时设置tabbar和navigationBar的文字
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:58/255.0 green:198/255.0 blue:100/255.0 alpha:1];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 先给外面传进来的小控制器 包装 一个导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];
    // 添加为子控制器
    [self addChildViewController:nav];
}

#pragma mark - CustomTabBarDelegate代理方法
- (void)tabBarDidClickguideButton:(CustomTabBar *)tabbar
{
        GuideMapController *vc = [[GuideMapController alloc] init];
        UINavigationController *naVc = [[UINavigationController alloc] initWithRootViewController:vc];
        naVc.navigationBar.translucent = YES;
        [self presentViewController:naVc animated:YES completion:nil];
 
}


-(void)tabbarDidClickCultureButton:(CustomTabBar *)tabbar
{
    CultureGameViewController *vc = [[CultureGameViewController alloc] init];
    UINavigationController *naVc = [[UINavigationController alloc] initWithRootViewController:vc];
    naVc.navigationBar.translucent = YES;
    [self presentViewController:naVc animated:YES completion:nil];
}

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


/*
#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
