import 'package:flutter/material.dart';

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

class CeilingSensorModel {
  String presenceState;
  int sensitivity;
  String checkingResult;
  int presenceRange;
  int sportsPara;
  String bodyMovement;
  String noBodyTime;
  int maxDistance;
  SpaceTypes spaceType;

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

  factory CeilingSensorModel.fromJson(List<Status> jsonList) {
    late String _presenceState = 'none';
    late int _sensitivity = 1;
    late String _checkingResult = '';
    int _presenceRange = 1;
    int _sportsPara = 1;
    String _bodyMovement = 'none';
    String _noBodyTime = 'none';
    int _maxDis = 0;
    SpaceTypes _spaceType = SpaceTypes.none;

    try {
      for (var status in jsonList) {
        switch (status.code) {
          case 'presence_state':
            _presenceState = status.value ?? 'none';
            break;
          case 'scene':
            _spaceType = getSpaceType(status.value ?? 'none');
            break;
          case 'sensitivity':
            _sensitivity =
                status.value is int ? status.value : int.tryParse(status.value ?? '1') ?? 1;
            break;
          case 'checking_result':
            _checkingResult = status.value ?? '';
            break;
          case 'presence_range':
            _presenceRange =
                status.value is int ? status.value : int.tryParse(status.value ?? '0') ?? 0;
            break;
          case 'sports_para':
            _sportsPara =
                status.value is int ? status.value : int.tryParse(status.value ?? '0') ?? 0;
            break;
          case 'body_movement':
            _bodyMovement = status.value ?? '';
            break;
          case 'nobody_time':
            _noBodyTime = status.value ?? 'none';
            break;
          case 'moving_max_dis':
            _maxDis = status.value is int ? status.value : int.tryParse(status.value ?? '0') ?? 0;
            break;
        }
      }
    } catch (e) {
      debugPrint(e.toString());
    }

    return CeilingSensorModel(
      presenceState: _presenceState,
      sensitivity: _sensitivity,
      checkingResult: _checkingResult,
      presenceRange: _presenceRange,
      sportsPara: _sportsPara,
      bodyMovement: _bodyMovement,
      noBodyTime: _noBodyTime,
      maxDistance: _maxDis,
      spaceType: _spaceType,
    );
  }

  CeilingSensorModel copyWith({
    String? presenceState,
    int? sensitivity,
    String? checkingResult,
    int? presenceRange,
    int? sportsPara,
    String? bodyMovement,
    String? noBodyTime,
    int? maxDistance,
    SpaceTypes? spaceType,
  }) {
    return CeilingSensorModel(
      presenceState: presenceState ?? this.presenceState,
      sensitivity: sensitivity ?? this.sensitivity,
      checkingResult: checkingResult ?? this.checkingResult,
      presenceRange: presenceRange ?? this.presenceRange,
      sportsPara: sportsPara ?? this.sportsPara,
      bodyMovement: bodyMovement ?? this.bodyMovement,
      noBodyTime: noBodyTime ?? this.noBodyTime,
      maxDistance: maxDistance ?? this.maxDistance,
      spaceType: spaceType ?? this.spaceType,
    );
  }
}

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;
  }
}
