//
//  CustomYYLabel.m
//  zhuxun
//
//  Created by XuXiaoJun on 16/6/22.
//
//

#import "CustomTextSimpleEmoticonParser.h"
#import "NSString+YYAdd.h"
#import "NSBundle+YYAdd.h"
#import <YYImage/YYImage.h>

@implementation CustomTextSimpleEmoticonParser

+ (CustomTextSimpleEmoticonParser *)shareYYCustomTextSimpleEmoticonParser
{
    static CustomTextSimpleEmoticonParser *parser = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        
//        dispatch_async(dispatch_get_main_queue(), ^{

            //注册表情规则
            NSMutableDictionary *mapper = [NSMutableDictionary new];


            NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"EmotionZhuxue" ofType:@"bundle"]];

            NSString *path = [bundle pathForScaledResource:@"expression" ofType:@"plist"];

            NSDictionary *expressDict = [[NSDictionary alloc]initWithContentsOfFile:path];

            NSArray *allKeyArray = [expressDict allKeys];
            NSArray *allValueArray = [expressDict allValues];

            //key = code
            //value = pic
            for (int i = 0; i < allKeyArray.count; i ++) {

                NSString *emotionCode = allKeyArray[i];
                NSString *emotionPic = allValueArray[i];
                mapper[emotionCode] = [CustomTextSimpleEmoticonParser imageWithName:emotionPic];

            }

            parser = [CustomTextSimpleEmoticonParser new];
            parser.emoticonMapper = mapper;

    });
    return parser;
}

+ (UIImage *)imageWithName:(NSString *)name {
    
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"EmotionZhuxue" ofType:@"bundle"]];
    NSString *path = [bundle pathForScaledResource:name ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    YYImage *image = [YYImage imageWithData:data scale:2];
    image.preloadAllAnimatedImageFrames = YES;
    return image;
}
@end
