//
//  NSString+IconFont.m
//  Gengmei
//
//  Created by lizhen on 2018/5/25.
//  Copyright © 2018年 更美互动信息科技有限公司. All rights reserved.
//

#import "NSString+IconFont.h"
#import "NSAttributedString+GMSize.h"

#define RGBFCOLOR_HEX(hexColor) [UIColor colorWithRed: (((hexColor >> 16) & 0xFF))/255.0f         \
green: (((hexColor >> 8) & 0xFF))/255.0f             \
blue: ((hexColor & 0xFF))/255.0f                    \
alpha: 1]

@implementation NSString (IconFont)
- (NSMutableAttributedString *)replaceString:(NSArray<NSString *> *)oldArray
                                 withStrings:(NSArray<NSString *> *)newArray
                                        font:(UIFont *)font
                                   textColor:(UIColor *)textColor
                                   lineSpace:(NSInteger)lineSpace {
    NSString *contentString = [self copy];
    NSMutableArray *rangeArray = [NSMutableArray array];
    for (NSInteger i = 0; i < oldArray.count; i++) {
        NSInteger number = [[self mutableCopy] replaceOccurrencesOfString:oldArray[i] withString:newArray[i] options:NSCaseInsensitiveSearch range:NSMakeRange(0, self.length)];
        NSRange range = NSMakeRange(0, 0);
        for (NSInteger j = 0; j < number; j++) {
            if ([contentString containsString:oldArray[i]]) {
                //记录range,为替换成icomoon
                range = [contentString rangeOfString:oldArray[i]];
                contentString = [contentString stringByReplacingOccurrencesOfString:oldArray[i] withString:newArray[i] options:NSCaseInsensitiveSearch range:range];
                NSValue *value = [NSValue valueWithRange:range];
                [rangeArray addObject:value];
            }
        }
    }
    NSDictionary *attributeDic = @{NSForegroundColorAttributeName: textColor, NSFontAttributeName: font};
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:contentString attributes:attributeDic];
    for (NSInteger k = 0; k < rangeArray.count; k++) {
        //通过range遍历, 替换为icomoon
        [content setAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"icomoon" size:font.pointSize]} range:[rangeArray[k] rangeValue]];
    }
    [content addLineSpace:lineSpace];
    return content;
}
@end

// BRACKETS 即 brackets，中括号的意思
#define CHINESE_BRACKETS        @[@"【", @"】"]
#define ICON_FONT_BRACKETS      @[@"\U0000e901", @"\U0000e902"]
@implementation NSString (IconFontFactory)

- (NSMutableAttributedString *)welfareDetailNameWithFontSize:(NSInteger)size
                                                   textColor:(UIColor *)textColor {
    NSMutableAttributedString *content = [self replaceString:CHINESE_BRACKETS
                                                 withStrings:ICON_FONT_BRACKETS
                                                        font:[UIFont fontWithName:@"PingFangSC-Medium" size:size]
                                                   textColor:textColor ?: RGBFCOLOR_HEX(0x282828)
                                                   lineSpace:0];
    return content;
}

- (NSMutableAttributedString *)welfareNameWithFontSize:(NSInteger)size
                                             textColor:(UIColor *)textColor
                                             lineSpace:(NSInteger)lineSpace {
    NSMutableAttributedString *content = [self replaceString:CHINESE_BRACKETS withStrings:ICON_FONT_BRACKETS font:[UIFont fontWithName:@"PingFangSC-Regular" size:size] textColor:textColor ?: RGBFCOLOR_HEX(0x282828) lineSpace:lineSpace];
    return content;
}

- (NSMutableAttributedString *)customDetailNameWithFontSize:(UIFont *)font
                                             textColor:(UIColor *)textColor
                                             lineSpace:(NSInteger)lineSpace {
    NSMutableAttributedString *content = [self replaceString:CHINESE_BRACKETS withStrings:ICON_FONT_BRACKETS font:font textColor:textColor ?: RGBFCOLOR_HEX(0x282828)  lineSpace:lineSpace];
    return content;
}

@end
