//
//  MFFonts.m
//  myverizon
//
//  Created by Scott Pfeil on 11/17/14.
//  Copyright (c) 2014 Verizon Wireless. All rights reserved.
//

#import "MFFonts.h"
#import <CoreText/CoreText.h>
#import "MVMCoreUIUtility.h"
@import MVMCore.MVMCoreLoggingHandler;
@import MVMCore.MVMCoreErrorConstants;

NSString * const DSBold = @"VerizonNHGeDS-Bold";
NSString * const DSRegular = @"VerizonNHGeDS-Regular";
NSString * const TXBold = @"VerizonNHGeTX-Bold";
NSString * const TXRegular = @"VerizonNHGeTX-Regular";

@implementation MFFonts

+ (void)loadMVMFonts {
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        [MFFonts loadFont:DSBold type:@"otf"];
        [MFFonts loadFont:DSRegular type:@"otf"];
        [MFFonts loadFont:TXBold type:@"otf"];
        [MFFonts loadFont:TXRegular type:@"otf"];
        [MFFonts loadFont:@"OCRAExtended" type:@"ttf"];
    });
}

+ (void)loadFont:(NSString *)fontName type:(NSString *)type{
    
    NSString *fontPath = [[MVMCoreUIUtility bundleForMVMCoreUI] pathForResource:fontName ofType:type];
    NSData *inData = [NSData dataWithContentsOfFile:fontPath];
    CFErrorRef error;
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
    [UIFont familyNames];//fixing weird app freeze after launch app issue
    CGFontRef font = CGFontCreateWithDataProvider(provider);
    if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
        CFStringRef errorDescription = CFErrorCopyDescription(error);
        MVMCoreLog(@"Failed to load font %@: %@", fontName, errorDescription);
        CFRelease(errorDescription);
    }
    CFRelease(font);
    CFRelease(provider);
}

+ (nonnull UIFont *)mfFontDSBold:(CGFloat)size {
    [self loadMVMFonts];
    UIFont *font = [UIFont fontWithName:DSBold size:size];
    [self validFont:font fontName:DSBold];
    return font ?: [UIFont boldSystemFontOfSize:size];
}

+ (nonnull UIFont *)mfFontDSRegular:(CGFloat)size {
    [self loadMVMFonts];
    UIFont *font = [UIFont fontWithName:DSRegular size:size];
    [self validFont:font fontName:DSRegular];
    return font ?: [UIFont systemFontOfSize:size];
}

+ (nonnull UIFont *)mfFontTXBold:(CGFloat)size {
    [self loadMVMFonts];
    UIFont *font = [UIFont fontWithName:TXBold size:size];
    [self validFont:font fontName:TXBold];
    return font ?: [UIFont boldSystemFontOfSize:size];
}

+ (nonnull UIFont *)mfFontTXRegular:(CGFloat)size {
    [self loadMVMFonts];
    UIFont *font = [UIFont fontWithName:TXRegular size:size];
    [self validFont:font fontName:TXRegular];
    return font ?: [UIFont systemFontOfSize:size];
}


+ (nonnull UIFont *)mfFont75Bd:(CGFloat)size {
    if (size >= 15) {
        return [MFFonts mfFontDSBold:size];
    } else {
        return [MFFonts mfFontTXBold:size];
    }
}

+ (nonnull UIFont *)mfFont55Rg:(CGFloat)size {
    if (size >= 15) {
        return [MFFonts mfFontDSRegular:size];
    } else {
        return [MFFonts mfFontTXRegular:size];
    }
}

+ (nullable UIFont *)mfFontOcratxt:(CGFloat)size {
    [self loadMVMFonts];
    UIFont *font = [UIFont fontWithName:@"OCRAExtended" size:size];
    [self validFont:font fontName:@"OCRAExtended"];
    return font;
}

+ (UIFont *)mfFontWithName:(nonnull NSString *)name size:(CGFloat)size {
    UIFont *font = [UIFont fontWithName:name size:size];
    [self validFont:font fontName:name];
    return font ?: [self mfFont55Rg:size];
}

+ (void)validFont:(UIFont *)font fontName:(NSString *)fontName {
    if (font == nil) {
        MVMCoreErrorObject *errorObject = [[MVMCoreErrorObject alloc] initWithTitle:@"font can not load" message:[NSString stringWithFormat:@"missing font name is %@", fontName] code:ErrorCodeFontNotFound domain:ErrorDomainNative location:@"MFStyler"];
        [MVMCoreLoggingHandler addErrorToLog:errorObject];
    }
}
@end
