//
//  UIColor+MFConvenience.m
//  mobilefirst
//
//  Created by Chris Yang on 2/21/16.
//  Copyright © 2016 Verizon Wireless. All rights reserved.
//

#import "UIColor+MFConvenience.h"
@import MVMCore.MVMCoreDispatchUtility;

@implementation UIColor (MFConvenience)

#pragma mark - 2.0

+ (nonnull UIColor *)mfSwitchOnTintColor {
    return [UIColor mfShamrock];
}

+ (nonnull UIColor *)mfSwitchOffTintColor {
    return [UIColor mfColor8bitsWithRed:0 green:0 blue:0 alpha:1.0];
}

+ (nonnull UIColor *)mfSilver {
    return [UIColor mfColor8bitsWithRed:216 green:218 blue:218 alpha:1.0];
}

+ (nonnull UIColor *)mfBattleshipGrey {
    return [UIColor mfColor8bitsWithRed:116 green:118 blue:118 alpha:1.0];
}

+ (nonnull UIColor *)mfLightSilver {
    return [UIColor mfColor8bitsWithRed:243 green:243 blue:243 alpha:1.0];
}

+ (nonnull UIColor *)mfDarkSilver {
    return [UIColor mfColor8bitsWithRed:198 green:197 blue:197 alpha:1.0];
}

+ (nonnull UIColor *)mfPrimaryButtonHighlightBlackColor {
    return [UIColor mfColor8bitsWithRed:51 green:51 blue:51 alpha:1.0];
}

+ (nonnull UIColor *)mfPrimaryWhiteButtonDisableColor {
    return [UIColor mfColor8bitsWithRed:51 green:51 blue:51 alpha:1.0];
}

+ (nonnull UIColor *)mfTomatoRed {
    return [UIColor mfColor8bitsWithRed:213 green:43 blue:30 alpha:1.0];
}

+ (nonnull UIColor *)mfPumpkinColor {
    return [UIColor mfColor8bitsWithRed:204 green:77 blue:15 alpha:1.0];
}

+ (nonnull UIColor *)mvmOrange {
    return [UIColor mfColor8bitsWithRed:237 green:112 blue:0 alpha:1.0];
}

+ (nonnull UIColor *)mfShamrock {
    return [UIColor mfColor8bitsWithRed:0 green:134 blue:49 alpha:1.0];
}

+ (nonnull UIColor *)mvmGreen {
    return [UIColor mfColor8bitsWithRed:0 green:134 blue:48 alpha:1.0];
}

+ (nonnull UIColor *)mfCerulean {
    return [UIColor mfColor8bitsWithRed:0 green:122 blue:184 alpha:1.0];
}

+ (nonnull UIColor *)mfWhiteTwo {
    return [UIColor mfColor8bitsWithRed:246 green:246 blue:246 alpha:1.0];
}

#pragma mark - legacy

+ (nonnull UIColor *)mfRedColor {
    return [UIColor colorWithRed:.804 green:.016 blue:.043 alpha:1.0];
}

+ (nonnull UIColor *)mfDarkerRedColor {
    return [UIColor colorWithRed:.6 green:.012 blue:.031 alpha:1.0];
}

+ (nonnull UIColor *)mfBackgroundGray1 {
    return [UIColor colorWithRed:.894 green:.898 blue:.89 alpha:1.0];
}

+ (nonnull UIColor *)mfBackgroundGray {
    return [UIColor colorWithRed:.965 green:.965 blue:.965 alpha:1.0];
}

+ (nonnull UIColor *)mfTextDarkerGray {
    return [UIColor mfColor8bitsWithRed:51 green:51 blue:51 alpha:1.f];
}

+ (nonnull UIColor *)mfTextLightGray {
    return [UIColor mfColor8bitsWithRed:102 green:102 blue:102 alpha:1.0];
}

+ (nonnull UIColor *)mfSupportTitleColor {
    return [UIColor mfColor8bitsWithRed:155 green:155 blue:155 alpha:1.0];
}

