#import "PrivacyScreenEnhancedPlugin.h"

static UIImageView *imageView;

@implementation PrivacyScreenEnhancedPlugin

- (void)pluginInitialize
{
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidBecomeActive:)
                                               name:UIApplicationDidBecomeActiveNotification object:nil];

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillResignActive:)
                                               name:UIApplicationWillResignActiveNotification object:nil];
}

- (void)onAppDidBecomeActive:(UIApplication *)application
{
  if (imageView == NULL) {
    self.viewController.view.window.hidden = NO;
  } else {
    [imageView removeFromSuperview];
  }
}

- (void)onAppWillResignActive:(UIApplication *)application
{
  NSString* imgName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchImageFile"];
  UIImage *splash = [UIImage imageNamed:imgName];
  if (splash == NULL) {
    imageView = NULL;
    self.viewController.view.window.hidden = YES;
  } else {
    imageView = [[UIImageView alloc]initWithFrame:[self.viewController.view bounds]];
    
    //manual adjustment in the line below
    [imageView setContentMode:UIViewContentModeScaleAspectFill];
    [imageView setImage:splash];
    
    #ifdef __CORDOVA_4_0_0
        [[UIApplication sharedApplication].keyWindow addSubview:imageView];
    #else
        [self.viewController.view addSubview:imageView];
    #endif
  }
}

@end
