//
//  UIDevice+Reso.m
//  Simple UIDevice Category for handling different iOSs hardware resolutions
//
//  Created by Alejandro Luengo on 29/09/14.
//  (c) 2014 Intelygenz

//  iPhone 6 Plus   736x414 points      2208x1242 pixels    3x scale
//  iPhone 6        667x375 points      1334x750 pixels     2x scale
//  iPhone 5        568x320 points      1136x640 pixels     2x scale
//  iPhone 4s       480x320 points      960x640 pixels      2x scale
//  iPad            1024x768 points     1024x768 pixels     1x scale
//  iPad Retina     1024x768 points     2048x1536 pixels    2x scale

#import "UIDevice+Resolutions.h"
#import "sys/utsname.h"
#import <AdSupport/AdSupport.h>

@implementation UIDevice (Resolutions)

+ (NSString *)deviceId {
    NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    
    if (![idfa isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
        return idfa;
    }
    
    return idfv;
}

+ (UIDeviceResolution)resolutionType
{
    UIScreenMode *mode = [UIScreen mainScreen].preferredMode;
    if (mode.size.height == 960) {
        return iPhone35Inches;
    }
    else if (mode.size.height == 1136) {
        return iPhone40Inches;
    }
    else if (mode.size.height == 1334) {
        return iPhone47Inches;
    }
    else if (mode.size.height == 2208) {
        return iPhone55Inches;
    }
    else if (mode.size.height == 2436) {
        return iPhone58Inches;
    }
    else if (mode.size.height == 1792) {
        return iPhoneXRInches;
    }
    else if (mode.size.height == 2688) {
        return iPhoneXSMaxInches;
    }
    else
        return UnknowDevice;
}

+ (GMDeviceAspectRatioType)aspectRatioType {
    UIDevice *device = [UIDevice currentDevice];
    if ([device.model containsString:@"iPad"])
        return GMDeviceAspectRatioTypeNarrow;
    
    NSString *deviceVersion = [self deviceVersion];
    UIScreenMode *mode = [UIScreen mainScreen].preferredMode;
    if ([deviceVersion containsString:@"iPhone 4"] || [deviceVersion containsString:@"iPhone 5"] || mode.size.height == 960 || mode.size.height == 1136)
        return GMDeviceAspectRatioTypeNarrow;
    else if ([deviceVersion containsString:@"Plus"] || [deviceVersion containsString:@"Max"])
        return GMDeviceAspectRatioTypeWide;
    else
        return GMDeviceAspectRatioTypeNormal;
}

+ (NSString *)platform {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    return platform;
}

+ (NSString*)deviceVersion{
    NSString *platform = [self platform];
    
    //iPhone
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5C";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5C";
    
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5S";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5S";
   
    if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    
    if ([platform isEqualToString:@"iPhone8,3"])    return @"iPhoneSE";
    if ([platform isEqualToString:@"iPhone8,4"])    return @"iPhoneSE";
    
    if ([platform isEqualToString:@"iPhone9,1"])    return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,3"])    return @"iPhone 7";

    if ([platform isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus";

    if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
    if ([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
    if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
    if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
    
    if ([platform isEqualToString:@"iPhone11,8"]) return @"iPhone XR";
    if ([platform isEqualToString:@"iPhone11,2"]) return @"iPhone XS";
    if ([platform isEqualToString:@"iPhone11,4"]) return @"iPhone XS Max";
    if ([platform isEqualToString:@"iPhone11,6"]) return @"iPhone XS Max";
    
    return @"iphone X ++";
}

@end
