#import "RCTSplash.h"

static RCTRootView *rootView = nil;

@implementation RCTSplash

RCT_EXPORT_MODULE(Splash)

+ (void)init:(RCTRootView *)root {
    rootView = root;
    
    NSString *launchImageName = [self launchImageName:UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)];
    
    UIImageView *view = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    view.image = [UIImage imageNamed:launchImageName];
    
    [[NSNotificationCenter defaultCenter] removeObserver:rootView  name:RCTContentDidAppearNotification object:rootView];
    [rootView setLoadingView:view];
}

+ (NSDictionary *)launchImageSuffixes{
    NSMutableDictionary *imageSuffixes = [[NSMutableDictionary alloc] init];
    
    NSString *imageSuffix;
    NSNumber *hasLandscape = [NSNumber numberWithInt: 0];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        
        if ([UIScreen mainScreen].bounds.size.height == 480) {
            imageSuffix = @"-700@2x.png"; // iPhone 4/4s, 3.5 inch screen
        } else if ([UIScreen mainScreen].bounds.size.height == 568) {
            imageSuffix = @"-700-568h@2x.png"; // iPhone 5/5s, 4.0 inch screen
        } else if ([UIScreen mainScreen].bounds.size.height == 667) {
            imageSuffix = @"-800-667h@2x.png"; // iPhone 6/6s, 4.7 inch screen
        }
        
        if (imageSuffix) {
            hasLandscape = [NSNumber numberWithInt: 1];
            [imageSuffixes setValue:imageSuffix forKey:@"landscape"];
            [imageSuffixes setValue:imageSuffix forKey:@"portrait"];
        } else if ([UIScreen mainScreen].bounds.size.height == 736) { // iPhone 6+/6s+, 5.5 inch screen
            [imageSuffixes setValue:@"-800-Landscape-736h@3x.png" forKey:@"landscape"];
            [imageSuffixes setValue:@"-800-Portrait-736h@3x.png" forKey:@"portrait"];
        }
    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if ([UIScreen mainScreen].scale == 1) { // iPad 2
            [imageSuffixes setValue:@"-700-Landscape~ipad.png" forKey:@"landscape"];
            [imageSuffixes setValue:@"-700-Portrait~ipad.png" forKey:@"portrait"];
        } else if ([UIScreen mainScreen].scale == 2) { // Retina iPads
            [imageSuffixes setValue:@"-700-Landscape@2x~ipad.png" forKey:@"landscape"];
            [imageSuffixes setValue:@"-700-Portrait@2x~ipad.png" forKey:@"portrait"];
        }
    }
    
    [imageSuffixes setValue:hasLandscape forKey:@"hasLandscape"];

    return imageSuffixes;
}

+ (NSString *)launchImageName:(BOOL) isPortait{
    NSString *launchImage = [NSString stringWithFormat:@"LaunchImage%@.png", [[self launchImageSuffixes] objectForKey: isPortait ? @"portrait" : @"landscape"]];
    return launchImage;
    
}

+ (NSString *)launchImagePath:(BOOL) isPortait{
    NSMutableString *path = [[NSMutableString alloc]init];
    [path setString:[[NSBundle mainBundle] resourcePath]];
    [path setString:[path stringByAppendingPathComponent:[self launchImageName:isPortait]]];
    return path;
}

RCT_REMAP_METHOD(hide,
                  delay: (float) delay
                  duration: (float) duration) {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC),
                   dispatch_get_main_queue(),
                   ^{
                       [UIView animateWithDuration: duration
                                       animations: ^{
                                           rootView.loadingView.alpha = 1;
                                           rootView.loadingView.alpha = 0;
                                       }
                                       completion: ^(__unused BOOL finished) {
                                           [rootView.loadingView removeFromSuperview];
                                           rootView.loadingView.alpha = 1;
                                       }
                        ];
                   });
}


- (NSDictionary *)constantsToExport
{
    return @{
             @"LaunchImage": [RCTSplash launchImageName:UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)],
             @"LaunchImagePortrait": [RCTSplash launchImageName:YES],
             @"LaunchImageLandscape": [RCTSplash launchImageName:NO],
             @"LaunchImagePath": [RCTSplash launchImagePath:UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)],
             @"LaunchImagePortraitPath": [RCTSplash launchImagePath:YES],
             @"LaunchImageLandscapePath": [RCTSplash launchImagePath:NO],
             @"LandscapeSameAsPortrait": [[RCTSplash launchImageSuffixes] objectForKey:@"hasLandscape"]
             };
}


@end
