//
// SystemUtils.m
// MyProject
//
// Created by haibara on 6/25/15.
//
//

#import "SystemUtils.h"
#import <UIKit/UIKit.h>

@implementation SystemUtils

#pragma mark - Public

+ (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version {
	NSProcessInfo *processInfo = [NSProcessInfo processInfo];
	if ([processInfo respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
		return [processInfo isOperatingSystemAtLeastVersion:version];
	} else {
		return [[UIDevice currentDevice].systemVersion compare:[self stringWithOperatingSystemVersion:version] options:NSNumericSearch] != NSOrderedAscending;
	}
}

#pragma mark - Private

+ (NSOperatingSystemVersion)operatingSystemVersionWithString:(NSString *)string {
	NSOperatingSystemVersion version = { 0, 0, 0 };
	NSArray *array = [string componentsSeparatedByString:@"."];
	if (array.count > 0) {
		version.majorVersion = [array[0] integerValue];
	}
	if (array.count > 1) {
		version.minorVersion = [array[1] integerValue];
	}
	if (array.count > 2) {
		version.patchVersion = [array[2] integerValue];
	}
	return version;
}

+ (NSString *)stringWithOperatingSystemVersion:(NSOperatingSystemVersion)version {
	NSMutableArray *array = [NSMutableArray array];
	if (version.majorVersion > 0) {
		[array addObject:@(version.majorVersion)];
	}
	if (version.minorVersion > 0) {
		[array addObject:@(version.minorVersion)];
	}
	if (version.patchVersion > 0) {
		[array addObject:@(version.patchVersion)];
	}
	return [array componentsJoinedByString:@"."];
}

+ (BOOL)isVersion:(NSOperatingSystemVersion)version atLeastVersion:(NSOperatingSystemVersion)requiredVersion {
	if (version.majorVersion < requiredVersion.majorVersion) {
		return NO;
	}
	if (version.minorVersion < requiredVersion.minorVersion) {
		return NO;
	}
	if (version.patchVersion < requiredVersion.patchVersion) {
		return NO;
	}
	return YES;
}

@end
