//
//  OBIStreamingGTMHitIdFunctionCallMacroHandler.m
//  OWOX BI Streaming
//
//  Created by Maksym Prokopchuk on 3/11/16.
//  Copyright © 2016 OWOX. All rights reserved.
//

#import "ORZAnalyticsKitGTMHitIdFunctionCallMacroHandler.h"

// Corresponds to the function name field in the Google Tag Manager interface.
NSString *const kOBIStreamingGTMMacroFunctionHitId                 = @"getSessionId";
NSString *const kOBIStreamingGTMMacroFunctionHitIdParamClientIdKey = @"kORZAnalyticsKitGTMMacroFunctionHitIdParamClientId";

@implementation OBIStreamingGTMHitIdFunctionCallMacroHandler

- (id)valueForMacro:(NSString *)functionName parameters:(NSDictionary *)parameters {
    if (![functionName isEqualToString:kOBIStreamingGTMMacroFunctionHitId]) {
        return nil;
    }
    // Process and return the calculated value of this macro accordingly.
    return [self p_hitIdWithClientIdentifier:parameters[kOBIStreamingGTMMacroFunctionHitIdParamClientIdKey]];
}

- (NSString *)p_hitIdWithClientIdentifier:(NSString *)clientId {
    if ([clientId length] == 0) {
        return nil;
    }
    
    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
    return [NSString stringWithFormat:@"%@_%.0f", clientId, timeInterval * 1000];
}

@end
