// Copyright 2016 Google Inc. All Rights Reserved.

#import "GADUPluginUtil.h"

@interface UIView (unityStub)
@property UILayoutGuide *safeAreaLayoutGuide;
@end

BOOL GADUIsOperatingSystemAtLeastVersion(NSInteger majorVersion) {
  NSProcessInfo *processInfo = NSProcessInfo.processInfo;
  if ([processInfo respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
    // iOS 8+.
    NSOperatingSystemVersion version = {majorVersion};
    return [processInfo isOperatingSystemAtLeastVersion:version];
  } else {
    // pre-iOS 8. App supports iOS 7+, so this process must be running on iOS 7.
    return majorVersion >= 7;
  }
}

static CGFloat GADUSafeWidthLandscape(void) {
  CGRect screenBounds = [UIScreen mainScreen].bounds;
  if (GADUIsOperatingSystemAtLeastVersion(11)) {
    CGRect safeFrame = UIApplication.sharedApplication.keyWindow.safeAreaLayoutGuide.layoutFrame;
    if (!CGSizeEqualToSize(safeFrame.size, CGSizeZero)) {
      screenBounds = safeFrame;
    }
  }
  return MAX(CGRectGetWidth(screenBounds), CGRectGetHeight(screenBounds));
}

UIInterfaceOrientation GADUUIInterfaceOrientationForGADUScreenOrientation(
    GADUScreenOrientation orientation) {
  UIInterfaceOrientation uiOrientation;
  switch (orientation) {
    case kGADUScreenOrientationLandscapeLeft:
    case kGADUScreenOrientationLandscapeRight:
      uiOrientation = UIInterfaceOrientationLandscapeLeft;
      break;
    default:
      uiOrientation = UIInterfaceOrientationPortrait;
  }
  return uiOrientation;
}

@implementation GADUPluginUtil

static BOOL _pauseOnBackground = NO;

+ (BOOL)pauseOnBackground {
  return _pauseOnBackground;
}

+ (void)setPauseOnBackground:(BOOL)pause {
  _pauseOnBackground = pause;
}

+ (NSString *)GADUStringFromUTF8String:(const char *)bytes {
  return bytes ? @(bytes) : nil;
}

+ (GADAdSize)safeAdSizeForAdSize:(GADAdSize)adSize {
  if (GADUIsOperatingSystemAtLeastVersion(11) &&
      GADAdSizeEqualToSize(kGADAdSizeSmartBannerLandscape, adSize)) {
    CGSize usualSize = CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape);
    CGSize bannerSize = CGSizeMake(GADUSafeWidthLandscape(), usualSize.height);
    return GADAdSizeFromCGSize(bannerSize);
  } else {
    return adSize;
  }
}

+ (UIViewController *)unityGLViewController {
  id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  return appDelegate.window.rootViewController;
}

+ (void)positionView:(UIView *)view
        inParentView:(UIView *)parentView
          adPosition:(GADAdPosition)adPosition {
  CGRect parentBounds = parentView.bounds;
  if (GADUIsOperatingSystemAtLeastVersion(11)) {
    CGRect safeAreaFrame = parentView.safeAreaLayoutGuide.layoutFrame;
    if (!CGSizeEqualToSize(CGSizeZero, safeAreaFrame.size)) {
      parentBounds = safeAreaFrame;
    }
  }
  CGFloat top = CGRectGetMinY(parentBounds) + CGRectGetMidY(view.bounds);
  CGFloat left = CGRectGetMinX(parentBounds) + CGRectGetMidX(view.bounds);

  CGFloat bottom = CGRectGetMaxY(parentBounds) - CGRectGetMidY(view.bounds);
  CGFloat right = CGRectGetMaxX(parentBounds) - CGRectGetMidX(view.bounds);
  CGFloat centerX = CGRectGetMidX(parentBounds);
  CGFloat centerY = CGRectGetMidY(parentBounds);

  // If this view is of greater or equal width to the parent view, do not offset
  // to edge of safe area. Eg for smart banners that are still full screen
  // width.
  if (CGRectGetWidth(view.bounds) >= CGRectGetWidth(parentView.bounds)) {
    left = CGRectGetMidX(parentView.bounds);
  }

  // Similarly for height, if view is of custom size which is full screen
  // height, do not offset.
  if (CGRectGetHeight(view.bounds) >= CGRectGetHeight(parentView.bounds)) {
    top = CGRectGetMidY(parentView.bounds);
  }

  CGPoint center = CGPointMake(centerX, top);
  switch (adPosition) {
    case kGADAdPositionTopOfScreen:
      center = CGPointMake(centerX, top);
      break;
    case kGADAdPositionBottomOfScreen:
      center = CGPointMake(centerX, bottom);
      break;
    case kGADAdPositionTopLeftOfScreen:
      center = CGPointMake(left, top);
      break;
    case kGADAdPositionTopRightOfScreen:
      center = CGPointMake(right, top);
      break;
    case kGADAdPositionBottomLeftOfScreen:
      center = CGPointMake(left, bottom);
      break;
    case kGADAdPositionBottomRightOfScreen:
      center = CGPointMake(right, bottom);
      break;
    case kGADAdPositionCenterOfScreen:
      center = CGPointMake(centerX, centerY);
      break;
    default:
      break;
  }
  view.center = center;
}

