//
//  InMobiCommonUtils.m
//
//  Created by Anil Bakhla on 30/09/21.
//  Copyright © 2021 InMobi. All rights reserved.
//

#import "InMobiCommonUtils.h"
#import "InMobiConstant.h"

@implementation InMobiCommonUtils

#pragma mark - UIWindow/UIView related methods

+(NSDictionary*)getSafeAreaInsets {
    NSMutableDictionary* retValue = [NSMutableDictionary dictionary];
    __block UIEdgeInsets safeInsets = UIEdgeInsetsZero;
    __block UIWindow* keyWindow = [InMobiCommonUtils getKeyWindow];
    
    if (@available(iOS 11.0, *)) {
        if(IS_THIS_MAIN_THREAD){
            safeInsets = [[[keyWindow rootViewController] view] safeAreaInsets];
        } else {
            dispatch_sync(dispatch_get_main_queue(), ^{
                safeInsets = [[[keyWindow rootViewController] view] safeAreaInsets];
            });
        }
    }
    [retValue setValue:[NSNumber numberWithFloat:safeInsets.top] forKey:IM_UNITY_TOP_KEY];
    [retValue setValue:[NSNumber numberWithFloat:safeInsets.bottom] forKey:IM_UNITY_BOTTOM_KEY];
    [retValue setValue:[NSNumber numberWithFloat:safeInsets.right] forKey:IM_UNITY_RIGHT_KEY];
    [retValue setValue:[NSNumber numberWithFloat:safeInsets.left] forKey:IM_UNITY_LEFT_KEY];
    return retValue;
}

+(UIWindow*)getKeyWindow{
    __block UIWindow* retVal;
    
    if(IS_THIS_MAIN_THREAD) {
        retVal = [UIApplication sharedApplication].keyWindow;
    } else {
        dispatch_sync(dispatch_get_main_queue(), ^{
            retVal = [UIApplication sharedApplication].keyWindow;
        });
    }
    
    return retVal;
}

@end