import 'dart:typed_data';

import 'package:networks_sdk/src/data/datasources/socket/socket_client.dart';
import 'package:networks_sdk/src/domain/entities/socket_connection_state.dart';
import 'package:networks_sdk/src/domain/entities/socket_error.dart';
import 'package:networks_sdk/src/domain/repositories/networks_messaging_repository.dart';
import 'package:networks_sdk/src/presentation/wiring/socket_config.dart';

/// [NetworksMessagingRepository] 的实现，透传给 [SocketClient]
class NetworksMessagingRepositoryImpl implements NetworksMessagingRepository {
  SocketClient? _socketClient;
  bool _isInitialized = false;

  @override
  void initialize(SocketConfig config) {
    _socketClient = SocketClient(config: config);
    _isInitialized = true;
  }

  void _checkInitialized() {
    if (!_isInitialized || _socketClient == null) {
      throw StateError(
        'NetworksMessagingRepository not initialized. Call initialize() first.',
      );
    }
  }

  @override
  Future<bool> connect(String url, {String? token}) {
    _checkInitialized();
    return _socketClient!.connect(url, token: token);
  }

  @override
  Future<void> disconnect() {
    _checkInitialized();
    return _socketClient!.disconnect();
  }

  @override
  bool get isConnected {
    _checkInitialized();
    return _socketClient!.isConnected;
  }

  @override
  SocketConnectionState get connectionState {
    _checkInitialized();
    return _socketClient!.connectionState;
  }

  @override
  void updateToken(String token) {
    _checkInitialized();
    _socketClient!.updateToken(token);
  }

  @override
  Future<bool> send(Map<String, dynamic> message) {
    _checkInitialized();
    return _socketClient!.send(message);
  }

  @override
  Future<bool> sendString(String message) {
    _checkInitialized();
    return _socketClient!.sendString(message);
  }

  @override
  Future<bool> sendBytes(List<int> bytes) {
    _checkInitialized();
    return _socketClient!.sendBytes(bytes);
  }

  @override
  Stream<Map<String, dynamic>> get messageStream {
    _checkInitialized();
    return _socketClient!.messageStream;
  }

  @override
  Stream<String> get rawMessageStream {
    _checkInitialized();
    return _socketClient!.rawMessageStream;
  }

  @override
  Stream<Uint8List> get binaryMessageStream {
    _checkInitialized();
    return _socketClient!.binaryMessageStream;
  }

  @override
  Stream<SocketConnectionState> get connectionStateStream {
    _checkInitialized();
    return _socketClient!.connectionStateStream;
  }

  @override
  Stream<SocketError> get errorStream {
    _checkInitialized();
    return _socketClient!.errorStream;
  }

  @override
  void onEnterForeground() {
    _checkInitialized();
    _socketClient!.onEnterForeground();
  }

  @override
  void onEnterBackground() {
    _checkInitialized();
    _socketClient!.onEnterBackground();
  }

  @override
  Future<void> dispose() async {
    if (_socketClient != null) {
      await _socketClient!.dispose();
      _socketClient = null;
    }
    _isInitialized = false;
  }
}
