import asyncio
import carb
import os
import tempfile
import omni.kit.test
import omni.usd
import omni.client

from omni.kit.usd.layers import get_layers, LayerEditMode, get_layer_event_payload, LayerEventType


class TestLayersInterface(omni.kit.test.AsyncTestCase):
    async def setUp(self):
        self.usd_context = omni.usd.get_context()
        await omni.usd.get_context().new_stage_async()

    async def tearDown(self):
        if omni.usd.get_context().get_stage():
            await omni.usd.get_context().close_stage_async()

    async def test_edit_mode_switch(self):
        layers = get_layers()
        event_stream = layers.get_event_stream()

        payload = None

        def _on_layer_event(event: carb.events.IEvent):
            nonlocal payload
            temp = get_layer_event_payload(event)
            if temp.event_type == LayerEventType.EDIT_MODE_CHANGED:
                payload = temp

        subscription = event_stream.create_subscription_to_pop(_on_layer_event, name="Layers Interface Tests")

        self.assertEqual(layers.get_edit_mode(), LayerEditMode.NORMAL)

        def change_edit_mode_and_verify(edit_mode):
            nonlocal payload
            payload = None
            layers.set_edit_mode(edit_mode)
            self.assertIsNotNone(payload)
            self.assertEqual(payload.event_type, LayerEventType.EDIT_MODE_CHANGED)
            self.assertEqual(layers.get_edit_mode(), edit_mode)

        change_edit_mode_and_verify(LayerEditMode.AUTO_AUTHORING)
        change_edit_mode_and_verify(LayerEditMode.SPECS_LINKING)
        change_edit_mode_and_verify(LayerEditMode.NORMAL)
