import { KochavaMeasurementDeeplink, KochavaMeasurementInstallAttribution, type KochavaMeasurementConfigCompletedListener, type KochavaMeasurementLogLevel } from "react-native-kochava-measurement";
import type { KochavaMeasurementApi } from "./KochavaMeasurementApi";
import { Kochava } from "./web/kochava";

/**
* Implementation for web platforms. This includes pure web and Vega platforms.
*/
export class KochavaMeasurementImpl implements KochavaMeasurementApi {
  private kochava: Kochava;
  
  constructor() {
    // Create the Kochava instance for React
    this.kochava = Kochava.createForReactNative();
  }
  
  executeAdvancedInstruction(name: string, value: string): void {
    this.kochava.executeAdvancedInstruction(name, value);
  }
  
  setLogLevel(logLevel: KochavaMeasurementLogLevel): void {
    this.kochava.setLogLevel(logLevel);
  }
  
  setSleep(enabled: boolean): void {
    this.kochava.setSleep(enabled);
  }
  
  setAppLimitAdTracking(_enabled: boolean): void {
    // Not implemented (no-op) on this platform
  }
  
  registerCustomDeviceIdentifier(name: string, value: string): void {
    this.kochava.registerCustomDeviceIdentifier(name, value);
  }
  
  registerCustomStringValue(_name: string, _value: string | null): void {
    // Not implemented (no-op) on this platform
  }
  
  registerCustomBoolValue(_name: string, _value: boolean | null): void {
    // Not implemented (no-op) on this platform
  }
  
  registerCustomNumberValue(_name: string, _value: number | null): void {
    // Not implemented (no-op) on this platform
  }
  
  registerIdentityLink(name: string, value: string): void {
    this.kochava.registerIdentityLink(name, value);
  }
  
  enableAppleAppClips(_identifier: string): void {
    // Not implemented (no-op) on this platform
  }
  
  enableAppleAtt(): void {
    // Not implemented (no-op) on this platform
  }
  
  setAppleAttAuthorizationWaitTime(_timeInterval: number): void {
    // Not implemented (no-op) on this platform
  }
  
  setAppleAttAuthorizationAutoRequest(_enabled: boolean): void {
    // Not implemented (no-op) on this platform
  }
  
  setAppleAttAuthorizationCustomPrompt(_enabled: boolean): void {
    // Not implemented (no-op) on this platform
  }
  
  appleAttAuthorizationCustomPromptDidComplete(): void {
    // Not implemented (no-op) on this platform
  }
  
  registerPrivacyProfile(_name: string, _keys: string[]): void {
    // Not implemented (no-op) on this platform
  }
  
  setPrivacyProfileEnabled(_name: string, _enabled: boolean): void {
    // Not implemented (no-op) on this platform
  }
  
  setConfigCompletedListener(_configCompletedListener: KochavaMeasurementConfigCompletedListener | null): void {
    // Not implemented (no-op) on this platform
  }
  
  setIntelligentConsentGranted(_granted: boolean): void {
    // Not implemented (no-op) on this platform
  }
  
  getStarted(): Promise<boolean> {
    return Promise.resolve(this.kochava.getStarted());
  }
  
  start(parameters: { [key: string]: string }): void {
    // Extract the webAppGuid from parameters
    const vegaAppGuid = parameters.vegaAppGuid;
    this.kochava.disableAutoPage(true);
    // Attempt to start regardless of if an app guid was provided so we get the SDK log messages.
    this.kochava.startWithAppGuid(vegaAppGuid ?? "");
  }
  
  shutdown(deleteData: boolean): void {
    this.kochava.shutdown(deleteData);
  }
  
  retrieveInstallId(): Promise<string> {
    return Promise.resolve(this.kochava.getDeviceId());
  }
  
  retrieveInstallAttribution(): Promise<KochavaMeasurementInstallAttribution> {
    // Not implemented (no-op) on this platform
    return Promise.resolve(new KochavaMeasurementInstallAttribution(null));
  }
  
  processDeeplink(_path: string): Promise<KochavaMeasurementDeeplink> {
    // Not implemented (no-op) on this platform
    return Promise.resolve(new KochavaMeasurementDeeplink(null));
  }
  
  processDeeplinkWithOverrideTimeout(_path: string, _timeout: number): Promise<KochavaMeasurementDeeplink> {
    // Not implemented (no-op) on this platform
    return Promise.resolve(new KochavaMeasurementDeeplink(null));
  }

  registerDeeplinkWrapperDomain(_domain: string): void {
    // Not implemented (no-op) on this platform
  }
  
  registerDefaultEventStringParameter(_name: string, _value: string | null): void {
    // Not implemented (no-op) on this platform
  }
  
  registerDefaultEventBoolParameter(_name: string, _value: boolean | null): void {
    // Not implemented (no-op) on this platform
  }
  
  registerDefaultEventNumberParameter(_name: string, _value: number | null): void {
    // Not implemented (no-op) on this platform
  }
  
  registerDefaultEventUserId(_value: string | null): void {
    // Not implemented (no-op) on this platform
  }
  
  sendEvent(name: string): void {
    this.kochava.sendEvent(name);
  }
  
  sendEventWithString(name: string, data: string): void {
    this.kochava.sendEvent(name, data);
  }
  
  sendEventWithDictionary(name: string, data: object): void {
    this.kochava.sendEvent(name, data);
  }
  
  sendEventWithEvent(event: any): void {
    const data = event.getData();
    const eventName = data.eventName;
    const eventData = data.eventData;
    
    this.kochava.sendEvent(eventName, eventData);
  }
}