# Copyright (c) 2018-2020, 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.
#
from typing import Callable

import omni.ui as ui

from .ext_info_widget import ExtInfoWidget
from .ext_status_bar import ExtStatusBar
from .exts_graph_window import ExtsGraphWindow
from .exts_list_widget import ExtsListWidget
from .exts_properties_widget import ExtsPropertiesWidget
from .styles import get_style


class PageSwitcher:
    def __init__(self):
        self._main_pages = {}
        self._selected_page = None

    def add_page(self, name, widget):
        widget.set_visible(False)
        self._main_pages[name] = widget

    def select_page(self, name, force=False):
        if self._selected_page == name and not force:
            return
        # Set previous inviisible
        selected_page = self._main_pages.get(self._selected_page, None)
        if selected_page:
            selected_page.set_visible(False)

        # Set new and make visible
        self._selected_page = name
        selected_page = self._main_pages.get(self._selected_page, None)
        if selected_page:
            selected_page.set_visible(True)

    def destroy(self):
        self._main_pages = {}


PAGE_GRAPH = "graph"
PAGE_INFO = "info"
PAGE_PROPERTIES = "properties"


class ExtsWindow:
    """Extensions window"""

    def __init__(self, on_visibility_changed_fn: Callable):
        self._window = ui.Window("Extensions", width=1300, height=800, flags=ui.WINDOW_FLAGS_NO_SCROLLBAR)
        self._window.set_visibility_changed_fn(on_visibility_changed_fn)

        # Tree view style
        self._exts_list_widget = ExtsListWidget()
        self._page_switcher = PageSwitcher()

        # Build UI
        with self._window.frame:
            with ui.HStack(style=get_style(self)):
                with ui.ZStack(width=0):
                    # Draggable splitter
                    with ui.Placer(offset_x=392, draggable=True, drag_axis=ui.Axis.X):
                        ui.Rectangle(width=10, name="Splitter")
                    with ui.HStack():
                        with ui.VStack():
                            # Extensions List Widget (on the left)
                            self._exts_list_widget.build()
                            # Status bar
                            self._status_bar = ExtStatusBar()
                        ui.Spacer(width=10)

                with ui.ScrollingFrame(vertical_scrollbar_policy=ui.ScrollBarPolicy.SCROLLBAR_ALWAYS_OFF):
                    with ui.HStack():
                        # Selected Extension info
                        self._ext_info_widget = ExtInfoWidget()
                        self._page_switcher.add_page(PAGE_INFO, self._ext_info_widget)
                        # Graph
                        self._exts_graph = ExtsGraphWindow()
                        self._page_switcher.add_page(PAGE_GRAPH, self._exts_graph)
                        # Properties
                        self._exts_properties = ExtsPropertiesWidget()
                        self._page_switcher.add_page(PAGE_PROPERTIES, self._exts_properties)

        # Setup show graph callback
        self._exts_list_widget.set_show_graph_fn(lambda: self._show_graph(None))
        self._exts_list_widget.set_show_properties_fn(self._show_properties)
        self._exts_list_widget.set_ext_selected_fn(self._select_ext)
        self._ext_info_widget.set_show_graph_fn(self._show_graph)

        self._select_ext(None)

    def _show_graph(self, ext_id: str):
        self._exts_list_widget.clear_selection()
        self._exts_graph.select_ext(ext_id)
        self._page_switcher.select_page(PAGE_GRAPH, force=True)

    def _show_properties(self):
        self._exts_list_widget.clear_selection()
        self._page_switcher.select_page(PAGE_PROPERTIES)

    def _select_ext(self, ext_summary):
        self._page_switcher.select_page(PAGE_INFO)
        self._ext_info_widget.select_ext(ext_summary)

    def destroy(self):
        """
        Called by extension before destroying this object. It doesn't happen automatically.
        Without this hot reloading doesn't work.
        """
        self._exts_graph.destroy()
        self._exts_graph = None
        self._ext_info_widget.destroy()
        self._ext_info_widget = None
        self._exts_list_widget.destroy()
        self._exts_list_widget = None
        self._exts_properties.destroy()
        self._exts_properties = None
        self._page_switcher.destroy()
        self._page_switcher = None
        self._window = None
