export default class PlatformDetectionService {
  private static _userAgent: string = navigator.userAgent;
  private static _areClientHintsSupported: boolean = 'userAgentData' in navigator;
  private static _platform: string = '?';
  private static _platformVersion: string = '?';
  private static _browser: string = 'Unknown';
  private static _version: string | number = '?';
  private static _isWebview: boolean = false;
  private static _initialized: boolean = false;

  private constructor() {
    throw new Error('PlatformDetectionService is a static class that may not be instantiated');
  }

  static get platform(): string {
    this.initializeIfNeeded();
    return this._platform;
  }

  static get platformVersion(): string {
    this.initializeIfNeeded();
    return this._platformVersion;
  }

  static get userAgent(): string {
    return this._userAgent;
  }

  static get browser(): string {
    this.initializeIfNeeded();
    return this._browser;
  }

  static get version(): string | number {
    this.initializeIfNeeded();
    return this._version;
  }

  static get isWebview(): boolean {
    this.initializeIfNeeded();
    return this._isWebview;
  }

  static get areClientHintsSupported(): boolean {
    return this._areClientHintsSupported;
  }

  private static initializeIfNeeded(): void {
    if (this._initialized) return;
    this.initialize();
  }

  private static initialize(): void {
    try {
      const browserVersionMatch = this._userAgent.match(/(Chrome|Chromium|Firefox|Opera|Safari|Edge|OPR)\/([0-9]+)/);

      if (browserVersionMatch) {
        const [, browser, version] = browserVersionMatch;
        PlatformDetectionService._browser = browser === 'OPR' ? 'Opera' : browser;
        PlatformDetectionService._version = parseInt(version, 10)?.toString() || '?';
      } else if (this._userAgent.match(/^\(?Mozilla/)) {
        PlatformDetectionService._browser = 'Mozilla';

        // Check for IE/Edge
        if (this._userAgent.match(/MSIE/) || this._userAgent.match(/; Trident\/.*rv:[0-9]+/)) {
          PlatformDetectionService._browser = 'IE';
          const ieVersionMatch = this._userAgent.match(/rv:([0-9]+)/);

          if (ieVersionMatch) {
            PlatformDetectionService._version = parseInt(ieVersionMatch[1], 10)?.toString() || '?';
          }
        } else if (this._userAgent.match(/Edge\//)) {
          PlatformDetectionService._browser = 'Edge';
          const edgeVersionMatch = this._userAgent.match(/Edge\/([0-9]+)/);

          if (edgeVersionMatch) {
            PlatformDetectionService._version = parseInt(edgeVersionMatch[1], 10)?.toString() || '?';
          }
        }
      }

      // Handle Opera masquerading as other browsers
      if (this._userAgent.match(/OPR\//)) {
        PlatformDetectionService._browser = 'Opera';
        const operaVersionMatch = this._userAgent.match(/OPR\/([0-9]+)/);
        if (operaVersionMatch) {
          PlatformDetectionService._version = parseInt(operaVersionMatch[1], 10)?.toString() || '?';
        }
      }

      // Safari and iOS webviews
      if (this._userAgent.match(/AppleWebKit/i)) {
        if (this._userAgent.match(/iphone|ipod|ipad/i)) {
          PlatformDetectionService._browser = 'Safari';
          PlatformDetectionService._isWebview = true;
          const iosVersionMatch = this._userAgent.match(/OS\s([0-9]+)/);
          if (iosVersionMatch) {
            PlatformDetectionService._version = parseInt(iosVersionMatch[1], 10)?.toString() || '?';
          }
        } else if (this._userAgent.match(/Safari\//) && !this._userAgent.match(/Chrome/)) {
          PlatformDetectionService._browser = 'Safari';
          const safariVersionMatch = this._userAgent.match(/Version\/([0-9]+)/);
          if (safariVersionMatch) {
            PlatformDetectionService._version = parseInt(safariVersionMatch[1], 10)?.toString() || '?';
          }
        }
      }

      // Android webviews
      if (this._userAgent.match(/; wv/) || (this._userAgent.match(/Android/) && this._userAgent.match(/Version\/[0-9].[0-9]/))) {
        PlatformDetectionService._isWebview = true;
      }

      // React Native
      if (globalThis.navigator.product === 'ReactNative') {
        PlatformDetectionService._browser = 'ReactNative';
        PlatformDetectionService._version = navigator.productSub || '?';
      }

      // platform information
      if (this._userAgent.match(/Windows/)) {
        PlatformDetectionService._platform = 'Windows';
        const windowsVersionMatch = this._userAgent.match(/Windows NT ([0-9.]+)/);

        if (windowsVersionMatch) {
          PlatformDetectionService._platformVersion = windowsVersionMatch[1];
        }
      } else if (this._userAgent.match(/Mac OS X/)) {
        PlatformDetectionService._platform = 'macOS';
        const macVersionMatch = this._userAgent.match(/Mac OS X ([0-9._]+)/);

        if (macVersionMatch) {
          PlatformDetectionService._platformVersion = macVersionMatch[1].replace(/_/g, '.');
        }
      } else if (this._userAgent.match(/Linux/)) {
        PlatformDetectionService._platform = 'Linux';
      } else if (this._userAgent.match(/Android/)) {
        PlatformDetectionService._platform = 'Android';
        const androidVersionMatch = this._userAgent.match(/Android ([0-9.]+)/);

        if (androidVersionMatch) {
          PlatformDetectionService._platformVersion = androidVersionMatch[1];
        }
      } else if (this._userAgent.match(/iPhone|iPad|iPod/)) {
        PlatformDetectionService._platform = 'iOS';
        const iosVersionMatch = this._userAgent.match(/OS ([0-9_]+)/);

        if (iosVersionMatch) {
          PlatformDetectionService._platformVersion = iosVersionMatch[1].replace(/_/g, '.');
        }
      }

      this._initialized = true;
    } catch (error) {
      console.warn('Failed to initialize PlatformDetectionService:', error);
      // fallback values
      this._browser = 'Unknown';
      this._version = '?';
      this._platform = '?';
      this._platformVersion = '?';
      this._isWebview = false;
      this._initialized = true;
    }
  }
}
