import 'dart:async';
import 'package:flutter/widgets.dart';

import 'bloc/ai_chat_cubit.dart';
import 'bloc/command_state.dart';
import 'models/vehicle_cmd.dart';

/// 车辆命令处理器抽象类
abstract class VehicleCommandHandler {
  AIChatCommandCubit? commandCubit;

  /// 执行车辆控制命令
  Future<(bool, Map<String, dynamic>?)> executeCommand(VehicleCommand command);
}

class AIChatAssistantManager {
  static final AIChatAssistantManager _instance = AIChatAssistantManager._internal();

  static AIChatAssistantManager get instance => _instance;

  AIChatAssistantManager._internal() {
    // 初始化代码
    debugPrint('AIChatAssistant 单例创建');
    _commandCubit = AIChatCommandCubit();
  }

  AIChatCommandCubit? _commandCubit;

  VehicleCommandHandler? commandClient;

  /// 初始化命令处理器
  void setupCommandHandle({
    required VehicleCommandHandler commandHandler,
  }) {
    commandClient = commandHandler;
    commandClient?.commandCubit = _commandCubit;
  }

  /// 获取命令状态流
  Stream<AIChatCommandState> get commandStateStream {
    if (_commandCubit == null) {
      throw StateError('AIChatAssistant 未初始化');
    }
    return _commandCubit!.stream;
  }

  /// 释放资源
  void dispose() {
    _commandCubit?.close();
    _commandCubit = null;
  }
}
