import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:syncrow_web/utils/constants/app_enum.dart';

class AcStatusModel {
  final String uuid;
  final bool acSwitch;
  final String modeString;
  final int tempSet;
  final int currentTemp;
  final String fanSpeedsString;
  final bool childLock;
  final TempModes acMode;
  final FanSpeeds acFanSpeed;
  final int countdown1;

  AcStatusModel({
    required this.uuid,
    required this.acSwitch,
    required this.modeString,
    required this.tempSet,
    required this.currentTemp,
    required this.countdown1,
    required this.fanSpeedsString,
    required this.childLock,
  })  : acMode = getACMode(modeString),
        acFanSpeed = getFanSpeed(fanSpeedsString);

  factory AcStatusModel.fromJson(String id, List<Status> jsonList) {
    late bool acSwitch;
    late String mode;
    late int tempSet;
    late int currentTemp;
    late String fanSpeeds;
    late bool childLock;
    late int _countdown1 = 0;

    for (var status in jsonList) {
      switch (status.code) {
        case 'switch':
          acSwitch = status.value ?? false;
          break;
        case 'mode':
          mode = status.value ?? 'cold';
          break;
        case 'temp_set':
          tempSet = status.value ?? 210;
          break;
        case 'temp_current':
          currentTemp = status.value ?? 210;
          break;
        case 'level':
          fanSpeeds = status.value ?? 'low';
          break;
        case 'child_lock':
          childLock = status.value ?? false;
          break;
        case 'countdown_time':
          _countdown1 = status.value ?? 0;
          break;
      }
    }

    return AcStatusModel(
      uuid: id,
      acSwitch: acSwitch,
      modeString: mode,
      tempSet: tempSet,
      currentTemp: currentTemp,
      fanSpeedsString: fanSpeeds,
      childLock: childLock,
      countdown1: _countdown1,
    );
  }

  AcStatusModel copyWith({
    String? uuid,
    bool? acSwitch,
    String? modeString,
    int? tempSet,
    int? currentTemp,
    String? fanSpeedsString,
    bool? childLock,
    int? countdown1,
  }) {
    return AcStatusModel(
      uuid: uuid ?? this.uuid,
      acSwitch: acSwitch ?? this.acSwitch,
      modeString: modeString ?? this.modeString,
      tempSet: tempSet ?? this.tempSet,
      currentTemp: currentTemp ?? this.currentTemp,
      fanSpeedsString: fanSpeedsString ?? this.fanSpeedsString,
      childLock: childLock ?? this.childLock,
      countdown1: countdown1 ?? this.countdown1,
    );
  }

  static TempModes getACMode(String value) {
    switch (value) {
      case 'cold':
        return TempModes.cold;
      case 'hot':
        return TempModes.hot;
      case 'wind':
        return TempModes.wind;
      default:
        return TempModes.cold;
    }
  }

  static FanSpeeds getFanSpeed(String value) {
    switch (value) {
      case 'low':
        return FanSpeeds.low;
      case 'middle':
        return FanSpeeds.middle;
      case 'high':
        return FanSpeeds.high;
      case 'auto':
        return FanSpeeds.auto;
      default:
        return FanSpeeds.auto;
    }
  }
}
