# Copyright (c) 2021, 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.
#
import asyncio
import weakref

import carb
import omni.client
import omni.ui as ui
from omni.kit.window.property.templates import SimplePropertyWidget


class FileWidget(SimplePropertyWidget):
    def __init__(self):
        super().__init__(title="File Info", collapsed=False)
        self._stat_task = None
        self._created_by_model = ui.SimpleStringModel()
        self._created_time_model = ui.SimpleStringModel()
        self._modified_by_model = ui.SimpleStringModel()
        self._modified_time_model = ui.SimpleStringModel()
        self._file_size_model = ui.SimpleStringModel()

    def clean(self):
        super().clean()
        self._created_by_model = None
        self._created_time_model = None
        self._modified_by_model = None
        self._modified_time_model = None
        self._file_size_model = None

    def on_new_payload(self, payload):
        if not super().on_new_payload(payload):
            return False

        # Only support one selected path
        if len(payload) != 1:
            return False

        return True

    def reset(self):
        if self._stat_task:
            self._stat_task.cancel()
            self._stat_task = None
        if self._created_by_model:
            self._created_by_model.set_value("")
        if self._created_time_model:
            self._created_time_model.set_value("")
        if self._modified_by_model:
            self._modified_by_model.set_value("")
        if self._modified_time_model:
            self._modified_time_model.set_value("")
        if self._file_size_model:
            self._file_size_model.set_value("")
        super().reset()

    async def _stat_file_async(self):
        result, entry = await omni.client.stat_async(self._payload[0].path)
        if not result or not entry:
            return

        if result == omni.client.Result.OK:
            self._created_by_model.set_value(entry.created_by)
            self._created_time_model.set_value(entry.created_time.strftime("%x %X"))
            self._modified_by_model.set_value(entry.modified_by)
            self._modified_time_model.set_value(entry.modified_time.strftime("%x %X"))
            self._file_size_model.set_value(f"{entry.size:,} " + ("Bytes" if entry.size > 1 else "Byte"))
        else:
            carb.log_warn(f"Failed to get file info for {self._payload[0].path}")

        self._stat_task = None

    def _stat_file(self):
        self._stat_task = asyncio.ensure_future(self._stat_file_async())

    def build_items(self):
        self._stat_file()

        self.add_item_with_model("Date Created", self._created_time_model, identifier="created_time")
        self.add_item_with_model("Created by", self._created_by_model, identifier="created_by")
        self.add_item_with_model("Date Modified", self._modified_time_model, identifier="modified_time")
        self.add_item_with_model("Modified by", self._modified_by_model, identifier="modified_by")
        self.add_item_with_model("File Size", self._file_size_model, identifier="file_size")
