//
//  AppTracker.m
//  Project
//
//  Created by haibara on 11/16/18.
//

#import "AppTracker.h"
#import <CoreData/CoreData.h>
#import "Lang.h"
#import "SDK.h"
#import <PiwikTracker/PiwikTransaction.h>
#import <PiwikTracker/PiwikTransactionBuilder.h>
#import <PiwikTracker/PiwikTransactionItem.h>
#import "WebService.h"
#import "Vars.h"

@interface PiwikTracker ()
@property (nonatomic, readonly, strong) NSManagedObjectModel *managedObjectModel;
- (instancetype)initWithSiteID:(NSString*)siteID dispatcher:(id<PiwikDispatcher>)dispatcher;
@end

@implementation AppTracker

@synthesize managedObjectModel = _managedObjectModel;

static PiwikTracker *_sharedInstance;

#pragma mark - Protected

+ (instancetype)sharedInstanceWithSiteID:(NSString*)siteID dispatcher:(id<PiwikDispatcher>)dispatcher {
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		_sharedInstance = [[[self class] alloc] initWithSiteID:siteID dispatcher:dispatcher];
	});
	_sharedInstance.isPrefixingEnabled = NO;
	_sharedInstance.sessionTimeout = 0;
	_sharedInstance.dispatchInterval = 0;
	_sharedInstance.appName = [NSBundle mainBundle].bundleIdentifier;
	return _sharedInstance;
}

+ (instancetype)sharedInstance {
	if (_sharedInstance) {
		return _sharedInstance;
	}
	return [super sharedInstance];
}

- (NSString *)siteID {
#if DEBUG
	return @"31" ;
#else
	return [Vars piwikSiteId];
#endif
}

- (NSManagedObjectModel*)managedObjectModel {
	if (_managedObjectModel) {
		return _managedObjectModel;
	}
	_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[[NSBundle bundleForClass:[PiwikTracker class]] URLForResource:@"piwiktracker" withExtension:@"momd"]];
	return _managedObjectModel;
}

- (NSString*)platformName {
	return [IMDeviceHardware platformString];
}

#pragma mark - Protected

- (void)sendCartUpdateTransactionWithResponse:(CheckAvailabilityAndPricesResponse *)response {
	if (!response) {
		return;
	}
	if ([Vars appDelegate].shopCarts.entries.count == 0) {
		return;
	}
	PiwikTransactionBuilder *builder = [[PiwikTransactionBuilder alloc] init];
	builder.grandTotal = @(response.default_currency_charge);
	if ([Vars appDelegate].shopCarts.entries.count == response.entries.count) {
		for (int i = 0; i < [Vars appDelegate].shopCarts.entries.count; i++) {
			CartEntry *entry = [Vars appDelegate].shopCarts.entries[i];
			_Entry *_entry = response.entries[i];
			[builder addItemWithSku:[NSString stringWithFormat:@"%d", entry.product.product_id] name:entry.product.name category:entry.product.piwikCategory price:_entry.total_charge quantity:1];
		}
	} else {
		CartEntry *cartEntry = [Vars appDelegate].shopCarts.entries[0];
		[builder addItemWithSku:[NSString stringWithFormat:@"%d", cartEntry.product.product_id] name:cartEntry.product.name category:cartEntry.product.piwikCategory price:response.default_currency_charge quantity:1];
	}
	PiwikTransaction *transaction = [[PiwikTransaction alloc] initWithBuilder:builder];
	[self sendTransaction:transaction];
}

- (void)sendTrackOrderTransactionWithResponse:(CheckAvailabilityAndPricesResponse *)response orderCode:(NSString *)orderCode {
	if (!response || [StringUtils isEmpty:orderCode]) {
		return;
	}
	if ([Vars appDelegate].shopCarts.entries.count == 0) {
		return;
	}
	PiwikTransaction *transaction = [PiwikTransaction transactionWithBuilder:^(PiwikTransactionBuilder *builder) {
		builder.identifier = orderCode;
		builder.grandTotal = @(response.default_currency_charge);
		if ([Vars appDelegate].shopCarts.entries.count == response.entries.count) {
			for (int i = 0; i < [Vars appDelegate].shopCarts.entries.count; i++) {
				CartEntry *entry = [Vars appDelegate].shopCarts.entries[i];
				_Entry *_entry = response.entries[i];
				[builder addItemWithSku:[NSString stringWithFormat:@"%d", entry.product.product_id] name:entry.product.name category:entry.product.piwikCategory price:_entry.total_charge quantity:1];
			}
		} else {
			CartEntry *cartEntry = [Vars appDelegate].shopCarts.entries[0];
			[builder addItemWithSku:[NSString stringWithFormat:@"%d", cartEntry.product.product_id] name:cartEntry.product.name category:cartEntry.product.piwikCategory price:response.default_currency_charge quantity:1];
		}
	}];
	[self sendTransaction:transaction];
}

@end
