import '../enums/vehicle_command_type.dart';
import '../models/vehicle_status_info.dart';

/// 命令处理回调函数定义
typedef CommandCallback = Future<(bool, Map<String, dynamic>? params)> Function(
    VehicleCommandType type, Map<String, dynamic>? params);

/// 命令处理器类 - 负责处理来自AI的车辆控制命令(使用回调的方式交给App去实现具体的车控逻辑)
class CommandService {
  /// 保存主应用注册的回调函数
  static CommandCallback? onCommandReceived;

  /// 注册命令处理回调
  static void registerCallback(CommandCallback callback) {
    onCommandReceived = callback;
  }

  /// 执行车控命令
  /// 返回命令执行是否成功
  static Future<(bool, Map<String, dynamic>? params)> executeCommand(
      VehicleCommandType type,
      {Map<String, dynamic>? params}) async {
    if (onCommandReceived != null) {
      try {
        return await onCommandReceived!(type, params);
      } catch (e) {
        print('执行命令出错: $e');
      }
    } else {
      print('警告: 命令处理回调未注册');
    }
    return (false, null);
  }

  static bool checkState(
      VehicleCommandType type, Map<String, dynamic>? params) {
    // 获取车辆状态信息
    VehicleStatusInfo vehicleStatusInfo = VehicleStatusInfo();
    switch (type) {
      // 车辆开锁
      case VehicleCommandType.lock:
        return vehicleStatusInfo.leftFrontLockState &&
            vehicleStatusInfo.rightFrontLockState &&
            vehicleStatusInfo.leftRearLockState &&
            vehicleStatusInfo.rightRearLockState;
      // 车辆关锁
      case VehicleCommandType.unlock:
        return !(vehicleStatusInfo.leftFrontLockState &&
            vehicleStatusInfo.rightFrontLockState &&
            vehicleStatusInfo.leftRearLockState &&
            vehicleStatusInfo.rightRearLockState);
      // 打开窗户
      case VehicleCommandType.openWindow:
        return vehicleStatusInfo.leftFrontWindowState &&
            vehicleStatusInfo.rightFrontWindowState &&
            vehicleStatusInfo.leftRearWindowState &&
            vehicleStatusInfo.rightRearWindowState;
      // 关闭窗户
      case VehicleCommandType.closeWindow:
        return !(vehicleStatusInfo.leftFrontWindowState &&
            vehicleStatusInfo.rightFrontWindowState &&
            vehicleStatusInfo.leftRearWindowState &&
            vehicleStatusInfo.rightRearWindowState);
      // 预约空调
      case VehicleCommandType.appointAC:
        // todo
        return false;
      // 打开空调
      case VehicleCommandType.openAC:
        return vehicleStatusInfo.acState && vehicleStatusInfo.acSwitch;
      // 关闭空调
      case VehicleCommandType.closeAC:
        return !vehicleStatusInfo.acState && !vehicleStatusInfo.acSwitch;
      // 极速降温
      case VehicleCommandType.coolSharply:
        // todo
        return false;
      // 一键备车
      case VehicleCommandType.prepareCar:
        return false;
      // 一键融雪
      case VehicleCommandType.meltSnow:
        // todo
        return false;
      // 打开后备箱
      case VehicleCommandType.openTrunk:
        return vehicleStatusInfo.trunkState;
      // 关闭后备箱
      case VehicleCommandType.closeTrunk:
        return !vehicleStatusInfo.trunkState;
      // 车辆鸣笛
      case VehicleCommandType.honk:
        // todo
        return false;
      // 寻找车辆
      case VehicleCommandType.locateCar:
        // todo
        return false;
      // 修改空调温度
      case VehicleCommandType.changeACTemp:
        return vehicleStatusInfo.acState &&
            vehicleStatusInfo.acSwitch &&
            vehicleStatusInfo.acTemp == params?['temperature'];
      // 开启方向盘加热
      case VehicleCommandType.openWheelHeat:
        return vehicleStatusInfo.wheelHeat;
      // 关闭方向盘加热
      case VehicleCommandType.closeWheelHeat:
        return !vehicleStatusInfo.wheelHeat;
      // 开启主座椅加热
      case VehicleCommandType.openMainSeatHeat:
        return vehicleStatusInfo.mainSeatHeat;
      // 关闭主座椅加热
      case VehicleCommandType.closeMainSeatHeat:
        return !vehicleStatusInfo.mainSeatHeat;
      // 开启副座椅加热
      case VehicleCommandType.openMinorSeatHeat:
        return vehicleStatusInfo.minorSeatHeat;
      // 关闭副座椅加热
      case VehicleCommandType.closeMinorSeatHeat:
        return !vehicleStatusInfo.minorSeatHeat;
      default:
        return false;
    }
  }
}
