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

class CeilingSensorModel {
  String presenceState;
  int sensitivity;
  String checkingResult;
  int presenceRange;
  int sportsPara;
  String bodyMovement;
  String nobodyTime;
  int movingMaxDis;
  SpaceTypes spaceType;

  CeilingSensorModel(
      {required this.presenceState,
      required this.sensitivity,
      required this.nobodyTime,
      required this.spaceType,
      required this.checkingResult,
      required this.presenceRange,
      required this.sportsPara,
      required this.movingMaxDis,
      required this.bodyMovement});

  factory CeilingSensorModel.fromJson(List<StatusModel> jsonList) {
    String _presenceState = 'none';
    int _sensitivity = 1;
    String _checkingResult = '';
    int _presenceRange = 1;
    int _sportsPara = 1;
    int _moving_max_dis = 0;
    String _bodyMovement = 'none';
    String _nobody_time = 'none';
    SpaceTypes _spaceType = SpaceTypes.none;
    for (int i = 0; i < jsonList.length; i++) {
      if (jsonList[i].code == 'presence_state') {
        _presenceState = jsonList[i].value ?? 'none';
      } else if (jsonList[i].code == 'sensitivity') {
        _sensitivity = jsonList[i].value ?? 1;
      } else if (jsonList[i].code == 'checking_result') {
        _checkingResult = jsonList[i].value ?? '';
      } else if (jsonList[i].code == 'presence_range') {
        _presenceRange = jsonList[i].value ?? 0;
      } else if (jsonList[i].code == 'sports_para') {
        _sportsPara = jsonList[i].value ?? 0;
      } else if (jsonList[i].code == 'body_movement') {
        _bodyMovement = jsonList[i].value ?? '';
      } else if (jsonList[i].code == 'nobody_time') {
        _nobody_time = jsonList[i].value ?? 'none';
      } else if (jsonList[i].code == 'moving_max_dis') {
        _moving_max_dis = jsonList[i].value ?? 0;
      } else if (jsonList[i].code == 'scene')
        _spaceType = getSpaceType(jsonList[i].value ?? 'none');
    }
    return CeilingSensorModel(
        spaceType: _spaceType,
        movingMaxDis: _moving_max_dis,
        presenceState: _presenceState,
        sensitivity: _sensitivity,
        checkingResult: _checkingResult,
        presenceRange: _presenceRange,
        sportsPara: _sportsPara,
        nobodyTime: _nobody_time,
        bodyMovement: _bodyMovement);
  }
}

enum SpaceTypes {
  none,
  parlour,
  area,
  toilet,
  bedroom,
}

SpaceTypes getSpaceType(String value) {
  switch (value) {
    case 'parlour':
      return SpaceTypes.parlour;
    case 'area':
      return SpaceTypes.area;
    case 'toilet':
      return SpaceTypes.toilet;
    case 'bedroom':
      return SpaceTypes.bedroom;
    case 'none':
    default:
      return SpaceTypes.none;
  }
}
