//

//  TaskHallViewController.m

//  yybjproject

//

//  Created by bingjun on 15/10/27.

//  Copyright © 2015 bingjun. All rights reserved.

//

 

#import "TaskHomeHallViewController.h"

#import "UINavigationBar+Awesome.h"

#import "HomeViewController.h"

#import "moreSensationViewController.h"

 

#import "SVSegmentedControl.h"

#import "TaskListViewController.h"

#import "MyTaskViewController.h"

#import "TaskHallViewController.h"

 

@interface TaskHomeHallViewController ()

{

    MyTaskViewController *_myTaskVc;///< 红人任务控制器

    

    moreSensationViewController *_moreSensationVc;///< 红人榜单控制器

    TaskHallViewController *_shopTrolleyVc;///< 清单界面

    

    NSInteger _segmentSelectedIndex;///< 当前segmentindex

    

    UIButton *leftBtn;///< 城市的名字

}

@end

 

@implementation TaskHomeHallViewController

 

#pragma mark - tileView的一些设置

- (void)segmentedControlChangedValue:(SVSegmentedControl*)segmentedControl {

    

//    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

    segmentedControl.userInteractionEnabled = NO;

    if (_segmentSelectedIndex == segmentedControl.selectedSegmentIndex) {

//        [[UIApplication sharedApplication] endIgnoringInteractionEvents];

        segmentedControl.userInteractionEnabled = YES;

        return;

    }

    UIViewController *beforeVc;

    switch (_segmentSelectedIndex) {

        case 0:

            beforeVc = _myTaskVc;

            break;

        case 1:

            beforeVc = _shopTrolleyVc;

            break;

        case 2:

            beforeVc = _moreSensationVc;

            break;

        default:

            break;

    }

    //启动后先添加first,大儿子

    switch (segmentedControl.selectedSegmentIndex) {

        case 0:

        {

            _segmentSelectedIndex = segmentedControl.selectedSegmentIndex;

            //调用addChildViewController之前会自动调用secondwillMoveToParentViewController

            [self addChildViewController:_myTaskVc];

            

            [_myTaskVc.view setFrame:_myTaskVc.view.frame];

            

            [self transitionFromViewController:beforeVc toViewController:_myTaskVc duration:1.0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished){

                //删除之前first之前手动添加的willMoveToParentViewController

                [beforeVc willMoveToParentViewController:nil];

                //调用removeFromParentViewController之后会自动调用firstdidMoveToParentViewController

                [beforeVc removeFromParentViewController];

                //addChildViewController之后手动添加的didMoveToParentViewController

                [_myTaskVc didMoveToParentViewController:self];

//                [[UIApplication sharedApplication] endIgnoringInteractionEvents];

                segmentedControl.userInteractionEnabled = YES;

            }];

            break;

        }

        case 1:

        {

            _segmentSelectedIndex = segmentedControl.selectedSegmentIndex;

            [self addChildViewController:_shopTrolleyVc];

            

            [_shopTrolleyVc.view setFrame:_myTaskVc.view.frame];

            [self transitionFromViewController:beforeVc toViewController:_shopTrolleyVc duration:1.0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished){

                //addChildViewController之后手动添加的didMoveToParentViewController

                //删除之前first之前手动添加的willMoveToParentViewController

                [beforeVc willMoveToParentViewController:nil];

                //调用removeFromParentViewController之后会自动调用firstdidMoveToParentViewController

                [beforeVc removeFromParentViewController];

                [_shopTrolleyVc didMoveToParentViewController:self];

//                [[UIApplication sharedApplication] endIgnoringInteractionEvents];

                segmentedControl.userInteractionEnabled = YES;

            }];

            break;

        }

        case 2:

        {

            _segmentSelectedIndex = segmentedControl.selectedSegmentIndex;

            [self addChildViewController:_moreSensationVc];

            [_moreSensationVc.view setFrame:_myTaskVc.view.frame];

            [self transitionFromViewController:beforeVc toViewController:_moreSensationVc duration:1.0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished){

                //删除之前first之前手动添加的willMoveToParentViewController

                [beforeVc willMoveToParentViewController:nil];

                //调用removeFromParentViewController之后会自动调用firstdidMoveToParentViewController

                [beforeVc removeFromParentViewController];

                //addChildViewController之后手动添加的didMoveToParentViewController

                [_moreSensationVc didMoveToParentViewController:self];

//                [[UIApplication sharedApplication] endIgnoringInteractionEvents];

                segmentedControl.userInteractionEnabled = YES;

            }];

            break;

        }

        default:

            break;

    }

    

}

 

//设置titleVIew

- (void)initTitleViewOfMyTask

