from PySide6.QtWidgets import QDialog, QVBoxLayout, QTabWidget, QTextEdit, QDialogButtonBox
from PySide6.QtCore import Qt


class HelpModal(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Montaris-X User Guide")
        self.resize(650, 500)

        layout = QVBoxLayout(self)
        tabs = QTabWidget()
        layout.addWidget(tabs)

        # Getting Started
        tabs.addTab(self._text_tab(
            "Welcome to Montaris-X\n\n"
            "Montaris-X is a native ROI editor for scientific images.\n\n"
            "Quick Start:\n"
            "1. File > Open Image(s) to load a TIFF/PNG/JPG\n"
            "2. Click '+' in the Layers panel to add ROI layers\n"
            "3. Select a drawing tool (Brush, Polygon, etc.)\n"
            "4. Draw on the canvas to create ROI masks\n"
            "5. File > Save ROI Set to save your work\n\n"
            "Navigation:\n"
            "- Scroll wheel to zoom\n"
            "- Middle-click or Space+drag to pan\n"
            "- Ctrl+0 to fit to window\n"
            "- Ctrl+1 for 1:1 zoom"
        ), "Getting Started")

        # Tools
        from montaris.tools import TOOL_REGISTRY
        tools_text = ""
        for name, (_, _, shortcut, category) in TOOL_REGISTRY.items():
            tools_text += f"  [{shortcut}] {name} ({category})\n"
        tools_text += "\nBrush Size: [ / ] keys or slider\n"
        tools_text += "Polygon: Click to add vertices, Enter/double-click to close\n"
        tools_text += "Transform: Click ROI to show handles, drag to scale/rotate\n"
        tools_text += "Move: Click+drag to move ROI (component-aware)\n"
        tabs.addTab(self._text_tab(tools_text), "Tools")

        # Keyboard Shortcuts
        shortcuts_text = (
            "File:\n"
            "  Ctrl+O           Open Image(s)\n"
            "  Ctrl+Shift+O     Load ROI Set\n"
            "  Ctrl+S           Save ROI Set\n"
            "  Ctrl+Shift+S     Save Progress (Session)\n"
            "  Ctrl+E           Export ROI(s) as PNG\n"
            "  Ctrl+W           Close Image(s)\n"
            "  Ctrl+Q           Quit\n\n"
            "Edit:\n"
            "  Ctrl+Z           Undo\n"
            "  Ctrl+Y / Ctrl+Shift+Z   Redo\n"
            "  Ctrl+Alt+Z       Layer Undo\n"
            "  Ctrl+Alt+Y       Layer Redo\n"
            "  Ctrl+A           Select All ROIs\n"
            "  Delete           Delete Active/Selected ROI(s)\n\n"
            "View:\n"
            "  Ctrl+0           Fit to Window\n"
            "  Ctrl+1           Reset Zoom (1:1)\n"
            "  Ctrl+=           Zoom In\n"
            "  Ctrl+-           Zoom Out\n"
            "  Ctrl+H           Flip Horizontal\n"
            "  Ctrl+R           Rotate 90 CW\n"
            "  Ctrl+[           Collapse Left Sidebar\n"
            "  Ctrl+]           Collapse Right Sidebar\n"
            "  F11              Fullscreen\n"
            "  Ctrl+Shift+P     Screenshot\n\n"
            "Tools:\n"
            "  H                Hand (pan)\n"
            "  Q                Select\n"
            "  B / Ctrl+B       Brush\n"
            "  E                Eraser\n"
            "  P                Polygon\n"
            "  G / Ctrl+U       Bucket Fill\n"
            "  R                Rectangle\n"
            "  C                Circle\n"
            "  S                Stamp\n"
            "  T                Transform (selected)\n"
            "  Shift+T          Transform All\n"
            "  V                Move (selected)\n"
            "  Shift+V          Move All\n"
            "  [ / ]            Adjust brush size\n"
            "  Space            Pan (hold)\n"
            "  Enter            Finish Polygon\n"
            "  Escape           Cancel / clear selection\n\n"
            "Selection:\n"
            "  Ctrl+Click       Toggle ROI selection\n\n"
            "Help:\n"
            "  F1               User Guide\n"
        )
        tabs.addTab(self._text_tab(shortcuts_text), "Keyboard Shortcuts")

        # File Formats
        formats_text = (
            "Images:\n"
            "  TIFF (.tif, .tiff) — including 16/32-bit scientific\n"
            "  PNG (.png)\n"
            "  JPEG (.jpg, .jpeg)\n"
            "  BMP (.bmp)\n\n"
            "ROI Formats:\n"
            "  NumPy Archive (.npz) — native Montaris format\n"
            "  ImageJ ROI (.roi) — binary format\n"
            "  PNG Masks (.png) — binary masks as images\n"
            "  ZIP Archive (.zip) — containing .roi or .png files\n\n"
            "Instructions:\n"
            "  JSON (.json) — batch operation scripts\n"
            "  Text (.txt) — human-readable instructions\n"
        )
        tabs.addTab(self._text_tab(formats_text), "File Formats")

        # About
        about_text = (
            "Montaris-X\n"
            "Native ROI Editor for Scientific Images\n\n"
            "Developer: Digin Dominic\n"
            "License: MIT License\n"
            "Repository: https://github.com/digin1/Montaris-X\n\n"
            "---\n\n"
            "MIT License\n\n"
            "Copyright (c) 2026 Digin Dominic and Montaris-X Contributors\n\n"
            "Permission is hereby granted, free of charge, to any person obtaining a copy "
            "of this software and associated documentation files (the \"Software\"), to deal "
            "in the Software without restriction, including without limitation the rights "
            "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell "
            "copies of the Software, and to permit persons to whom the Software is "
            "furnished to do so, subject to the following conditions:\n\n"
            "The above copyright notice and this permission notice shall be included in all "
            "copies or substantial portions of the Software.\n\n"
            "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR "
            "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, "
            "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
            "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER "
            "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, "
            "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE "
            "SOFTWARE."
        )
        tabs.addTab(self._text_tab(about_text), "About")

        bb = QDialogButtonBox(QDialogButtonBox.Close)
        bb.rejected.connect(self.reject)
        layout.addWidget(bb)

    def _text_tab(self, text):
        te = QTextEdit()
        te.setReadOnly(True)
        te.setPlainText(text)
        return te