+ (nonnull UIColor *)mfSupportTextColor {
    return [UIColor mfColor8bitsWithRed:255 green:255 blue:255 alpha:1.0];
}

+ (nonnull UIColor *)mfColorForCardText {
    return [UIColor mfColor8bitsWithRed:156 green:125 blue:75 alpha:1.0];
}

+ (nonnull UIColor *)mfLightGrayColor {
    return [UIColor mfColor8bitsWithRed:116 green:118 blue:118 alpha:1.0];
}

+ (nonnull UIColor *)mfLighterGrayColor {
    return [UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1.0];
}

+ (nonnull UIColor *)mfGrayColor {
    return [UIColor colorWithRed:.29 green:.29 blue:.29 alpha:1.0];
}

+ (nonnull UIColor *)mfWarmGray {
    return [UIColor colorWithRed:.462 green:.462 blue:.462 alpha:1.0];
}

+ (nonnull UIColor *)mfCharcoalColor {
    return [UIColor colorWithRed:.2 green:.2 blue:.2 alpha:1.0];
}

+ (nonnull UIColor *)mfSupportSearchXGreyColor {
	return [UIColor colorWithRed:.584 green:.584 blue:.584 alpha:1.0];
}

+ (nonnull UIColor *)mfMainMenuCellColor {
    return [UIColor colorWithRed:.133 green:.133 blue:.133 alpha:1.0];
}

+ (nonnull UIColor *)mfMainMenuSeparatorColor {
    return [UIColor colorWithRed:229 green:229 blue:227 alpha:1.0];
}

