//
// NSUtils.h
// IvyMedia
//
// Created by iphone development on 8/19/14.
//
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <FSCalendar/FSCalendar.h>
#import <HCSStarRatingView/HCSStarRatingView.h>
#import "PointAnnotation.h"
#import "SMModernCalloutView.h"

@interface CocoaUtils : NSObject

+ (NSString *)userName;
+ (NSString *)bundleName;
+ (NSString *)bundleVersion;
+ (BOOL)isIPhoneX;
+ (NSLocale *)applicationLocale;
+ (void)callPhoneNumber:(NSString *)phoneNumber;
+ (void)openSettings;
+ (NSString *)launchImageName;
+ (BOOL)allowsBackgroundLocationUpdates;
+ (CGFloat)deltaRetinaDisplayScreenWidth;

#pragma mark - Collection

+ (NSMutableArray *)mutableArray:(NSArray *)array;
+ (void)removeObjectsInArray:(NSMutableArray *)array fromIndex:(NSUInteger)anIndex;
+ (NSArray *)unshiftObject:(id)obj toArray:(NSMutableArray *)array count:(NSInteger)count;
+ (id)objectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop)) predicate fromArray:(NSArray *)array;
+ (id)objectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop)) predicate fromArrays:(NSArray *)arrays;
+ (id)objectFilteredByClass:(Class)klass fromArray:(NSArray *)array;
+ (NSDictionary *)nonNullDictionary:(NSDictionary *)dictionary;
+ (NSDictionary *)nonEmptyDictionary:(NSDictionary *)dictionary;
+ (NSDictionary *)nonZeroDictionary:(NSDictionary *)dictionary;
+ (NSDictionary *)dictionaryForKeys:(NSArray *)keys retainEntries:(NSDictionary *)entries;
+ (NSArray *)powerSet:(NSArray *)array;

#pragma mark - String

+ (NSString *)jsonStringWithDictionary:(NSDictionary *)dictionary;
+ (NSString *)stringWithDeviceToken:(NSData *)deviceToken;
+ (NSString *)normalizeUserGeneratedContentString:(NSString *)string;

#pragma mark - AttributedString

+ (NSAttributedString *)attributedStringForImage:(UIImage *)image;
+ (NSAttributedString *)attributedStringForImage:(UIImage *)image verticalAlignWithFont:(UIFont *)font;
+ (NSAttributedString *)attributedStringPrependingAsteriskToString:(NSString *)string;
+ (NSAttributedString *)attributedStringForAsteriskWithFont:(UIFont *)font;

#pragma mark - XML

+ (NSString *)noDeclarationXmlString:(NSString *)xmlString;
+ (NSString *)xmlStringWithValue:(id)value forElement:(NSString *)elementName;
+ (NSString *)xmlStringWithProperties:(NSDictionary *)dictionary;
+ (NSString *)xmlStringWithNonZeroProperties:(NSDictionary *)dictionary;
+ (NSString *)firstValueMatchedInString:(NSString *)xmlString forElement:(NSString *)elementName;
+ (NSArray *)allValuesMatchedInString:(NSString *)xmlString forElement:(NSString *)elementName;
+ (NSDictionary *)dictionaryWithXmlString:(NSString *)xmlString forElement:(NSString *)elementName;

#pragma mark - HTML

+ (NSAttributedString *)attributedStringWithHtmlString:(NSString *)htmlString;
+ (NSAttributedString *)attributedStringWithHtmlString:(NSString *)htmlString fixAttributes:(NSDictionary *)attributes;
+ (BOOL)isHtmlString:(NSString *)htmlString;
+ (NSString *)stripHtmlString:(NSString *)htmlString;

#pragma mark - URL

+ (NSString *)securityUrlString:(NSString *)urlString;
+ (BOOL)canBeConvertedToUrlEncodedFromString:(NSString *)string usingEncoding:(NSStringEncoding)encoding;

#pragma mark - Number

+ (NSString *)percentStringWithNumber:(NSNumber *)number;

#pragma mark - Date

+ (NSString *)localizedStringFromDate:(NSDate *)date template:(NSString *)template;
+ (NSInteger)dateIntervalForComponent:(NSCalendarUnit)unit fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;
+ (NSArray *)continuousDatesInRange:(NSRange)range ofUnit:(NSCalendarUnit)unit forDate:(NSDate *)date;
+ (NSString *)durationStringWithUnitsStyle:(NSDateComponentsFormatterUnitsStyle)unitsStyle locale:(NSLocale *)locale fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;
+ (NSTimeInterval )durationTimeWithUnitsStyle:(NSDateComponentsFormatterUnitsStyle)unitsStyle locale:(NSLocale *)locale fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;

#pragma mark - IndexPath

+ (NSIndexPath *)nextIndexPath:(NSIndexPath *)indexPath;
+ (NSIndexPath *)prevIndexPath:(NSIndexPath *)indexPath;
+ (NSArray *)indexPathsForRowsWithRange:(NSRange)range inSection:(NSUInteger)section;
+ (NSIndexPath *)indexPathOfObjectIdenticalTo:(id)obj fromArray:(NSArray *)array;
+ (NSIndexPath *)indexPathOfObjectIdenticalTo:(id)obj fromArray:(NSArray *)array inSection:(NSUInteger)section;
+ (NSIndexPath *)indexPathOfObjectIdenticalTo:(id)obj fromArrays:(NSArray *)arrays;

#pragma mark - UserDefaults

+ (void)synchronizeUserDefaultsBySettingWithObject:(id)obj forKey:(NSString *)key;
+ (id)objectFromUserDefaultsForKey:(NSString *)key;

#pragma mark - Graphics