+ (void)positionView:(UIView *)view
        inParentView:(UIView *)parentView
      customPosition:(CGPoint)adPosition {
  CGPoint origin = parentView.bounds.origin;
  if (GADUIsOperatingSystemAtLeastVersion(11)) {
    CGRect safeAreaFrame = parentView.safeAreaLayoutGuide.layoutFrame;
    if (!CGSizeEqualToSize(CGSizeZero, safeAreaFrame.size)) {
      origin = safeAreaFrame.origin;
    }
  }

  CGPoint center = CGPointMake(origin.x + adPosition.x + CGRectGetMidX(view.bounds),
                               origin.y + adPosition.y + CGRectGetMidY(view.bounds));
  view.center = center;
}

+ (GADAdSize)adSizeForWidth:(CGFloat)width height:(CGFloat)height {
  UIInterfaceOrientation currentOrientation = UIApplication.sharedApplication.statusBarOrientation;

  if (width == kGADUAdSizeUseFullWidth && UIInterfaceOrientationIsPortrait(currentOrientation)) {
    return GADAdSizeFullWidthPortraitWithHeight(height);
  } else if ((width == kGADUAdSizeUseFullWidth &&
              UIInterfaceOrientationIsLandscape(currentOrientation))) {
    return GADAdSizeFromCGSize(CGSizeMake(GADUSafeWidthLandscape(), height));
  }
  return GADAdSizeFromCGSize(CGSizeMake(width, height));
}

+ (GADAdSize)adaptiveAdSizeForWidth:(CGFloat)width orientation:(GADUBannerOrientation)orientation {
  if (width == kGADUAdSizeUseFullWidth) {
    width = GADUDeviceSafeWidth();
  }
  switch (orientation) {
    case kGADUBannerOrientationCurrent:
      return GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(width);
    case kGADUBannerOrientationLandscape:
      return GADLandscapeAnchoredAdaptiveBannerAdSizeWithWidth(width);
    case kGADUBannerOrientationPortrait:
      return GADPortraitAnchoredAdaptiveBannerAdSizeWithWidth(width);
  }
}

+ (GADAdSize)largeAdaptiveAdSizeForWidth:(CGFloat)width orientation:(GADUBannerOrientation)orientation {
  if (width == kGADUAdSizeUseFullWidth) {
    width = GADUDeviceSafeWidth();
  }
  switch (orientation) {
    case kGADUBannerOrientationCurrent:
      return GADLargeAnchoredAdaptiveBannerAdSizeWithWidth(width);
    case kGADUBannerOrientationLandscape:
      return GADLargeLandscapeAnchoredAdaptiveBannerAdSizeWithWidth(width);
    case kGADUBannerOrientationPortrait:
      return GADLargePortraitAnchoredAdaptiveBannerAdSizeWithWidth(width);
  }
}


+ (BOOL)isNull:(nullable id)object {
  return object == nil || [[NSNull null] isEqual:object];
}


+ (BOOL)isNotNull:(nullable id)object {
  return ![GADUPluginUtil isNull:object];
}
@end
