import 'package:objectbox/objectbox.dart';
import 'package:mobdr/objectbox.g.dart';
import 'package:xml/xml.dart';

// ignore_for_file: public_member_api_docs

@Entity()
class PhotoTypology {
  // specify the id
  @Id()
  int id = 0;

  int id_photo_typologie;
  String libelle;
  int ordre;

  PhotoTypology({
    this.id = 0,
    required this.id_photo_typologie,
    required this.libelle,
    required this.ordre,
  });

  PhotoTypology.fromJson(Map<String, dynamic> json)
      : id_photo_typologie = json['id_photo_typologie'],
        libelle = getI18nLabel(json['libelle']),
        ordre = json['ordre'];
}

String getI18nLabel(String label) {
  try {
    String translatedText = '';
    final document = XmlDocument.parse(label);

    translatedText = document.children[0].getElement('fr')!.text;

    if (translatedText == '') {
      translatedText = document.children[0].getElement('fr')!.text;
    }

    return translatedText;
  } catch (e) {}
  return label;
}
