//
//  IBTUIViewController.m
//  IBTImagePicker
//
//  Created by Xummer on 1/17/15.
//  Copyright (c) 2015 Xummer. All rights reserved.
//

#import "IBTUIViewController.h"

@interface IBTUIViewController ()

@end

@implementation IBTUIViewController

#pragma mark - Life Cycle

- (instancetype)init {
    self = [super init];
    if (self) {
        self.isShowNavigationBar = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = kBacroundColor;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"fafafa"];
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]  initWithImage:[UIImage imageNamed:@"bac"] style:UIBarButtonItemStyleDone target:self action:@selector(popAction)];
   leftBtn.tintColor = kTextColorDarkGray;
    self.navigationItem.leftBarButtonItem = leftBtn;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:21],NSFontAttributeName, [UIColor colorWithHexString:@"333333"], NSForegroundColorAttributeName,nil]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    

    // Do any additional setup after loading the view.
}



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

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (!self.navigationController) {
        return;
    }
    
    NSInteger viewCtrlsCount = [self.navigationController.viewControllers count];
    if (!_bNotAutoAddCancelButton &&
        viewCtrlsCount == 1 &&
        !self.navigationItem.leftBarButtonItems &&
        [self isPresentedIn])
    {
        if (!_cancelButtonName) {
            self.cancelButtonName = [IBTCommon localizableString:@"Cancel"];
        }
        [self addLeftBarBtnItemWithName:_cancelButtonName action:@selector(disMissSelf)];
    }
    
    [self.navigationController setNavigationBarHidden:!self.isShowNavigationBar animated:YES];

    

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self addKeyboardNotification];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self removeKeyboardNotification];
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}

- (void)popAction {
    [self.view endEditing:YES];
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [self.navigationController.navigationBar endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Public Method
- (void)safeSetEdgesForExtendedLayout:(UIRectEdge)extendedLayout {
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = extendedLayout;
    }
}

- (BOOL)isPresentedIn {
    return self.presentingViewController.presentedViewController == self
    || self.navigationController.presentingViewController.presentedViewController == self.navigationController
    || [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]];
}

- (BOOL)isPushedIn {
    if ([self.navigationController.viewControllers count] > 1) {
        return self != [self.navigationController.viewControllers firstObject];
    }
    else {
        return NO;
    }
}

- (void)disMissSelf {
    [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - Route

// Below iOS6 use this method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

// Above iOS6 use these two methods
- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

#pragma mark - iOS7 Status bar

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}

- (BOOL)automaticallyAdjustsScrollViewInsets {
    return NO;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationNone;
}

- (void)updateStatueBarAppearance {
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        [self setNeedsStatusBarAppearanceUpdate];
    }
}

#pragma mark - Keyboard Notification

- (void)addKeyboardNotification {
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter addObserver:self
                   selector:@selector(keyboardWillShow:)
                       name:UIKeyboardWillShowNotification
                     object:nil];
    [notiCenter addObserver:self
                   selector:@selector(keyboardWillHide:)
                       name:UIKeyboardWillHideNotification
                     object:nil];
}

- (void)removeKeyboardNotification {
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter removeObserver:self
                          name:UIKeyboardWillShowNotification
                        object:nil];
    [notiCenter removeObserver:self
                          name:UIKeyboardWillHideNotification
                        object:nil];
}

- (void)keyboardWillShow:(NSNotification *)note {
    
}

- (void)keyboardWillHide:(NSNotification *)note {
    
}

- (void)dealloc {
    CLog(@"%@注销了",[NSString stringWithUTF8String:object_getClassName(self)]);
}

@end
