# Copyright (c) 2021-2022, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
#
"""Keeps a history of file events that is made available via an API to other extensions"""
__all__ = [
    "FileEventHistoryExtension", "FileEventModel",
    "get_latest_urls_from_event_queue", "get_last_url_visited", "get_last_url_opened", "get_last_url_saved"
]

import carb.events
FILE_OPENED_EVENT: int = carb.events.type_from_string("omni.kit.helper.file_utils.FILE_OPENED")
FILE_SAVED_EVENT: int = carb.events.type_from_string("omni.kit.helper.file_utils.FILE_SAVED")
FILE_EVENT_QUEUE_UPDATED: int = carb.events.type_from_string("omni.kit.helper.file_utils.FILE_EVENT_QUEUE_UPDATED")

from typing import List
from .extension import get_instance, FileEventHistoryExtension, FileEventModel


def get_latest_urls_from_event_queue(num_latest: int = 1, asset_type: str = None, event_type: int = 0, tag: str = None) -> List[str]:
    ext = get_instance()
    if ext:
        return ext.get_latest_urls_from_event_queue(num_latest=num_latest, asset_type=asset_type, event_type=event_type, tag=tag)
    return []

def get_last_url_visited(asset_type: str = None, tag: str = None) -> str:
    urls = get_latest_urls_from_event_queue(num_latest=1, asset_type=asset_type, tag=tag)
    if urls:
        return urls[0]
    else:
        return None

def get_last_url_opened(asset_type: str = None, tag: str = None) -> str:
    urls = get_latest_urls_from_event_queue(num_latest=1, asset_type=asset_type, event_type=FILE_OPENED_EVENT, tag=tag)
    if urls:
        return urls[0]
    else:
        return None

def get_last_url_saved(asset_type: str = None, tag: str = None) -> str:
    urls = get_latest_urls_from_event_queue(num_latest=1, asset_type=asset_type, event_type=FILE_SAVED_EVENT, tag=tag)
    if urls:
        return urls[0]
    else:
        return None

def reset_file_event_queue():
    ext = get_instance()
    if ext:
        return ext.clear_event_queue()
