//
/***************************************************************
 * Copyright © 2017 HERE Global B.V. All rights reserved. *
 **************************************************************/

#import <Foundation/Foundation.h>
#import <React/RCTLog.h>

#import "HereMobilitySDK.h"
#import "NSString+Utilities.h"
#import "HereSDKUser+Dictionary.h"

@implementation HereMobilitySDK

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(setUser:(NSDictionary *)userInfo) {
    
    HereSDKUser *user = nil;
    if(userInfo){
        
        // User Info.
        NSString* userID = [userInfo objectForKey:@"userId"];
        NSNumber* expiration = [userInfo objectForKey:@"expirationSec"];
        NSString* verificationHash = [userInfo objectForKey:@"hash"];
        
        //Set the user.
        user = [HereSDKUser userWithId:userID expiration:[NSDate dateWithTimeIntervalSince1970:expiration.longLongValue] verificationHash: verificationHash];
    }
    HereSDKManager.sharedManager.user = user;
}

RCT_EXPORT_METHOD(getUser:(RCTResponseSenderBlock)cb) {
    HereSDKUser *user = HereSDKManager.sharedManager.user;
    if (user) {
        cb(@[[NSString hereSDK_jsonFromObject:user.dictionary]]);
    } else {
        cb(@[[NSNull null]]);
    }
}

RCT_EXPORT_METHOD(getLoggingId:(RCTResponseSenderBlock)cb) {
    cb(@[[HereSDKManager sharedManager].loggingId]);
}

@end