+ (UIColor *)colorWithRGBs:(NSUInteger)colors;
+ (UIFont *)renderedFont:(UIFont *)font withSymbolicTraits:(UIFontDescriptorSymbolicTraits)symbolicTraits;
+ (UIStatusBarStyle)contrastingStatusBarStyleForColor:(UIColor *)backgroundColor;

#pragma mark - View

+ (CALayer *)sublayerWithName:(NSString *)name fromSuperlayer:(CALayer *)superlayer;
+ (NSArray *)descendantsOfView:(UIView *)superview;

+ (void)autoVerticalDistributionToSubviews:(NSArray *)subviews spacing:(CGFloat)spacing shouldSpacingToSuperviewEdge:(BOOL)shouldSpacingToSuperviewEdge;
+ (void)autoVerticalDistributionToSubviews:(NSArray *)subviews spacing:(CGFloat)spacing;
+ (void)autoVerticalAlignmentToSubviews:(NSArray *)subviews spacing:(CGFloat)spacing;
+ (void)removeGestureRecognizerKindOfClass:(Class)klass fromView:(UIView *)view;

+ (void)pinButton:(UIButton *)button positioningImageEdge:(NSLayoutAttribute)edge nearTitleSpacing:(CGFloat)spacing;
+ (void)addKeyboardToolbarForTextField:(UITextField *)textField title:(NSString *)title handler:(void (^)(void))handler;
+ (void)configureStarRatingView:(HCSStarRatingView *)starRatingView editing:(BOOL)editing;
+ (void)configureCalloutView:(SMModernCalloutView *)calloutView annotation:(PointAnnotation *)annotation;
+ (void)showBusyIndicator:(UIView *)view;
+ (void)showBusyIndicator:(UIView *)view title:(NSString *)title;
+ (void)showBusyIndicator:(UIView *)view title:(NSString *)title autoshrink:(BOOL)autoshrink;
+ (void)hideBusyIndicator:(UIView *)view;
+ (void)showNotificationWithMessage:(NSString *)message;
+ (void)showTooltipWithMessage:(NSString *)message inView:(UIView *)inView atFrame:(CGRect)frame;
+ (void)showTooltipWithCustomView:(NSString *)customView inView:(UIView *)inView atFrame:(CGRect)frame;

#pragma mark - TableView

+ (CGFloat)calculatedContentWidthForCell:(UITableViewCell *)cell onTableView:(UITableView *)tableView;
+ (void)fillSeparatorOfCell:(UITableViewCell *)cell;
+ (void)hideSeparatorOfCell:(UITableViewCell *)cell;

#pragma mark - ViewController

+ (UIViewController *)topMostViewController;
+ (void)proxyTopMostViewController:(UIViewController *)viewController;
+ (void)proxyAlertControllerAnimated:(BOOL)animated;
+ (UINavigationController *)wrappingNavigationControllerAroundViewController:(UIViewController *)viewController;
+ (UIViewController *)prevViewControllerOnNavigationController:(UINavigationController *)navigationController;
+ (UIViewController *)lastViewControllerWithClass:(Class)klass onNavigationController:(UINavigationController *)navigationController;
+ (void)popViewControllerInQuantity:(NSInteger)quantity onNavigationController:(UINavigationController *)navigationController animated:(BOOL)animated;

#pragma mark - AlertController

+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message;
+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message dismissalHandler:(void (^)(void))dismissalHandler;
+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle;
+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle cancelActionHandler:(void (^)(void))cancelActionHandler;
+ (void)showConfirmWithTitle:(NSString *)title message:(NSString *)message dismissalHandler:(void (^)(void))dismissalHandler;
+ (void)showConfirmWithTitle:(NSString *)title message:(NSString *)message otherActionTitle:(NSString *)otherActionTitle otherActionHandler:(void (^)(void))otherActionHandler;
+ (void)showConfirmWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle otherActionTitle:(NSString *)otherActionTitle otherActionHandler:(void (^)(void))otherActionHandler;
+ (void)showMessageBoxWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle otherActionTitles:(NSArray *)otherActionTitles actionHandler:(void (^)(NSInteger buttonIndex))actionHandler;
+ (void)showPlainInputBoxWithTitle:(NSString *)title message:(NSString *)message configurationHandler:(void (^)(UITextField *textField)) configurationHandler dismissalHandler:(void (^)(UITextField *textField))dismissalHandler;
+ (void)showInputBoxWithStyle:(UIAlertViewStyle)alertViewStyle title:(NSString *)title message:(NSString *)message configurationHandler:(void (^)(NSInteger inputIndex, UITextField *textField)) configurationHandler cancelActionTitle:(NSString *)cancelActionTitle otherActionTitles:(NSArray *)otherActionTitles actionHandler:(void (^)(NSInteger buttonIndex, NSArray *textFields))actionHandler;

#pragma mark - Contacts

+ (void)showSelectionOfContactWithDisplayedPropertyKeys:(NSArray *)displayedPropertyKeys completion:(void (^)(id contactOrProperty))completion;

#pragma mark - CoreLocation

+ (CLLocationDistance)distanceFromCoordinate:(CLLocationCoordinate2D)fromCoordinate toCoordinate:(CLLocationCoordinate2D)toCoordinate;
+ (void)locationManager:(CLLocationManager *)manager requestLocationWithCompletion:(void (^)(BOOL granted,CLLocation *location, NSError *error))completion;
+ (void)geocoder:(CLGeocoder *)geocoder reverseGeocodeLocation:(CLLocation *)location completionHandler:(void (^)(CLPlacemark *placemark, NSError *error))completion;

#pragma mark - StoreKit

+ (void)loadProductWithITunesItemIdentifier:(NSString *)appId;

@end

