import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:mnemo_cards/di/locator.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';

class ImagesHolder {
  Map<String, MemoryImage> _images = {};
  Map<String, MemoryImage> _packImages = {};
  String? packId;
  DateTime? unlockTime;

  ImagesHolder();

  void setPackImages(Map<String, MemoryImage> images, String packId) {
    _packImages = Map.from(images);
    this.packId = packId;
  }

  void setImage(String key, MemoryImage image) {
    _images[key] = image;
  }

  void clear() {
    _images.clear();
    _packImages.clear();
    packId = null;
  }

  MemoryImage? get(String id) {
    final memoryImage = getPackImage(id) ?? _images[id];

    return memoryImage;
  }

  MemoryImage? getPackImage(String id) {
    final memoryImage = _packImages[id];
    return memoryImage;
  }
}

extension ImagesHolderStringExt on String {
  MemoryImage? get memoryImage => locator.imagesHolder.get(this);
}

extension ImagesHolderCardExt on GameCardDto {
  MemoryImage? get memoryImage => locator.imagesHolder.get(id.toString());
}
