//
//  UINavigationController+Rotation.m
//  iBrainAgeApp
//
//  Created by Q16 on 8/29/19.
//  Copyright © 2019 Anren. All rights reserved.
//
#import "UIController+Rotation.h"
#import <objc/runtime.h>

@implementation UIViewController (ShouldAutorotate)
// 是否支持自动转屏

- (BOOL)shouldAutorotate
{
//    if (!self.childViewControllers.count) return YES;
    return NO;
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}

// 默认的屏幕方向（当前ViewController必须是通过模态出来的UIViewController（模态带导航的无效）方式展现出来的，才会调用这个方法）
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  return UIInterfaceOrientationPortrait;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
  return UIStatusBarStyleDefault;
}
- (BOOL)prefersStatusBarHidden {
  return NO;
}
@end


@implementation UITabBarController (ShouldAutorotate)
+ (void)load {
  SEL selectors[] = {
    @selector(selectedIndex)
  };
  for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
    SEL originalSelector = selectors[index];
    SEL swizzledSelector = NSSelectorFromString([@"wm_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
    Method originalMethod = class_getInstanceMethod(self, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
    if (class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
      class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
      method_exchangeImplementations(originalMethod, swizzledMethod);
    }
  }
}

- (NSInteger)wm_selectedIndex {
  NSInteger index = [self wm_selectedIndex];
  if (index > self.viewControllers.count){
    return 0;
  }
  return index;
}
// 是否支持自动转屏
- (BOOL)shouldAutorotate {
  UIViewController *vc = self.viewControllers[self.selectedIndex];
  if ([vc isKindOfClass:UINavigationController.class]) {
    UINavigationController *nav = (UINavigationController *)vc;
    return [nav.topViewController shouldAutorotate];
  } else {
    return [vc shouldAutorotate];
  }
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  UIViewController *vc = self.viewControllers[self.selectedIndex];
  if ([vc isKindOfClass:UINavigationController.class]) {
    UINavigationController *nav = (UINavigationController *)vc;
    return [nav.topViewController supportedInterfaceOrientations];
  } else {
    return [vc supportedInterfaceOrientations];
  }
}
// 默认的屏幕方向（当前ViewController必须是通过模态出来的UIViewController（模态带导航的无效）方式展现出来的，才会调用这个方法）
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  UIViewController *vc = self.viewControllers[self.selectedIndex];
  if ([vc isKindOfClass:UINavigationController.class]) {
    UINavigationController *nav = (UINavigationController *)vc;
    return [nav.topViewController preferredInterfaceOrientationForPresentation];
  } else {
    return [vc preferredInterfaceOrientationForPresentation];
  }
}
@end

@implementation UINavigationController (ShouldAutorotate)
// 是否支持自动转屏
- (BOOL)shouldAutorotate {
  return [self.topViewController shouldAutorotate];
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  return [self.topViewController supportedInterfaceOrientations];
}
// 默认的屏幕方向（当前ViewController必须是通过模态出来的UIViewController（模态带导航的无效）方式展现出来的，才会调用这个方法）
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (UIViewController *)childViewControllerForStatusBarStyle {
  return self.topViewController;
}
- (UIViewController *)childViewControllerForStatusBarHidden {
  return self.topViewController;
}

@end

@implementation UIAlertController (ShouldAutorotate)
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations; {
  return UIInterfaceOrientationMaskAll;
}
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  return UIInterfaceOrientationMaskAll;
}
#endif
@end



@implementation UINavigationController (Rotation)

- (BOOL)shouldAutorotate {
  return ![[self.viewControllers lastObject] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations {
  return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end
