import 'dart:html' as html;
import 'package:flutter/foundation.dart';

class PortDetector {
  /// Detects the current port from the browser's window.location
  /// Returns null if not running on web platform or if detection fails
  static String? getCurrentPort() {
    if (!kIsWeb) {
      return null;
    }

    try {
      final location = html.window.location;
      final port = location.port;
      
      // If port is empty, it means we're using default ports
      if (port.isEmpty) {
        // Return default ports based on protocol
        return location.protocol == 'https:' ? '443' : '80';
      }
      
      return port;
    } catch (e) {
      // If anything goes wrong, return null to fall back to defaults
      return null;
    }
  }

  /// Gets the current hostname from the browser
  /// Returns null if not running on web platform or if detection fails
  static String? getCurrentHostname() {
    if (!kIsWeb) {
      return null;
    }

    try {
      return html.window.location.hostname;
    } catch (e) {
      return null;
    }
  }

  /// Gets the current protocol from the browser
  /// Returns null if not running on web platform or if detection fails
  static String? getCurrentProtocol() {
    if (!kIsWeb) {
      return null;
    }

    try {
      final protocol = html.window.location.protocol;
      // Remove the trailing colon
      return protocol.endsWith(':') ? protocol.substring(0, protocol.length - 1) : protocol;
    } catch (e) {
      return null;
    }
  }

  /// Builds the base URL using detected browser information
  /// Falls back to provided defaults if detection fails
  static String buildBaseUrl({
    String defaultHost = '[::1]',
    String defaultPort = '8080',
    String defaultProtocol = 'http',
  }) {
    final detectedProtocol = getCurrentProtocol() ?? defaultProtocol;
    final detectedHost = getCurrentHostname() ?? defaultHost;
    final detectedPort = getCurrentPort() ?? defaultPort;

    // For IPv6 addresses, we need to wrap them in brackets
    final host = detectedHost.contains(':') && !detectedHost.startsWith('[') 
        ? '[$detectedHost]' 
        : detectedHost;

    return '$detectedProtocol://$host:$detectedPort';
  }

  /// Builds the WebSocket URL using detected browser information
  /// Falls back to provided defaults if detection fails
  static String buildWebSocketUrl({
    String defaultHost = '[::1]',
    String defaultPort = '8080',
    String defaultProtocol = 'ws',
  }) {
    final detectedProtocol = getCurrentProtocol();
    final detectedHost = getCurrentHostname() ?? defaultHost;
    final detectedPort = getCurrentPort() ?? defaultPort;

    // Convert HTTP protocol to WebSocket protocol
    String wsProtocol = defaultProtocol;
    if (detectedProtocol != null) {
      wsProtocol = detectedProtocol == 'https' ? 'wss' : 'ws';
    }

    // For IPv6 addresses, we need to wrap them in brackets
    final host = detectedHost.contains(':') && !detectedHost.startsWith('[') 
        ? '[$detectedHost]' 
        : detectedHost;

    return '$wsProtocol://$host:$detectedPort/ws';
  }
}
