//
//  P101XPHelper.mm
//  Created by Domasevich Mikhail on 2021/03/15.
//

#import "P101XPHelper.h"

@implementation P101XPHelper

+ (NSError *) createErrorWithMessage:(NSString *) message {
    return [NSError errorWithDomain:@"com.101xp.unity_ios" code:0 userInfo:@{@"message" : message}];
}

+ (NSString *)jsonStringFrom:(NSDictionary *)dictionary {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                       options:0
                                                         error:&error];
    if (!jsonData) {
        NSLog(@"%s: error: %@", __func__, error.localizedDescription);
        return @"{}";
    } else {
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
}

+ (void)logWithPrefix:(NSString *)prefix withMessage:(NSString *)message withError:(NSError *)error {
    NSString *text = @"";
    if (prefix != nil) {
        text = [NSString stringWithFormat:@"[%@] ", prefix];
    }
    if (message != nil) {
        text = [NSString stringWithFormat:@"%@Message:(%@) ", text, message];
    }
    if (error != nil) {
        text = [NSString stringWithFormat:@"%@Error:(%@) ", text, error.localizedDescription];
    }
    
    NSLog(@"[P101XP-UnityWrapper] %@", text);
}

+ (void)logWithPrefix:(NSString *)prefix withMessage:(NSString *)message {
    [P101XPHelper logWithPrefix:prefix withMessage:message withError:nil];
}

+ (void)logWithPrefix:(NSString *)prefix withError:(NSError *)error {
    [P101XPHelper logWithPrefix:prefix withMessage:nil withError:error];
}

+ (void)logWithPrefix:(NSString *)prefix {
    [P101XPHelper logWithPrefix:prefix withMessage:nil withError:nil];
}

+ (void)logWithMessage:(NSString *)message {
    [P101XPHelper logWithPrefix:nil withMessage:message withError:nil];
}

+ (void)logWithError:(NSError *)error {
    [P101XPHelper logWithPrefix:nil withMessage:nil withError:error];
}

+ (void)logWithErrorMessage:(NSString *)message {
    NSError *error = [P101XPHelper createErrorWithMessage:message];
    [P101XPHelper logWithPrefix:nil withMessage:nil withError:error];
}

+ (NSMutableDictionary *)tokenToDict:(P101XPToken *)token {
    if (token == nil) {
        return nil;
    }

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:token.accessToken forKey: @"accessToken"];
    [dict setValue:[NSNumber numberWithLongLong:token.expires] forKey: @"expires"];
    [dict setValue:[NSNumber numberWithLongLong:token.expiresIn] forKey: @"expiresIn"];
    return dict;
}

+ (NSMutableDictionary *)accountToDict:(P101XPAccountData *)account {
    if (account == nil) {
        return nil;
    }

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:[NSNumber numberWithLongLong:account.userGameId] forKey: @"userGameId"];
    [dict setValue:[NSNumber numberWithLongLong:account.portalId] forKey: @"portalId"];
    [dict setValue:account.displayName forKey: @"userName"];
    [dict setValue:account.photoUrl forKey: @"userPhotoUrl"];
    [dict setValue:[NSNumber numberWithBool:account.isGuest] forKey: @"isGuest"];
    return dict;
}

+ (NSMutableDictionary *)productToDict:(P101XPProduct *)product {
    NSMutableDictionary *prod_dict = [[NSMutableDictionary alloc] init];
    // Basic data
    [prod_dict setValue:product.appStoreProduct.localizedTitle forKey: @"title"];
    [prod_dict setValue:product.appStoreProduct.localizedDescription forKey: @"description"];
    [prod_dict setValue:product.appStoreProduct.productIdentifier forKey: @"productId"];
    // Build up a price strings
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterCurrencyStyle;
    formatter.locale = product.appStoreProduct.priceLocale;  // Set the formatter's locale to the product's priceLocale
    NSString *currencyCode = [product.appStoreProduct.priceLocale objectForKey:NSLocaleCurrencyCode];
    NSString *formattedPrice = [formatter stringFromNumber:product.appStoreProduct.price];
    NSString *priceString = [NSString stringWithFormat:@"%@ %@", currencyCode, formattedPrice];
    [prod_dict setValue:priceString forKey: @"price"];
    [prod_dict setValue:currencyCode forKey: @"currencyCode"];
    [prod_dict setValue:formattedPrice forKey: @"priceValue"];
    return prod_dict;
}

@end
