"""EF Execution Debugging Page"""

import omni.ui as ui
from omni.kit.exec.core.unstable import dump_graph_topology
from omni.kit.window.preferences import PreferenceBuilder, SettingType


class ExecutionDebugPage(PreferenceBuilder):
    """EF Execution Debugging Page"""

    def __init__(self):
        super().__init__("Execution")

        # Setting keys. When changing, make sure to update ExecutionController.cpp as well.
        self.serial_eg = "/app/execution/debug/forceSerial"
        self.parallel_eg = "/app/execution/debug/forceParallel"

        # ID counter for file path write-out.
        self.id = 0
        self.file_name = "ExecutionGraph_"

    def build(self):
        """Build the EF execution debugging page"""
        with ui.VStack(height=0):
            with self.add_frame("Execution Debugging"):
                with ui.VStack():
                    dump_graph_topology_button = ui.Button("Dump execution graph", width=24)
                    dump_graph_topology_button.set_clicked_fn(self._dump_graph_topology)
                    dump_graph_topology_button.set_tooltip(
                        "Writes the execution graph topology out as a GraphViz (.dot) file. File location\n"
                        + "will differ depending on the kit app being run, whether the button was pressed\n"
                        + "in a unit test, etc., but typically it can be found under something like\n"
                        + "kit/_compiler/omni.app.dev; if not, then just search for .dot files containing\n"
                        + "the name ExecutionGraph in the kit directory.\n"
                    )
                    self.create_setting_widget("Force serial execution", self.serial_eg, SettingType.BOOL)
                    self.create_setting_widget("Force parallel execution", self.parallel_eg, SettingType.BOOL)

    def _dump_graph_topology(self):
        """Dump graph topology with incremented file"""
        dump_graph_topology(self.file_name + str(self.id))
        self.id += 1
