import 'package:syncrow_app/features/devices/model/status_model.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';

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

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

  factory AcStatusModel.fromJson(String id, List<StatusModel> jsonList) {
    bool _acSwitch = false;
    String _mode = '';
    int _tempSet = 210;
    int _currentTemp = 210;
    String _fanSpeeds = '';
    int _countdown1 = 0;
    bool _childLock = false;
    for (int i = 0; i < jsonList.length; i++) {
      if (jsonList[i].code == 'switch') {
        _acSwitch = jsonList[i].value ?? false;
      } else if (jsonList[i].code == 'mode') {
        _mode = jsonList[i].value ?? '';
      } else if (jsonList[i].code == 'temp_set') {
        _tempSet = jsonList[i].value ?? 210;
      } else if (jsonList[i].code == 'temp_current') {
        _currentTemp = jsonList[i].value ?? 210;
      } else if (jsonList[i].code == 'level') {
        _fanSpeeds = jsonList[i].value ?? '';
      } else if (jsonList[i].code == 'child_lock') {
        _childLock = jsonList[i].value ?? false;
      } else if (jsonList[i].code == 'countdown_time') {
        _countdown1 = jsonList[i].value ?? 0;
      }
    }
    return AcStatusModel(
        uuid: id,
        acSwitch: _acSwitch,
        modeString: _mode,
        tempSet: _tempSet,
        currentTemp: _currentTemp,
        fanSpeedsString: _fanSpeeds,
        countdown1: _countdown1,
        childLock: _childLock);
  }

  static TempModes getACMode(String value) {
    if (value == 'cold') {
      return TempModes.cold;
    } else if (value == 'hot') {
      return TempModes.hot;
    } else if (value == 'wind') {
      return TempModes.wind;
    } else {
      return TempModes.cold;
    }
  }

  static FanSpeeds getFanSpeed(String value) {
    if (value == 'low') {
      return FanSpeeds.low;
    } else if (value == 'middle') {
      return FanSpeeds.middle;
    } else if (value == 'high') {
      return FanSpeeds.high;
    } else if (value == 'auto') {
      return FanSpeeds.auto;
    } else {
      return FanSpeeds.auto;
    }
  }
}
