import 'dart:typed_data';

import 'package:hl_lieferservice/model/delivery.dart';
import 'package:hl_lieferservice/feature/delivery/detail/service/notes_service.dart';

class NoteRepository {
  final NoteService service;

  NoteRepository({required this.service});

  Future<Note> addNote(String deliveryId, String content) async {
    return service.addNote(content, int.parse(deliveryId));
  }

  Future<void> editNote(String noteId, String content) async {
    return service.editNote(Note(content: content, id: int.parse(noteId)));
  }

  Future<void> deleteNote(String noteId) async {
    return service.deleteNote(int.parse(noteId));
  }

  Future<List<Note>> loadNotes(String deliveryId) async {
    return service.getNotes(deliveryId);
  }

  Future<List<Uint8List>> loadImages(List<String> urls) async {
    List<Uint8List> images = [];

    for (final image in await service.downloadImages(urls)) {
      images.add(await image);
    }

    return images;
  }

  Future<List<NoteTemplate>> loadTemplates() async {
    return service.getNoteTemplates();
  }

  Future<ImageNote> addImage(String deliveryId, Uint8List bytes) async {
    final fileName =
        "delivery_note_${deliveryId}_${DateTime.timestamp().microsecondsSinceEpoch}.jpg";

    String objectId = await service.uploadImage(
      deliveryId,
      fileName,
      bytes,
      "image/png",
    );

    return ImageNote.make(objectId, fileName);
  }

  Future<ImageNote> addNamedImage(String deliveryId, Uint8List bytes, String filename) async {
    String objectId = await service.uploadImage(
      deliveryId,
      filename,
      bytes,
      "image/png",
    );

    return ImageNote.make(objectId, filename);
  }

  Future<void> deleteImage(String deliveryId, String objectId) async {
    await service.removeImage(objectId);
  }
}