+ (nonnull UIColor *)mfTextButtonColor {
    return [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
}

+ (nonnull UIColor *)mfTextButtonColorHighlighted {
    return [UIColor colorWithRed:0 green:.278 blue:.557 alpha:1.0];
}

+ (nonnull UIColor *)mfTealColor {
    return [UIColor colorWithRed:.51 green:.808 blue:.675 alpha:1.0];
}

+ (nonnull UIColor *)mfSkyColor {
    return [UIColor colorWithRed:.671 green:.878 blue:.976 alpha:1.0];
}

+ (nonnull UIColor *)mfGoldColor {
    return [UIColor colorWithRed:.973 green:.827 blue:.384 alpha:1.0];
}


+ (nonnull UIColor *)mfLakeBlueColor {
    return [UIColor colorWithRed:0.015 green:0.498 blue:0.827 alpha:1];
}

+ (nonnull UIColor *)mfSalmonColor {
    return [UIColor colorWithRed:.976 green:.698 blue:.584 alpha:1.0];
}

+ (nonnull UIColor *)mfGreenColor {
    return [UIColor colorWithRed:.914 green:.973 blue:.914 alpha:1.0];
}

+ (nonnull UIColor *)mfBlueColor {
    return [UIColor colorWithRed:.91 green:.965 blue:.996 alpha:1.0];
}

+ (nonnull UIColor *)mfGrayLoadingSpinnerColor {
    return [UIColor colorWithRed:0.847 green:0.847f blue:0.847 alpha:1.0f];
}

+ (nonnull UIColor *)mfYellowColor {
    return [UIColor colorWithRed:.996 green:1 blue:.894 alpha:1.0];
}

+ (nonnull UIColor *)mfYellowLightMustard {
    return [UIColor colorWithRed:0.965 green:0.831 blue:0.365 alpha:1.0];
}

+ (nonnull UIColor *)mfYellowLightMustardTwo {
    return [UIColor colorWithRed:248/255.f green:211/255.f blue:98/255.f alpha:1.0];
}

+ (nonnull UIColor *)mfOrangeColor {
    return [UIColor mfGetColorForHex:@"#ed7000"];
}

+ (nonnull UIColor *)mfRobinsEggBlue {
    return [UIColor mfColor8bitsWithRed:171 green:224 blue:249 alpha:1.0];
}

+ (nonnull UIColor *)mfPaleTealColor{
    return [UIColor mfColor8bitsWithRed:130 green:206 blue:172 alpha:1.0];
}

+ (nonnull UIColor *)mfBrightSkyBlue{
    return [UIColor mfColor8bitsWithRed:0 green:189 blue:255 alpha:1.0];
}


+ (nonnull UIColor *)mfGrayish {
    return [UIColor mfColor8bitsWithRed:175 green:175 blue:175 alpha:1.0];
}

+ (nonnull UIColor *)mfBlushRed {
    return [UIColor mfColor8bitsWithRed:247 green:181 blue:148 alpha:1.0];
}

+ (nonnull UIColor *)mfGrayishBrown {
    return [UIColor mfColor8bitsWithRed:75 green:75 blue:75 alpha:1.0];
}

+ (nonnull UIColor *)mfBubbleBackgroundGray {
    return [UIColor mfColor8bitsWithRed:75 green:75 blue:75 alpha:1.0];
}

+ (nonnull UIColor *)mfSupportSearchBarTextColor {
    return [UIColor mfColor8bitsWithRed:101 green:101 blue:101 alpha:1.0];
}

+ (nonnull UIColor *)mfSupportHeaderTextLightGrayColor {
    return [UIColor mfColor8bitsWithRed:164 green:164 blue:164 alpha:1.0];
}

+ (nonnull UIColor *)mfBlack {
    return [UIColor mfColor8bitsWithRed:34 green:34 blue:34 alpha:1.0];
}

+ (nonnull UIColor *)mfSupportBackgroundColor {
    return [UIColor mfColor8bitsWithRed:50 green:50 blue:50 alpha:1.0];
}

+ (nonnull UIColor *)mfSeafoamBlue {
    return [UIColor mfColor8bitsWithRed:105 green:178 blue:213 alpha:1.0];
}

+ (nonnull UIColor *)mfLightSalmon {
    return [UIColor mfColor8bitsWithRed:249 green:178 blue:149 alpha:1.0];
}

+ (nonnull UIColor *)mfBrownishGrey {
    return [UIColor mfColor8bitsWithRed:102 green:102 blue:102 alpha:1.0];
}

+ (nonnull UIColor *)mfPaleGrey {
    return [UIColor mfColor8bitsWithRed:245 green:245 blue:246 alpha:1.0];
}

+ (nonnull UIColor *)mfToolBarGrayColor {
    return [UIColor mfColor8bitsWithRed:233 green:234 blue:235 alpha:1.0];
}

+ (nonnull UIColor *)mfPinkishGray {
    return [UIColor mfColor8bitsWithRed:204 green:204 blue:204 alpha:1.0];
}

+ (nonnull UIColor *)mfLiliac {
    return [UIColor mfColor8bitsWithRed:197 green:150 blue:250 alpha:1.0];
}

+ (nonnull UIColor *)mfPowderPink {
    return [UIColor mfColor8bitsWithRed:250 green:175 blue:227 alpha:1.0];
}

+ (nonnull UIColor *)mfLightGrey {
    return [UIColor mfColor8bitsWithRed:229 green:229 blue:227 alpha:1.0];
}

+ (nonnull UIColor *)mfWhiteThree {
    return [UIColor mfColor8bitsWithRed:214 green:214 blue:214 alpha:1.0];
}

+ (nonnull UIColor *)mfSeafoamBlue2 {
    return [UIColor mfColor8bitsWithRed:147 green:191 blue:248 alpha:1.0];
}

+ (nonnull UIColor *)mfLoyaltySpecialOffer {
    return [UIColor colorWithRed:.992 green:.733 blue:.294 alpha:1.0];
}

+ (nonnull UIColor *)mfSlateBlue {
    return [UIColor mfColor8bitsWithRed:0 green:125 blue:255 alpha:1.0];
}

+ (nullable UIColor *)mfGetColorForString:(nullable NSString *)string {
    static NSDictionary *stringColorMapping;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        stringColorMapping = @{@"PrimaryRed":[UIColor mfRedColor],
                               @"black":[UIColor blackColor],
                               @"red":[UIColor mfRedColor],
                               @"greyish":[UIColor mfLightGrayColor],
                               @"robinsEggBlue" : [UIColor mfRobinsEggBlue],
                               @"lightSalmon" : [UIColor mfLightSalmon],
                               @"seafoamBlue" : [UIColor mfSeafoamBlue],
                               @"light_salmon" : [UIColor mfLightSalmon],
                               @"seafoam_blue" : [UIColor mfSeafoamBlue2],
                               @"robins_egg_blue" : [UIColor mfRobinsEggBlue],
                               @"pinkish_grey": [UIColor mfPinkishGray],
                               @"pale_teal":[UIColor mfPaleTealColor],
                               @"liliac":[UIColor mfLiliac],
                               @"powder_pink":[UIColor mfPowderPink],
                               @"lightGrey":[UIColor mfLightGrey],
                               @"lightMustard":[UIColor mfYellowLightMustard],
                               @"whiteThree":[UIColor mfWhiteThree],
                               @"lightest_gray":[UIColor mfSupportTitleColor],
                               @"slate_blue":[UIColor mfSlateBlue],
                               @"pumpkin":[UIColor mfPumpkinColor],
                               @"cerulean":[UIColor mfCerulean]
                               };
    });
    
    UIColor *color = nil;
    if (string && string.length > 0) {
        color = [stringColorMapping objectForKey:string];
        if (!color){
            color = [UIColor blackColor];
        }
    } else {
        color = [UIColor blackColor];
    }
    return color;
}

