import 'dart:typed_data';

import 'package:networks_sdk/src/data/repositories/networks_messaging_repository_impl.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/facade/networks_messaging_api.dart';
import 'package:networks_sdk/src/presentation/wiring/socket_config.dart';

/// [NetworksMessagingApi] 的实现，透传给 [NetworksMessagingRepository]
class NetworksMessagingApiImpl implements NetworksMessagingApi {
  NetworksMessagingRepository? _repository;

  @override
  void initialize(SocketConfig config) {
    _repository = NetworksMessagingRepositoryImpl();
    _repository!.initialize(config);
  }

  void _checkInitialized() {
    if (_repository == null) {
      throw StateError(
        'NetworksMessagingApi not initialized. Call initialize() first.',
      );
    }
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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