#import "EventMark.h"
#import <DataEyeSDK.h>

@interface EventMark ()

@property (nonatomic, strong) DataEyeSDK *dataEyeInstance;

@end

@implementation EventMark

// 静态实例变量
static EventMark *sharedInstance = nil;

// 类方法，用于获取单例实例
+ (instancetype)sharedEventMark {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
    
}

// 初始化 DataEyeSDK 的方法
- (void)initDataEyeSDK {
    
    // NSLog(@"barry initDataEyeSDK-------");
    DEConfig *config = [[DEConfig alloc] init];
    config.appid = @"12730";
    config.configureURL = @"https://deapi.funsdata.com/v1/sdk/report";
    config.debugMode = DataEyeDebug;
    DataEyeSDK * instance = [DataEyeSDK startWithConfig:config];
    
    NSDictionary *superProperties = @{
            @"channel": @"ta",
            @"age": @1,
            @"isSuccess": @YES,
            @"birthday": [NSDate date],
            @"object": @{
                @"key":@"value"
            },
            @"object_arr":@[
                @{
                    @"key":@"value"
                }
            ],
            @"arr": @[@"value"],
        };
    [instance setSuperProperties:superProperties];
        
    [instance enableAutoTrack:DataEyeEventTypeAppInstall | DataEyeEventTypeAppStart | DataEyeEventTypeAppEnd];
    
//    NSDictionary *eventProperties = @{@"new_head": @"100"};
//    [instance track:@"product_buy" properties:eventProperties];
    
    NSString *device = [instance getDeviceId];
    // NSLog(@"barry initDataEyeSDK11-------%@",device);
    
//    [self trackProduct: @"100"];
}

// 追踪产品购买事件的方法
//- (void)trackProduct:(NSString *) data_ {
//    
//    NSLog(@"barry trackProduct-------%@",data_);
//    NSDictionary *eventProperties = data_;
//    [self.dataEyeInstance track:@"test_event" properties:eventProperties];
//}

-(void)trackProduct:(NSString *)eventName dictionaryJson:(NSString *)dictionaryJson;
{
    // NSLog(@"barry trackProduct eventName=== %@",eventName);
    NSError *error;
    NSData *data = [dictionaryJson dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
        
    NSDictionary *eventProperties = dictionary;
    [self.dataEyeInstance track:eventName properties:eventProperties];



    // 处理接收到的字典
    // NSLog(@"barry trackProduct dictionary=== %@", dictionary);
}

@end
