import 'package:objectbox/objectbox.dart';
import 'package:intl/intl.dart';

import 'package:mobdr/service/shared_prefs.dart';

// ignore_for_file: public_member_api_docs

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

  int id_visite;
  int id_photo_typologie;
  String image_name;
  DateTime date_photo;
  int id_photo_mp4;
  int photo_privee;
  int photo_principale;
  String tags;
  int id_concurrence_lien;
  bool depuis_galerie;

  VisitPhoto(
      {this.id = 0,
      required this.id_visite,
      required this.id_photo_typologie,
      required this.image_name,
      this.id_photo_mp4 = -1,
      this.photo_privee = 0,
      this.photo_principale = 0,
      this.tags = '',
      DateTime? date_photo,
      this.id_concurrence_lien = 0,
      this.depuis_galerie = false})
      : date_photo = date_photo ?? DateTime.now();

  static String? _photosDir = SharedPrefs().photosDir;

  String getImage() {
    if (_photosDir == null) {
      throw Exception('Photos directory not initialized');
    }
    if (depuis_galerie == false) {
      return '$_photosDir/$image_name';
    } else
      return image_name;
  }

  String get dateFormat => DateFormat('dd.MM.yyyy hh:mm:ss').format(date_photo);

  VisitPhoto copyWith({
    int? id,
    int? id_visite,
    int? id_photo_typologie,
    String? image,
    String? image_name,
    DateTime? date_photo,
    int? id_photo_mp4,
    int? photo_privee,
    int? photo_principale,
    String? tags,
    int? id_concurrence_lien,
    bool? depuis_galerie,
  }) {
    return VisitPhoto(
      id: id ?? this.id,
      id_visite: id_visite ?? this.id_visite,
      id_photo_typologie: id_photo_typologie ?? this.id_photo_typologie,
      image_name: image_name ?? this.image_name,
      date_photo: date_photo ?? this.date_photo,
      id_photo_mp4: id_photo_mp4 ?? this.id_photo_mp4,
      photo_privee: photo_privee ?? this.photo_privee,
      photo_principale: photo_principale ?? this.photo_principale,
      tags: tags ?? this.tags,
      id_concurrence_lien: id_concurrence_lien ?? this.id_concurrence_lien,
      depuis_galerie: depuis_galerie ?? this.depuis_galerie,
    );
  }
  /*
   Photo.fromJson(Map<String, dynamic> json)
      : id_visite = json['id_visite'],
        id_photo_typologie = json['id_photo_typologie'],
        image = json['image'];
        */
}
