import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/exercise_device.dart';
import 'package:aitrainer_app/service/exercise_device_service.dart';

class ExerciseDeviceRepository {
  List<ExerciseDevice> _devices = [];

  List<ExerciseDevice> getDevices() {
    return this._devices;
  }

  void setDevices(List<ExerciseDevice> list) {
    _devices = list;
  }

  Future<List<ExerciseDevice>> getDBDevices() async {
    this._devices = await ExerciseDeviceApi().getDevices();
    return this._devices;
  }

  bool isGym(int deviceId) {
    bool isGym = false;
    _devices.forEach((element) {
      isGym = isGymElement(element.name);
    });
    return isGym;
  }

  bool isGymElement(String name) {
    return name == "Cable" ||
        name == "Baar" ||
        name == "Gym Machine" ||
        name == "Dumbbells" ||
        name == "Barbell" ||
        name == "HOME" ||
        name == "STREET";
  }

  List<ExerciseDevice> getGymDevices() {
    if (Cache().getDevices() == null) return [];
    final List<ExerciseDevice> gymDevices = [];
    if (_devices.isEmpty) {
      _devices = Cache().getDevices()!;
    }
    _devices.forEach((element) {
      if (isGymElement(element.name)) {
        gymDevices.add(element);
      }
    });
    return gymDevices;
  }
}
