import { Injectable } from '@angular/core';
import { MatomoInjector, MatomoTracker } from 'ngx-matomo';
import { AuthService } from '../auth/auth.service';
import { ConfigurationService } from '../configuration/configuration.service';

@Injectable()
export class MatomoService {

	constructor(
		private configurationService: ConfigurationService,
		private matomoInjector: MatomoInjector,
		private matomoTracker: MatomoTracker,
		private authService: AuthService
	) {

	}

	init() {
		if (this.configurationService.matomoEnabled) {
			this.matomoInjector.init(this.configurationService.matomoSiteUrl, this.configurationService.matomoSiteId);
		}
	}

	trackPageView(customTitle?: string): void {
		if (this.configurationService.matomoEnabled) {
			var principal = this.authService.current();
			if (principal != null) { this.matomoTracker.setUserId(principal.id); }
			this.matomoTracker.trackPageView(customTitle);
		}
	}

	trackSiteSearch(keyword: string, category?: string, resultsCount?: number): void {
		if (this.configurationService.matomoEnabled) {
			var principal = this.authService.current();
			if (principal != null) { this.matomoTracker.setUserId(principal.id); }
			this.matomoTracker.trackSiteSearch(keyword, category, resultsCount);
		}
	}
}
