import 'package:equatable/equatable.dart';

String _parseString(dynamic value, {String fallback = ''}) {
  if (value == null) return fallback;
  if (value is String) return value;
  return value.toString();
}

int _parseInt(dynamic value, {int fallback = 0}) {
  if (value == null) return fallback;
  if (value is int) return value;
  if (value is double) return value.toInt();
  if (value is String) {
    final parsed = int.tryParse(value.trim());
    return parsed ?? fallback;
  }
  return fallback;
}

/// 空调状态 BO - 对应 GET /app/device/ac/status 响应
class AcStatusBO extends Equatable {
  final int deviceId;
  final String deviceName;
  final String powerStatus;       // 电源开关状态码：0=关闭 1=开启
  final String currentTemperature; // 当前室内温度值
  final String targetTemperature;  // 目标设定温度值
  final String mode;              // 当前工作模式码：cooling/heating/dehumidify/fan/auto
  final String modeText;          // 工作模式中文文本
  final String fanSpeed;          // 当前风速档位码：low/mid/high/auto
  final String fanSpeedText;      // 风速中文文本

  const AcStatusBO({
    required this.deviceId,
    required this.deviceName,
    required this.powerStatus,
    required this.currentTemperature,
    required this.targetTemperature,
    required this.mode,
    required this.modeText,
    required this.fanSpeed,
    required this.fanSpeedText,
  });

  factory AcStatusBO.fromJson(Map<String, dynamic> json) {
    return AcStatusBO(
      deviceId: _parseInt(json['deviceId']),
      deviceName: _parseString(json['deviceName']),
      powerStatus: _parseString(json['powerStatus']),
      currentTemperature: _parseString(json['currentTemperature']),
      targetTemperature: _parseString(json['targetTemperature']),
      mode: _parseString(json['mode']),
      modeText: _parseString(json['modeText']),
      fanSpeed: _parseString(json['fanSpeed']),
      fanSpeedText: _parseString(json['fanSpeedText']),
    );
  }

  /// 电源是否开启
  bool get isPowerOn => powerStatus == '1';

  /// 当前温度（int）
  int get currentTempInt => int.tryParse(currentTemperature) ?? 0;

  /// 目标温度（int）
  int get targetTempInt => int.tryParse(targetTemperature) ?? 24;

  @override
  List<Object?> get props => [
        deviceId,
        deviceName,
        powerStatus,
        currentTemperature,
        targetTemperature,
        mode,
        modeText,
        fanSpeed,
        fanSpeedText,
      ];
}

/// Mode 映射工具
class AcModeMapper {
  /// UI 模式名 → GET 返回的 mode 码
  static const Map<String, String> uiToGetMode = {
    '制冷': 'cooling',
    '制热': 'heating',
    '除湿': 'dehumidify',
    '送风': 'fan',
    '自动': 'auto',
  };

  /// GET 返回的 mode 码 → UI 模式名
  static const Map<String, String> getToUiMode = {
    'cooling': '制冷',
    'heating': '制热',
    'dehumidify': '除湿',
    'fan': '送风',
    'auto': '自动',
  };

  /// UI 模式名 → POST 请求的 mode 码
  static const Map<String, String> uiToPostMode = {
    '制冷': 'cool',
    '制热': 'heat',
    '除湿': 'dry',
    '送风': 'fan',
    '自动': 'auto',
  };

  /// UI 风速名 → GET 返回的 API 风速码
  static const Map<String, String> uiToGetFanSpeed = {
    '自动': 'auto',
    '低': 'low',
    '中': 'mid',
    '高': 'high',
  };

  /// GET 返回的 API 风速码 → UI 风速名
  static const Map<String, String> getToUiFanSpeed = {
    'auto': '自动',
    'low': '低',
    'mid': '中',
    'high': '高',
  };

  /// UI 风速名 → POST 请求的 API 风速码（注意：POST 用 medium，GET 用 mid）
  static const Map<String, String> uiToPostFanSpeed = {
    '自动': 'auto',
    '低': 'low',
    '中': 'medium',
    '高': 'high',
  };

  /// 根据 GET mode 码获取 UI 模式名，未知时返回原值
  static String modeTextFromCode(String modeCode) {
    return getToUiMode[modeCode] ?? modeCode;
  }

  /// 根据 UI 模式名获取 POST mode 码
  static String postModeFromUi(String uiModeName) {
    return uiToPostMode[uiModeName] ?? 'cool';
  }

  /// 根据 GET 风速码获取 UI 风速名
  static String fanSpeedTextFromGetCode(String fanSpeedCode) {
    return getToUiFanSpeed[fanSpeedCode] ?? fanSpeedCode;
  }

  /// 根据 UI 风速名获取 POST 风速码
  static String postFanSpeedFromUi(String uiFanSpeedName) {
    return uiToPostFanSpeed[uiFanSpeedName] ?? 'auto';
  }
}
