import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../config/app_config.dart';

class WebSocketClient {
  WebSocketChannel? _channel;
  final String baseUrl;
  Timer? _heartbeatTimer;
  Timer? _reconnectTimer;
  bool _isConnected = false;
  int _reconnectAttempts = 0;
  String? _lastPath;
  String? _lastToken;
  final _messageController = StreamController<Map<String, dynamic>>.broadcast();

  static const int _maxReconnectAttempts = 10;
  static const Duration _baseReconnectDelay = Duration(milliseconds: 1000);
  static const Duration _maxReconnectDelay = Duration(seconds: 60);

  Stream<Map<String, dynamic>> get messages => _messageController.stream;
  bool get isConnected => _isConnected;

  WebSocketClient({required this.baseUrl});

  Future<void> connect(String path, {String? token}) async {
    _lastPath = path;
    _lastToken = token;

    final uri = Uri.parse('$baseUrl$path');
    try {
      _channel = IOWebSocketChannel.connect(
        uri,
        headers: token != null ? {'Authorization': 'Bearer $token'} : null,
      );
      _isConnected = true;
      _reconnectAttempts = 0;

      _channel!.stream.listen(
        (data) {
          final decoded = jsonDecode(data as String) as Map<String, dynamic>;
          _messageController.add(decoded);
        },
        onDone: () {
          _isConnected = false;
          _scheduleReconnect();
        },
        onError: (error) {
          _isConnected = false;
          _scheduleReconnect();
        },
      );

      _startHeartbeat();
    } catch (e) {
      _isConnected = false;
      _scheduleReconnect();
    }
  }

  void send(Map<String, dynamic> data) {
    if (_isConnected && _channel != null) {
      _channel!.sink.add(jsonEncode(data));
    }
  }

  void _startHeartbeat() {
    _heartbeatTimer?.cancel();
    _heartbeatTimer = Timer.periodic(
      const Duration(seconds: 30),
      (_) => send({'type': 'ping'}),
    );
  }

  /// Reconnects with exponential backoff + jitter, up to [_maxReconnectAttempts].
  void _scheduleReconnect() {
    _heartbeatTimer?.cancel();
    _reconnectTimer?.cancel();

    if (_reconnectAttempts >= _maxReconnectAttempts) return;

    // Exponential backoff: 1s, 2s, 4s, 8s, ... capped at 60s
    final delayMs = min(
      _baseReconnectDelay.inMilliseconds * pow(2, _reconnectAttempts).toInt(),
      _maxReconnectDelay.inMilliseconds,
    );
    final jitter = Random().nextInt(500);

    _reconnectAttempts++;
    _reconnectTimer = Timer(
      Duration(milliseconds: delayMs + jitter),
      () {
        if (_lastPath != null) {
          connect(_lastPath!, token: _lastToken);
        }
      },
    );
  }

  /// Resets reconnect counter and forces an immediate reconnect attempt.
  Future<void> reconnect() async {
    _reconnectAttempts = 0;
    if (_lastPath != null) {
      await connect(_lastPath!, token: _lastToken);
    }
  }

  Future<void> disconnect() async {
    _heartbeatTimer?.cancel();
    _reconnectTimer?.cancel();
    _isConnected = false;
    _reconnectAttempts = _maxReconnectAttempts; // Prevent auto-reconnect
    await _channel?.sink.close();
  }

  void dispose() {
    disconnect();
    _messageController.close();
  }
}

final webSocketClientProvider = Provider<WebSocketClient>((ref) {
  final config = ref.watch(appConfigProvider);
  return WebSocketClient(baseUrl: config.wsBaseUrl);
});
