from qtpy.QtWidgets import QWidget

from bec_widgets.applications.views.developer_view.developer_widget import DeveloperWidget
from bec_widgets.applications.views.view import ViewBase, ViewTourSteps


class DeveloperView(ViewBase):
    """
    A view for users to write scripts and macros and execute them within the application.
    """

    def __init__(
        self,
        parent: QWidget | None = None,
        content: QWidget | None = None,
        *,
        view_id: str | None = None,
        title: str | None = None,
        **kwargs,
    ):
        super().__init__(parent=parent, content=content, view_id=view_id, title=title, **kwargs)
        self.developer_widget = DeveloperWidget(parent=self)
        self.set_content(self.developer_widget)

    def register_tour_steps(self, guided_tour, main_app) -> ViewTourSteps | None:
        """Register Developer View components with the guided tour.

        Args:
            guided_tour: The GuidedTour instance to register with.
            main_app: The main application instance (for accessing set_current).

        Returns:
            ViewTourSteps | None: Model containing view title and step IDs.
        """
        step_ids = []
        dev_widget = self.developer_widget

        # IDE Toolbar
        def get_ide_toolbar():
            main_app.set_current("developer_view")
            return (dev_widget.toolbar, None)

        step_id = guided_tour.register_widget(
            widget=get_ide_toolbar,
            title="IDE Toolbar",
            text="Quick access to save files, execute scripts, and configure IDE settings. Use the toolbar to manage your code and execution.",
        )
        step_ids.append(step_id)

        # IDE Explorer
        def get_ide_explorer():
            main_app.set_current("developer_view")
            return (dev_widget.explorer_dock.widget(), None)

        step_id = guided_tour.register_widget(
            widget=get_ide_explorer,
            title="File Explorer",
            text="Browse and manage your macro files. Create new files, open existing ones, and organize your scripts.",
        )
        step_ids.append(step_id)

        # IDE Editor
        def get_ide_editor():
            main_app.set_current("developer_view")
            return (dev_widget.monaco_dock.widget(), None)

        step_id = guided_tour.register_widget(
            widget=get_ide_editor,
            title="Code Editor",
            text="Write and edit Python code with syntax highlighting, auto-completion, and signature help. Monaco editor provides a modern coding experience.",
        )
        step_ids.append(step_id)

        # IDE Console
        def get_ide_console():
            main_app.set_current("developer_view")
            return (dev_widget.console_dock.widget(), None)

        step_id = guided_tour.register_widget(
            widget=get_ide_console,
            title="BEC Shell Console",
            text="Interactive Python console with BEC integration. Execute commands, test code snippets, and interact with the BEC system in real-time.",
        )
        step_ids.append(step_id)

        # IDE Plotting Area
        def get_ide_plotting():
            main_app.set_current("developer_view")
            return (dev_widget.plotting_ads, None)

        step_id = guided_tour.register_widget(
            widget=get_ide_plotting,
            title="Plotting Area",
            text="View plots and visualizations generated by your scripts. Arrange multiple plots in a flexible layout.",
        )
        step_ids.append(step_id)

        return ViewTourSteps(view_title="Developer View", step_ids=step_ids)


if __name__ == "__main__":
    import sys

    from bec_qthemes import apply_theme
    from qtpy.QtWidgets import QApplication

    from bec_widgets.applications.main_app import BECMainApp

    app = QApplication(sys.argv)
    apply_theme("dark")

    _app = BECMainApp()
    screen = app.primaryScreen()
    screen_geometry = screen.availableGeometry()
    screen_width = screen_geometry.width()
    screen_height = screen_geometry.height()
    # 70% of screen height, keep 16:9 ratio
    height = int(screen_height * 0.9)
    width = int(height * (16 / 9))

    # If width exceeds screen width, scale down
    if width > screen_width * 0.9:
        width = int(screen_width * 0.9)
        height = int(width / (16 / 9))

    _app.resize(width, height)
    developer_view = DeveloperView()
    _app.add_view(
        icon="code_blocks",
        title="IDE",
        widget=developer_view,
        view_id="developer_view",
        exclusive=True,
    )
    _app.show()
    # developer_view.show()
    # developer_view.setWindowTitle("Developer View")
    # developer_view.resize(1920, 1080)
    # developer_view.set_stretch(horizontal=[1, 3, 2], vertical=[5, 5]) #can be set during runtime
    sys.exit(app.exec_())