+ (nonnull UIColor *)mfGetColorForHex:(nonnull NSString *) hexString {
    unsigned int hexint = 0;
    
    // Create scanner
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    
    // Tell scanner to skip the # character
    [scanner setCharactersToBeSkipped:[NSCharacterSet
                                       characterSetWithCharactersInString:@"#"]];
    [scanner scanHexInt:&hexint];
    
    UIColor *color = [UIColor colorWithRed:((CGFloat) ((hexint & 0xFF0000) >> 16))/255
                                     green:((CGFloat) ((hexint & 0xFF00) >> 8))/255
                                      blue:((CGFloat) (hexint & 0xFF))/255
                                     alpha:1];
    return color;
}

// Needs to be 8 digit hash
+ (nonnull UIColor *)mfGetColorForHexWithTransparency:(nonnull NSString *)hexString {
    unsigned int hexint = 0;
    
    // Create scanner
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    
    // Tell scanner to skip the # character
    [scanner setCharactersToBeSkipped:[NSCharacterSet
                                       characterSetWithCharactersInString:@"#"]];
    [scanner scanHexInt:&hexint];
    
    UIColor *color = [UIColor colorWithRed:((CGFloat) ((hexint & 0xFF000000) >> 16))/255
                                     green:((CGFloat) ((hexint & 0xFF0000) >> 8))/255
                                      blue:((CGFloat) (hexint & 0xFF00))/255
                                     alpha:((CGFloat) (hexint & 0xFF))/255];
    return color;
}

+ (UIColor *)mfColor8bitsWithRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:red/255.0f green:green/255.0 blue:blue/255.f alpha:1.0];
}

+ (nonnull UIColor *)mfGradientColor:(UIColor *)color {
    CGFloat h,s,b,a;
    if ([color getHue:&h saturation:&s brightness:&b alpha:&a]) {
        return [UIColor colorWithHue:h
                          saturation:MAX(s - 0.17 , 0.0)
                          brightness:MIN(b - 0.03, 1.0)
                               alpha:a];
    }
    return [UIColor whiteColor];
}

#pragma mark - Hex String

+ (nullable NSString *)hexStringForColor:(nonnull UIColor*)color {
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    if (CGColorGetNumberOfComponents(color.CGColor)>=3) {
        CGFloat r = components[0];
        CGFloat g = components[1];
        CGFloat b = components[2];
        return [NSString stringWithFormat:@"%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)];
    } else {
        return nil;
    }
}

@end
