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

class FlushSensorModel {
  FlushSensorModel({
    required this.presenceState,
    required this.farDetection,
    required this.illuminance,
    required this.sensitivity,
    required this.occurDistReduce,
    required this.noneDelay,
    required this.presenceDelay,
    required this.nearDetection,
    required this.sensiReduce,
    required this.checkingResult,
  });

  static const String codePresenceState = 'presence_state';
  static const String codeSensitivity = 'sensitivity';
  static const String codeNearDetection = 'near_detection';
  static const String codeFarDetection = 'far_detection';
  static const String codeCheckingResult = 'checking_result';
  static const String codePresenceDelay = 'presence_delay';
  static const String codeNoneDelay = 'none_delay';
  static const String codeOccurDistReduce = 'occur_dist_reduce';
  static const String codeIlluminance = 'illum_value';
  static const String codeSensiReduce = 'sensi_reduce';

  String presenceState;
  int sensitivity;
  int nearDetection;
  int farDetection;
  String checkingResult;
  int presenceDelay;
  int noneDelay;
  int occurDistReduce;
  int illuminance;
  int sensiReduce;

  factory FlushSensorModel.fromJson(List<StatusModel> jsonList) {
    String presenceState = 'none';
    int sensitivity = 0;
    int nearDetection = 0;
    int farDetection = 0;
    String checkingResult = 'none';
    int presenceDelay = 0;
    int noneDelay = 0;
    int occurDistReduce = 0;
    int illuminance = 0;
    int sensiReduce = 0;

    for (var status in jsonList) {
      switch (status.code) {
        case codePresenceState:
          presenceState = status.value ?? 'presence';
          break;
        case codeSensitivity:
          sensitivity = status.value ?? 0;
          break;
        case codeNearDetection:
          nearDetection = status.value ?? 0;
          break;
        case codeFarDetection:
          farDetection = status.value ?? 0;
          break;
        case codeCheckingResult:
          checkingResult = status.value ?? 'check_success';
          break;
        case codePresenceDelay:
          presenceDelay = status.value ?? 0;
          break;
        case codeNoneDelay:
          noneDelay = status.value ?? 0;
          break;
        case codeOccurDistReduce:
          occurDistReduce = status.value ?? 0;
          break;
        case codeIlluminance:
          illuminance = status.value ?? 0;
          break;
        case codeSensiReduce:
          sensiReduce = status.value ?? 0;
          break;
      }
    }

    return FlushSensorModel(
      presenceState: presenceState,
      sensitivity: sensitivity,
      nearDetection: nearDetection,
      farDetection: farDetection,
      checkingResult: checkingResult,
      presenceDelay: presenceDelay,
      noneDelay: noneDelay,
      occurDistReduce: occurDistReduce,
      illuminance: illuminance,
      sensiReduce: sensiReduce,
    );
  }
}
