import 'package:aitrainer_app/model/exercise_ability.dart';
import 'package:aitrainer_app/util/app_language.dart';
import 'package:flutter/material.dart';

class ExerciseType {
  ///exerciseTypeId
  late int exerciseTypeId;

  /// name
  late String name;

  /// description
  late String description;

  /// unit
  late String unit;

  /// unitQuantity
  late String unitQuantity;

  /// unitQuantityUnit
  String? unitQuantityUnit;

  ///active
  late bool active;

  /// base
  late bool base;

  /// imageUrl
  String imageUrl = "";

  /// nameTranslation
  String nameTranslation = "";

  /// descriptionTranslation
  String descriptionTranslation = "";

  /// devices[]
  List<int> devices = [];

  /// parents[]
  List<int> parents = [];

  /// alternatives []
  List<int> alternatives = [];

  /// ability
  ExerciseAbility? ability;

  ExerciseType({required this.name, required this.description});

  ExerciseType.fromJson(Map json) {
    this.exerciseTypeId = json['exerciseTypeId'];
    //this.treeId = json['treeId'];
    this.name = json['name'];
    this.description = json['description'];
    this.unit = json['unit'];
    this.unitQuantity = json['unitQuantity'];
    this.unitQuantityUnit = json['unitQuantityUnit'];
    this.active = json['active'];
    this.base = json['base'];
    if (json['images'].length > 0) {
      this.imageUrl = json['images'][0]['url'];
    }
    if (json['translations'].length > 0) {
      this.nameTranslation = AppLanguage().appLocal == Locale('hu') ? json['translations'][0]['name'] : json['name'];
      this.descriptionTranslation = AppLanguage().appLocal == Locale('hu') ? json['translations'][0]['description'] : json['description'];
    }

    if (json['devices'].length > 0) {
      final List jsonDevices = json['devices'];

      jsonDevices.forEach((device) {
        this.devices.add(device['exerciseDeviceId']);
      });
    }

    if (json['parents'].length > 0) {
      final List jsonParents = json['parents'];

      jsonParents.forEach((parent) {
        this.parents.add(parent['exerciseTreeId']);
      });
    }

    if (json['alternatives'].length > 0) {
      final List jsonAlternatives = json['alternatives'];

      jsonAlternatives.forEach((alternative) {
        this.alternatives.add(alternative['exerciseTypeChildId']);
      });
    }
  }

  Map<String, dynamic> toJson() => {
        "name": name,
        "description": description,
        "unit": unit,
        "unitQuantity": unitQuantity,
        "unitQuantityUnit": unitQuantityUnit,
        "active": active,
        "devices": this.devices.toString(),
        "nameTranslation": this.nameTranslation,
        "parents": this.parents.toString()
      };

  void setAbility(ExerciseAbility ability) {
    this.ability = ability;
  }

  ExerciseAbility getAbility() {
    return this.ability!;
  }

  bool is1RM() {
    return this.ability!.equalsTo(ExerciseAbility.oneRepMax);
  }

  @override
  String toString() {
    return this.toJson().toString();
  }
}
