//
//  NSString+PictureData.m
//  rainbow
//
//  Created by Doha Tubaileh on 9/8/19.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "NSString+PictureData.h"

@implementation NSString (PictureData)

//this like get image bitmap in android
-(NSData*)getImageData {
    UIGraphicsBeginImageContext(CGSizeMake(50, 50));
    CGContextRef context = UIGraphicsGetCurrentContext();
    //
    // Fill background of context
    //
    NSString *text = [self getFirstAndLastLetter];
    UIColor *color = [self stringToColor];
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, 50, 50));
    //
    // Draw text in the context
    //
    NSDictionary *textAttributes  = @{
        NSFontAttributeName: [self fontForFontName:nil],
        NSForegroundColorAttributeName: [UIColor whiteColor]
    };
    CGSize textSize = [text sizeWithAttributes:textAttributes];
    [text drawInRect:CGRectMake(25/2,
                                25/2,
                                textSize.width,
                                textSize.height)
      withAttributes:textAttributes];
    UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *imageData = UIImagePNGRepresentation(snapshot);
    return imageData;
}

-(NSString *)getFirstAndLastLetter {
    NSMutableString *displayString = [NSMutableString stringWithString:@""];
    NSMutableArray *words = [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
    if ([words count]) {
        NSString *firstWord = [words firstObject];
        if ([firstWord length]) {
            // Get character range to handle emoji (emojis consist of 2 characters in sequence)
            NSRange firstLetterRange = [firstWord rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 1)];
            [displayString appendString:[firstWord substringWithRange:firstLetterRange]];
        }
        if ([words count] >= 2) {
            NSString *lastWord = [words lastObject];
            while ([lastWord length] == 0 && [words count] >= 2) {
                [words removeLastObject];
                lastWord = [words lastObject];
            }
            if ([words count] > 1) {
                // Get character range to handle emoji (emojis consist of 2 characters in sequence)
                NSRange lastLetterRange = [lastWord rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 1)];
                [displayString appendString:[lastWord substringWithRange:lastLetterRange]];
            }
        }
    }
    return (NSString *)displayString;
}

-(UIColor*)stringToColor {
    NSArray* colors = @[@0xFF4500FF, @0xD38700FF, @0x348833FF, @0x007356FF, @0x00B2A9FF, @0x00B0E5FF, @0x0085CAFF, @0x6639B7FF, @0x91278AFF , @0xCF0072FF, @0xA50034FF, @0xD20000FF];
    NSString* capitalizedString = [self uppercaseString];
    int sum = 0;
    for (int i=0; i< capitalizedString.length; i++) {
        sum += [capitalizedString characterAtIndex:i];
    }
    NSInteger value = [colors[0 + sum%[colors count]] integerValue];
    UIColor* color = [self colorFromHexa:(int)value];
    return color;
}

-(UIColor*)colorFromHexa:(int)hexaCode {
    return [UIColor colorWithRed:((hexaCode & 0xFF000000) >> 24) / 255.0f
                           green:((hexaCode & 0x00FF0000) >> 16) /255.0f
                            blue:((hexaCode & 0x0000FF00) >> 8) / 255.0f
                           alpha:(hexaCode & 0x000000FF)/255.0f];
    
}


- (UIFont *)fontForFontName:(NSString *)fontName {
    CGFloat fontSize = CGRectGetWidth(CGRectMake(50, 50, 50, 50))* 0.42f;
    if (fontName) {
        return [UIFont fontWithName:fontName size:fontSize];
    }
    else {
        return [UIFont systemFontOfSize:fontSize];
    }
}

- (NSData *)getColorImageFromString {
    UIColor *color = [self stringToColor];
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return UIImagePNGRepresentation(image);
}

@end
