## 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 omni.kit.test
import omni.kit.app
from omni.kit.test import AsyncTestCase

import omni.kit.ui_test as ui_test
from omni.ui.tests.test_base import OmniUiTest
from pathlib import Path

from omni.kit.window.script_editor.scripts import ScriptEditorWindow
import omni.ui as ui

CURRENT_PATH = Path(__file__).parent.joinpath("../../../../../data")

# Use it to receive extraterrestrial messages from executing script
public_mailbox = ""


class TestScriptEditor(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        global public_mailbox
        public_mailbox = ""
        window = ScriptEditorWindow()
        await ui_test.wait_n_updates(2)
        self.editor = ui_test.find("Script Editor")
        await self.editor.focus()

        # Close tab and clear script
        await ui_test.emulate_key_combo("CTRL+W")
        window.clear_data()

        # Focus on text field
        await ui_test.emulate_mouse_move_and_click(ui_test.Vec2(700, 500))

    async def test_script_run(self):
        await ui_test.emulate_char_press("import omni.kit.window.script_editor.tests.test_script_editor as editor\n")
        await ui_test.emulate_char_press("editor.public_mailbox = 'who is there?'\n")
        await ui_test.wait_n_updates(2)
        await ui_test.emulate_key_combo("CTRL+ENTER")
        self.assertEqual(public_mailbox, "who is there?")

        # Add another line
        await ui_test.emulate_char_press("editor.public_mailbox = 123\n")
        await ui_test.wait_n_updates(2)
        await ui_test.emulate_key_combo("CTRL+ENTER")
        self.assertEqual(public_mailbox, 123)

        # New tab
        await ui_test.emulate_key_combo("CTRL+N")
        await ui_test.emulate_mouse_move_and_click(ui_test.Vec2(700, 500))
        await ui_test.emulate_char_press("import omni.kit.window.script_editor.tests.test_script_editor as editor\n")
        await ui_test.emulate_char_press("editor.public_mailbox = 'yellow'\n")
        await ui_test.wait_n_updates(2)
        await ui_test.emulate_key_combo("CTRL+ENTER")
        self.assertEqual(public_mailbox, "yellow")

    async def test_script_fail(self):
        await ui_test.emulate_char_press("import omni.kit.window.script_editor.tests.test_script_editor as editor\n")
        await ui_test.emulate_char_press("editor.public_mailbox = 555\n")
        await ui_test.emulate_char_press("x = \n") # typo
        await ui_test.wait_n_updates(2)
        await ui_test.emulate_key_combo("CTRL+ENTER")
        self.assertEqual(public_mailbox, "")

        # fix it
        await ui_test.emulate_key_combo("BACKSPACE")
        await ui_test.emulate_key_combo("BACKSPACE")
        await ui_test.emulate_char_press("3")
        await ui_test.wait_n_updates(2)
        await ui_test.emulate_key_combo("CTRL+ENTER")
        self.assertEqual(public_mailbox, 555)

    async def test_script_get_source(self):
        # That tests: exts."omni.kit.window.script_editor".executeInTempFile = true
        # If we are not writing to a temp file ast/inspect doesn't work and throws error that it can't find source.
        await ui_test.emulate_char_press("""
import ast
import inspect
import omni.kit.window.script_editor.tests.test_script_editor as editor

foo = lambda y: y
lines = inspect.getsource(foo)
r = ast.parse(lines)
editor.public_mailbox = r is not None
""")
        await ui_test.wait_n_updates(2)
        await ui_test.emulate_key_combo("CTRL+ENTER")
        self.assertEqual(public_mailbox, True)

    async def test_close_and_reopen_window(self):
        """Tests to make sure that content remains in the window even after it has been closed and reopened."""
        global public_mailbox

        await ui_test.emulate_char_press("import omni.kit.window.script_editor.tests.test_script_editor as editor\n")
        await ui_test.emulate_char_press("editor.public_mailbox = 'Hello World'\n")
        await ui_test.wait_n_updates(2)
        await ui_test.emulate_key_combo("CTRL+ENTER")
        self.assertEqual(public_mailbox, "Hello World")
        await ui_test.wait_n_updates(2)

        self.editor._widget.visible = False
        await ui_test.wait_n_updates(2)
        public_mailbox = ""

        self.editor._widget.visible = True
        await ui_test.wait_n_updates(2)
        await ui_test.emulate_key_combo("CTRL+ENTER")
        self.assertEqual(public_mailbox, "Hello World")
