# 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 carb.audio
import omni.kit.audio.oscilloscope
import omni.kit.ui
import omni.ui
import threading
import time
import re
import asyncio


class OscilloscopeWindowExtension(omni.ext.IExt):
    """Audio Recorder Window Extension"""

    def _menu_callback(self, a, b):
        self._window.visible = not self._window.visible

    def _read_callback(self, event):
        img = self._oscilloscope.get_image();
        self._waveform_image_provider.set_bytes_data(img, [self._waveform_width, self._waveform_height])

    def _record_clicked(self):
        if self._recording:
            self._record_button.set_style({"image_url": "resources/glyphs/audio_record.svg"})
            self._recording = False;
            self._oscilloscope.stop()
        else:
            self._record_button.set_style({"image_url": "resources/glyphs/timeline_stop.svg"})
            self._recording = True
            self._oscilloscope.start()


    def on_startup(self):
        self._timer = None
        self._waveform_width = 512;
        self._waveform_height = 460;
        try:
            self._oscilloscope = omni.kit.audio.oscilloscope.create_oscilloscope(self._waveform_width, self._waveform_height);
        except Exception as e:
            return;
        self._window = omni.ui.Window("Audio Oscilloscope", width=512, height=540)
        self._recording = False
        with self._window.frame:
            with omni.ui.VStack(height=0, spacing=8):
                # waveform
                with omni.ui.HStack(height=460):
                    omni.ui.Spacer()
                    self._waveform_image_provider = omni.ui.ByteImageProvider()
                    self._waveform_image = omni.ui.ImageWithProvider(
                        self._waveform_image_provider,
                        width=omni.ui.Percent(100),
                        height=omni.ui.Percent(100),
                        fill_policy=omni.ui.IwpFillPolicy.IWP_STRETCH,
                    )
                    omni.ui.Spacer()

                # buttons
                with omni.ui.HStack():
                    omni.ui.Spacer()

                    self._record_button = omni.ui.Button(
                        width=32,
                        height=32,
                        clicked_fn=self._record_clicked,
                        style={"image_url": "resources/glyphs/audio_record.svg"},
                    )

                    omni.ui.Spacer()

        self._sub = self._oscilloscope.get_event_stream().create_subscription_to_pop(self._read_callback)
        self._menuEntry = omni.kit.ui.get_editor_menu().add_item("Window/Oscilloscope", self._menu_callback)
        self._window.visible = False

    def on_shutdown(self):  # pragma: no cover
        self._sub = None
        self._recorder = None
        self._window = None
        self._menuEntry = None
        self._oscilloscope = None
        menu = omni.kit.ui.get_editor_menu()
        if menu is not None:
            menu.remove_item("Window/Oscilloscope")
