import { PlatformService } from "./PlatformService";
import { WebPlatformService } from "./platforms/WebPlatformService";
import { CapacitorPlatformService } from "./platforms/CapacitorPlatformService";
import { ElectronPlatformService } from "./platforms/ElectronPlatformService";
import { PyWebViewPlatformService } from "./platforms/PyWebViewPlatformService";

/**
 * Factory class for creating platform-specific service implementations.
 * Implements the Singleton pattern to ensure only one instance of PlatformService exists.
 *
 * The factory determines which platform implementation to use based on the VITE_PLATFORM
 * environment variable. Supported platforms are:
 * - capacitor: Mobile platform using Capacitor
 * - electron: Desktop platform using Electron
 * - pywebview: Python WebView implementation
 * - web: Default web platform (fallback)
 *
 * @example
 * ```typescript
 * const platformService = PlatformServiceFactory.getInstance();
 * await platformService.takePicture();
 * ```
 */
export class PlatformServiceFactory {
  private static instance: PlatformService | null = null;

  /**
   * Gets or creates the singleton instance of PlatformService.
   * Creates the appropriate platform-specific implementation based on environment.
   *
   * @returns {PlatformService} The singleton instance of PlatformService
   */
  public static getInstance(): PlatformService {
    if (PlatformServiceFactory.instance) {
      return PlatformServiceFactory.instance;
    }

    const platform = process.env.VITE_PLATFORM || "web";

    switch (platform) {
      case "capacitor":
        PlatformServiceFactory.instance = new CapacitorPlatformService();
        break;
      case "electron":
        PlatformServiceFactory.instance = new ElectronPlatformService();
        break;
      case "pywebview":
        PlatformServiceFactory.instance = new PyWebViewPlatformService();
        break;
      case "web":
      default:
        PlatformServiceFactory.instance = new WebPlatformService();
        break;
    }

    return PlatformServiceFactory.instance;
  }
}
