import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:ai_chat_assistant/ai_chat_assistant.dart';

class VehicleCommandClent extends VehicleCommandHandler {
  @override
  AIChatCommandCubit? get commandCubit => super.commandCubit;

  @override
  Future<(bool, Map<String, dynamic>?)> executeCommand(VehicleCommand command) async {
    // 在这里实现具体的车控命令执行逻辑
    print('执行车控命令: ${command.type}, 参数: ${command.params}');

    if (commandCubit != null) {

      commandCubit?.emit(AIChatCommandState(
        commandId: command.commandId,
        commandType: command.type,
        params: command.params,
        status: AIChatCommandStatus.executing,
        timestamp: DateTime.now(),
        errorMessage: null,
        result: null,
      ));
    }

    // 模拟命令执行完成
    await Future.delayed(const Duration(seconds: 2));

    if (commandCubit != null) {
      commandCubit?.emit(AIChatCommandState(
        commandId: command.commandId,
        commandType: command.type,
        params: command.params,
        status: AIChatCommandStatus.success,
        timestamp: DateTime.now(),
        errorMessage: null,
        result: {'message': '命令执行成功'},
      ));
    }

    return Future.value((true, {'message': '命令已执行'}));
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 请求麦克风权限
  if (!await Permission.microphone.isGranted) {
    await Permission.microphone.request();
  }

  // 初始化 AI Chat Assistant，注册车控命令回调
  // ChatAssistantApp.initialize(
  //   commandCallback: (VehicleCommandType type, Map<String, dynamic>? params) async {
  //     // 这里是示例的车控命令处理逻辑
  //     print('收到车控命令: $type, 参数: $params');
  //     return Future.value((true, {'message': '命令已执行'}));
  //   },
  // );

  AIChatAssistantManager.instance.setupCommandHandle(commandHandler: VehicleCommandClent());

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AI Chat Assistant Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: true,
      ),
      home: const ExampleHomePage(),
    );
  }
}

class ExampleHomePage extends StatelessWidget {
  const ExampleHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: ChatAssistantApp(),
    );
  }
}