{

    NSArray *array;

    if (kDeviceWidth > 320) {

        array = [NSArray arrayWithObjects:@"  ", @"  ", @"  ", nil];

    }else {

        array = [NSArray arrayWithObjects:@"任务", @"红人", @"榜单", nil];

    }

    

    SVSegmentedControl *yellowRC = [[SVSegmentedControl alloc] initWithSectionTitles:array];

    [yellowRC addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];

    //    yellowRC.crossFadeLabelsOnDrag = YES;

    yellowRC.animateToInitialSelection = YES;

    yellowRC.font = [UIFont systemFontOfSize:14];

    yellowRC.textColor = [UIColor whiteColor];//没有被选中的字体颜色

    //    yellowRC.titleEdgeInsets = UIEdgeInsetsMake(0, 14, 0, 14);

    yellowRC.height = 30;

    yellowRC.backgroundTintColor = YSRedProjectColor;//没有被选中的背景颜色

    //  yellowRC.textShadowColor = [UIColor whiteColor];

    [yellowRC setSelectedSegmentIndex:0 animated:NO];

    yellowRC.thumb.tintColor = [UIColor whiteColor];//选中的背景颜色

    yellowRC.thumb.textColor = YSRedProjectColor;//选中的title字体颜色

    yellowRC.thumb.textShadowColor = [UIColor whiteColor];

    //    yellowRC.thumb.textShadowOffset = CGSizeMake(0, 1);

    yellowRC.thumb.gradientIntensity = 0.05;

    yellowRC.center = CGPointMake(180, 100);

    

    self.navigationItem.titleView = yellowRC;

    _segmentSelectedIndex = 0;

}

 

//添加右上角按钮

- (void)addLeftBarButtonItemAction

{

    CGFloat kLefyViewWidth;

    CGFloat kImageViewWidth;

    CGFloat kTitleViewWidth;

    CGFloat kTitleFontSize;

    if (kDeviceWidth > 320) {

        kLefyViewWidth = 75.0f;

        kImageViewWidth = 14.0f;

        kTitleViewWidth = 60.0f;

        kTitleFontSize = 13.0f;

    }else {

        kLefyViewWidth = 60.0f;

        kImageViewWidth = 10.0f;

        kTitleViewWidth = 50.0f;

        kTitleFontSize = 10.0f;

    }

    

    UIView *rightItemView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kLefyViewWidth, 34.0f)];

    rightItemView.backgroundColor = [UIColor clearColor];

    

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, (34 - kImageViewWidth) / 2.0f, kImageViewWidth, kImageViewWidth)];

    imageView.image = [UIImage imageNamed:@"positionIcon.png"];

    imageView.contentMode = UIViewContentModeScaleAspectFit;

    [rightItemView addSubview:imageView];

    

    leftBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    

    User *user = [[User instance] getUserInformation];

    if (user.userCity && ![user.userCity isKindOfClass:[NSNull class]]) {

        [leftBtn setTitle:user.userCity forState:UIControlStateNormal];

    }else {

        [leftBtn setTitle:@"暂无" forState:UIControlStateNormal];

    }

    leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    leftBtn.frame=CGRectMake(kImageViewWidth, 0.0f, kTitleViewWidth, 34.0f);

    [leftBtn.titleLabel setFont:[UIFont systemFontOfSize:kTitleFontSize]];

    [rightItemView addSubview:leftBtn];

    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:rightItemView];

    self.navigationItem.leftBarButtonItem = leftItem;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    

    [self addLeftBarButtonItemAction];

    

    //设置导航栏颜色

    self.navigationController.navigationBar.translucent = YES;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

        self.edgesForExtendedLayout = UIRectEdgeNone;

    }

    [self.navigationController.navigationBar lt_setBackgroundColor:YSRedProjectColor];

    

    HomeViewController *home = [HomeViewController singleton];

    //    HomeViewController *home = self.navigationController.tabBarController;

    [home showTabBar:YES];

    

    //设置titleView

    [self initTitleViewOfMyTask];

    

    //生俩儿子

    _myTaskVc = [[MyTaskViewController alloc] initWithNibName:@"MyTaskViewController" bundle:nil];

    _moreSensationVc = [[moreSensationViewController alloc] initWithNibName:nil bundle:nil];

    _shopTrolleyVc = [[TaskHallViewController alloc] initWithNibName:@"TaskHallViewController" bundle:nil];

    _shopTrolleyVc.isNeedShowSearchFlag = YES;

    

    //启动后先添加first,大儿子

    [self addChildViewController:_myTaskVc];

    [_myTaskVc.view setFrame:self.view.frame];

    [self.view addSubview:_myTaskVc.view];

    [_myTaskVc didMoveToParentViewController:self];

    

    [[NSNotificationCenter defaultCenter]addObserver:self

                                            selector:@selector(update_CityName:)

                                                name:Update_CityName object: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.

}

*/

 

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    

    HomeViewController *home = [HomeViewController singleton];

    [home showTabBar:NO];

}

 

- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    

    HomeViewController *home = [HomeViewController singleton];

    [home hideTabBar:NO];

}

 

- (void)update_CityName:(NSNotification *)notification

{

     NSString *cityName = notification.object;

    [leftBtn setTitle:cityName forState:UIControlStateNormal];

}

@end