import omni.ui as ui
import omni.kit.clipboard

from .about_widget import AboutWidget
from .style import ABOUT_STYLE

class AboutWindow(ui.Window):
    def __init__(self):
        super().__init__(
            "About",
            width=800,
            height=540,
            flags=ui.WINDOW_FLAGS_NO_SCROLLBAR | ui.WINDOW_FLAGS_NO_DOCKING,
            visible=False
        )
        self.frame.set_build_fn(self._build_ui)
        self.frame.set_style(ABOUT_STYLE)

    def destroy(self):
        self.visible = False
        super().destroy()

    def _build_ui(self):
        with self.frame:
            with ui.ZStack():
                with ui.VStack():
                    self._widget = AboutWidget()
                    ui.Line(height=0)
                    ui.Spacer(height=4)
                    with ui.HStack(height=26):
                        ui.Spacer()
                        ui.Button("Close", width=80, clicked_fn=self._hide, style={"padding": 0, "margin": 0})
                ui.Button(
                    "###copy_to_clipboard",
                    style={"background_color": 0x00000000},
                    mouse_pressed_fn=lambda x, y, b, a: self.__copy_to_clipboard(b),
                    identifier="copy_to_clipboard",
                )

    def _hide(self):
        self.visible = False

    def __copy_to_clipboard(self, button):
        if button != 1:
            return

        omni.kit.clipboard.copy("\n".join(self._widget.versions))