# Copyright (c) 2018-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.
#
__all__ = ["ConsoleWindow"]

import omni.kit
import omni.ui as ui

from .._console import ConsoleWidget


class ConsoleWindow(ui.Window):
    """The Console window"""

    def __init__(self):
        self._title = "Console"

        super().__init__(
            self._title,
            width=1000,
            height=600,
            dockPreference=ui.DockPreference.LEFT_BOTTOM,
            raster_policy=ui.RasterPolicy.NEVER
        )

        # Dock it to the same space where Content is docked, make it the second tab and the active tab.
        self.deferred_dock_in("Content", ui.DockPolicy.CURRENT_WINDOW_IS_ACTIVE)
        self.dock_order = 1

        self.set_visibility_changed_fn(self._visibility_changed_fn)
        with self.frame:
            self._console_widget = ConsoleWidget()

    def _visibility_changed_fn(self, visible):
        if self._visiblity_changed_listener:
            self._visiblity_changed_listener(visible)

    def set_visibility_changed_listener(self, listener):
        self._visiblity_changed_listener = listener

    def destroy(self):
        """
        Called by extension before destroying this object. It doesn't happen automatically.
        Without this hot reloading doesn't work.
        """
        if self._console_widget:
            self._console_widget.destroy()
            self._console_widget = None
        self._visiblity_changed_listener = None
        super().destroy()
