from manifest_models_interface import ManifestLabel, ManifestLabelInterface
from data import model


class ManifestLabelPreOCI(ManifestLabelInterface):
  def get_manifest_labels(self, namespace_name, repository_name, manifestref, filter=None):
    try:
      tag_manifest = model.tag.load_manifest_by_digest(namespace_name, repository_name, manifestref)
    except model.DataModelException:
      return None

    labels = model.label.list_manifest_labels(tag_manifest, prefix_filter=filter)
    return [self._label(l) for l in labels]

  def create_manifest_label(self, namespace_name, repository_name, manifestref, key, value, source_type_name, media_type_name):
    try:
      tag_manifest = model.tag.load_manifest_by_digest(namespace_name, repository_name, manifestref)
    except model.DataModelException:
      return None

    return self._label(model.label.create_manifest_label(tag_manifest, key, value, source_type_name, media_type_name))
  
  def get_manifest_label(self, namespace_name, repository_name, manifestref, label_uuid):
    try:
      tag_manifest = model.tag.load_manifest_by_digest(namespace_name, repository_name, manifestref)
    except model.DataModelException:
      return None

    return self._label(model.label.get_manifest_label(label_uuid, tag_manifest))

  def delete_manifest_label(self, namespace_name, repository_name, manifestref, label_uuid):
    try:
      tag_manifest = model.tag.load_manifest_by_digest(namespace_name, repository_name, manifestref)
    except model.DataModelException:
      return None

    return self._label(model.label.delete_manifest_label(label_uuid, tag_manifest))
  
  def _label(self, label_obj):
    if not label_obj:
      return None
    return ManifestLabel(
      uuid=label_obj.uuid,
      key=label_obj.key,
      value=label_obj.value,
      source_type_name=label_obj.source_type.name,
      media_type_name=label_obj.media_type.name,
    )

pre_oci_model = ManifestLabelPreOCI()