#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Widget Showcase - Qt Modern Style
=================================

A comprehensive showcase of all styled widgets.
"""

import sys

try:
    from PyQt5.QtWidgets import (
        QApplication,
        QMainWindow,
        QWidget,
        QVBoxLayout,
        QHBoxLayout,
        QGridLayout,
        QPushButton,
        QLabel,
        QLineEdit,
        QTextEdit,
        QPlainTextEdit,
        QCheckBox,
        QRadioButton,
        QComboBox,
        QSpinBox,
        QDoubleSpinBox,
        QGroupBox,
        QTabWidget,
        QSlider,
        QProgressBar,
        QListWidget,
        QTreeWidget,
        QTreeWidgetItem,
        QTableWidget,
        QTableWidgetItem,
        QDateEdit,
        QTimeEdit,
        QDateTimeEdit,
        QMenuBar,
        QMenu,
        QStatusBar,
        QToolBar,
        QToolButton,
        QSplitter,
        QScrollArea,
        QFrame,
    )
    from PyQt5.QtCore import Qt
    from PyQt5.QtGui import QAction
except ImportError:
    try:
        from PyQt6.QtWidgets import (
            QApplication,
            QMainWindow,
            QWidget,
            QVBoxLayout,
            QHBoxLayout,
            QGridLayout,
            QPushButton,
            QLabel,
            QLineEdit,
            QTextEdit,
            QPlainTextEdit,
            QCheckBox,
            QRadioButton,
            QComboBox,
            QSpinBox,
            QDoubleSpinBox,
            QGroupBox,
            QTabWidget,
            QSlider,
            QProgressBar,
            QListWidget,
            QTreeWidget,
            QTreeWidgetItem,
            QTableWidget,
            QTableWidgetItem,
            QDateEdit,
            QTimeEdit,
            QDateTimeEdit,
            QMenuBar,
            QMenu,
            QStatusBar,
            QToolBar,
            QToolButton,
            QSplitter,
            QScrollArea,
            QFrame,
        )
        from PyQt6.QtCore import Qt
        from PyQt6.QtGui import QAction
    except ImportError:
        try:
            from PySide6.QtWidgets import (
                QApplication,
                QMainWindow,
                QWidget,
                QVBoxLayout,
                QHBoxLayout,
                QGridLayout,
                QPushButton,
                QLabel,
                QLineEdit,
                QTextEdit,
                QPlainTextEdit,
                QCheckBox,
                QRadioButton,
                QComboBox,
                QSpinBox,
                QDoubleSpinBox,
                QGroupBox,
                QTabWidget,
                QSlider,
                QProgressBar,
                QListWidget,
                QTreeWidget,
                QTreeWidgetItem,
                QTableWidget,
                QTableWidgetItem,
                QDateEdit,
                QTimeEdit,
                QDateTimeEdit,
                QMenuBar,
                QMenu,
                QStatusBar,
                QToolBar,
                QToolButton,
                QSplitter,
                QScrollArea,
                QFrame,
            )
            from PySide6.QtCore import Qt
            from PySide6.QtGui import QAction
        except ImportError:
            from PySide2.QtWidgets import (
                QApplication,
                QMainWindow,
                QWidget,
                QVBoxLayout,
                QHBoxLayout,
                QGridLayout,
                QPushButton,
                QLabel,
                QLineEdit,
                QTextEdit,
                QPlainTextEdit,
                QCheckBox,
                QRadioButton,
                QComboBox,
                QSpinBox,
                QDoubleSpinBox,
                QGroupBox,
                QTabWidget,
                QSlider,
                QProgressBar,
                QListWidget,
                QTreeWidget,
                QTreeWidgetItem,
                QTableWidget,
                QTableWidgetItem,
                QDateEdit,
                QTimeEdit,
                QDateTimeEdit,
                QMenuBar,
                QMenu,
                QStatusBar,
                QToolBar,
                QToolButton,
                QSplitter,
                QScrollArea,
                QFrame,
            )
            from PySide2.QtCore import Qt
            from PySide2.QtGui import QAction

import qt_modern_style


class WidgetShowcase(QMainWindow):
    """Main window showcasing all styled widgets."""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Qt Modern Style - Widget Showcase")
        self.setMinimumSize(900, 700)

        self._setup_menubar()
        self._setup_toolbar()
        self._setup_statusbar()
        self._setup_central_widget()

    def _setup_menubar(self):
        """Setup the menu bar."""
        menubar = self.menuBar()

        # File menu
        file_menu = menubar.addMenu("&File")
        file_menu.addAction("New")
        file_menu.addAction("Open")
        file_menu.addAction("Save")
        file_menu.addSeparator()
        file_menu.addAction("Exit")

        # Edit menu
        edit_menu = menubar.addMenu("&Edit")
        edit_menu.addAction("Cut")
        edit_menu.addAction("Copy")
        edit_menu.addAction("Paste")

        # Help menu
        help_menu = menubar.addMenu("&Help")
        help_menu.addAction("About")

    def _setup_toolbar(self):
        """Setup the toolbar."""
        toolbar = QToolBar("Main Toolbar")
        self.addToolBar(toolbar)

        # Add tool buttons with icons
        new_btn = QToolButton()
        new_btn.setText("New")
        new_btn.setIcon(qt_modern_style.get_icon("new-file"))
        toolbar.addWidget(new_btn)

        open_btn = QToolButton()
        open_btn.setText("Open")
        open_btn.setIcon(qt_modern_style.get_icon("folder-opened"))
        toolbar.addWidget(open_btn)

        save_btn = QToolButton()
        save_btn.setText("Save")
        save_btn.setIcon(qt_modern_style.get_icon("save"))
        toolbar.addWidget(save_btn)

        toolbar.addSeparator()

        settings_btn = QToolButton()
        settings_btn.setText("Settings")
        settings_btn.setIcon(qt_modern_style.get_icon("settings"))
        toolbar.addWidget(settings_btn)

    def _setup_statusbar(self):
        """Setup the status bar."""
        self.statusBar().showMessage("Ready - Qt Modern Style Showcase")

    def _setup_central_widget(self):
        """Setup the central widget with tabs."""
        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        layout = QVBoxLayout(central_widget)
        layout.setContentsMargins(10, 10, 10, 10)

        # Create tab widget
        tabs = QTabWidget()
        tabs.addTab(self._create_buttons_tab(), "Buttons")
        tabs.addTab(self._create_inputs_tab(), "Inputs")
        tabs.addTab(self._create_selections_tab(), "Selections")
        tabs.addTab(self._create_views_tab(), "Views")
        tabs.addTab(self._create_misc_tab(), "Misc")

        layout.addWidget(tabs)

    def _create_buttons_tab(self):
        """Create the buttons showcase tab."""
        widget = QWidget()
        layout = QVBoxLayout(widget)
        layout.setSpacing(20)

        # Normal buttons
        normal_group = QGroupBox("Normal Buttons")
        normal_layout = QHBoxLayout(normal_group)

        btn1 = QPushButton("Normal")
        btn2 = QPushButton("Hover me")
        btn3 = QPushButton("Checkable")
        btn3.setCheckable(True)
        btn4 = QPushButton("Disabled")
        btn4.setEnabled(False)

        normal_layout.addWidget(btn1)
        normal_layout.addWidget(btn2)
        normal_layout.addWidget(btn3)
        normal_layout.addWidget(btn4)
        layout.addWidget(normal_group)

        # Primary buttons
        primary_group = QGroupBox("Primary Buttons")
        primary_layout = QHBoxLayout(primary_group)

        for text in ["Submit", "Confirm", "Apply", "Disabled"]:
            btn = QPushButton(text)
            btn.setProperty("primary", True)
            if text == "Disabled":
                btn.setEnabled(False)
            primary_layout.addWidget(btn)
        layout.addWidget(primary_group)

        # Danger buttons
        danger_group = QGroupBox("Danger Buttons")
        danger_layout = QHBoxLayout(danger_group)

        for text in ["Delete", "Remove", "Cancel", "Disabled"]:
            btn = QPushButton(text)
            btn.setProperty("danger", True)
            if text == "Disabled":
                btn.setEnabled(False)
            danger_layout.addWidget(btn)
        layout.addWidget(danger_group)

        # Buttons with icons
        icon_group = QGroupBox("Buttons with Icons")
        icon_layout = QHBoxLayout(icon_group)

        icons = [
            ("save", "Save"),
            ("copy", "Copy"),
            ("edit", "Edit"),
            ("trash", "Delete"),
        ]
        for icon_name, text in icons:
            btn = QPushButton(text)
            btn.setIcon(qt_modern_style.get_icon(icon_name))
            icon_layout.addWidget(btn)
        layout.addWidget(icon_group)

        layout.addStretch()
        return widget

    def _create_inputs_tab(self):
        """Create the inputs showcase tab."""
        widget = QWidget()
        layout = QVBoxLayout(widget)
        layout.setSpacing(20)

        # Line edits
        line_group = QGroupBox("Line Edits")
        line_layout = QGridLayout(line_group)

        line_layout.addWidget(QLabel("Normal:"), 0, 0)
        line1 = QLineEdit()
        line1.setPlaceholderText("Enter text...")
        line_layout.addWidget(line1, 0, 1)

        line_layout.addWidget(QLabel("With text:"), 1, 0)
        line2 = QLineEdit("Some content")
        line_layout.addWidget(line2, 1, 1)

        line_layout.addWidget(QLabel("Disabled:"), 2, 0)
        line3 = QLineEdit("Disabled")
        line3.setEnabled(False)
        line_layout.addWidget(line3, 2, 1)

        layout.addWidget(line_group)

        # Spinboxes and date/time
        spin_group = QGroupBox("Numeric & Date/Time")
        spin_layout = QGridLayout(spin_group)

        spin_layout.addWidget(QLabel("SpinBox:"), 0, 0)
        spin_layout.addWidget(QSpinBox(), 0, 1)

        spin_layout.addWidget(QLabel("DoubleSpinBox:"), 1, 0)
        spin_layout.addWidget(QDoubleSpinBox(), 1, 1)

        spin_layout.addWidget(QLabel("DateEdit:"), 2, 0)
        spin_layout.addWidget(QDateEdit(), 2, 1)

        spin_layout.addWidget(QLabel("TimeEdit:"), 3, 0)
        spin_layout.addWidget(QTimeEdit(), 3, 1)

        layout.addWidget(spin_group)

        # Text edits
        text_group = QGroupBox("Text Areas")
        text_layout = QHBoxLayout(text_group)

        text1 = QTextEdit()
        text1.setPlaceholderText("QTextEdit...")
        text1.setMaximumHeight(100)
        text_layout.addWidget(text1)

        text2 = QPlainTextEdit()
        text2.setPlaceholderText("QPlainTextEdit...")
        text2.setMaximumHeight(100)
        text_layout.addWidget(text2)

        layout.addWidget(text_group)

        layout.addStretch()
        return widget

    def _create_selections_tab(self):
        """Create the selections showcase tab."""
        widget = QWidget()
        layout = QVBoxLayout(widget)
        layout.setSpacing(20)

        # Checkboxes
        check_group = QGroupBox("Checkboxes")
        check_layout = QVBoxLayout(check_group)

        check1 = QCheckBox("Unchecked")
        check2 = QCheckBox("Checked")
        check2.setChecked(True)
        check3 = QCheckBox("Disabled unchecked")
        check3.setEnabled(False)
        check4 = QCheckBox("Disabled checked")
        check4.setChecked(True)
        check4.setEnabled(False)

        check_layout.addWidget(check1)
        check_layout.addWidget(check2)
        check_layout.addWidget(check3)
        check_layout.addWidget(check4)
        layout.addWidget(check_group)

        # Radio buttons
        radio_group = QGroupBox("Radio Buttons")
        radio_layout = QVBoxLayout(radio_group)

        radio1 = QRadioButton("Option 1")
        radio1.setChecked(True)
        radio2 = QRadioButton("Option 2")
        radio3 = QRadioButton("Option 3 (disabled)")
        radio3.setEnabled(False)

        radio_layout.addWidget(radio1)
        radio_layout.addWidget(radio2)
        radio_layout.addWidget(radio3)
        layout.addWidget(radio_group)

        # Combo boxes
        combo_group = QGroupBox("Combo Boxes")
        combo_layout = QGridLayout(combo_group)

        combo_layout.addWidget(QLabel("Normal:"), 0, 0)
        combo1 = QComboBox()
        combo1.addItems(["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"])
        combo_layout.addWidget(combo1, 0, 1)

        combo_layout.addWidget(QLabel("Disabled:"), 1, 0)
        combo2 = QComboBox()
        combo2.addItems(["Disabled"])
        combo2.setEnabled(False)
        combo_layout.addWidget(combo2, 1, 1)

        layout.addWidget(combo_group)

        layout.addStretch()
        return widget

    def _create_views_tab(self):
        """Create the views showcase tab."""
        widget = QWidget()
        layout = QHBoxLayout(widget)
        layout.setSpacing(10)

        # List widget
        list_group = QGroupBox("List View")
        list_layout = QVBoxLayout(list_group)
        list_widget = QListWidget()
        for i in range(1, 11):
            list_widget.addItem(f"List item {i}")
        list_layout.addWidget(list_widget)
        layout.addWidget(list_group)

        # Tree widget
        tree_group = QGroupBox("Tree View")
        tree_layout = QVBoxLayout(tree_group)
        tree_widget = QTreeWidget()
        tree_widget.setHeaderLabels(["Name", "Value"])

        root1 = QTreeWidgetItem(tree_widget, ["Root 1", ""])
        QTreeWidgetItem(root1, ["Child 1.1", "Value A"])
        QTreeWidgetItem(root1, ["Child 1.2", "Value B"])

        root2 = QTreeWidgetItem(tree_widget, ["Root 2", ""])
        QTreeWidgetItem(root2, ["Child 2.1", "Value C"])

        tree_widget.expandAll()
        tree_layout.addWidget(tree_widget)
        layout.addWidget(tree_group)

        # Table widget
        table_group = QGroupBox("Table View")
        table_layout = QVBoxLayout(table_group)
        table_widget = QTableWidget(5, 3)
        table_widget.setHorizontalHeaderLabels(["Column 1", "Column 2", "Column 3"])

        for row in range(5):
            for col in range(3):
                table_widget.setItem(
                    row, col, QTableWidgetItem(f"Cell {row+1},{col+1}")
                )

        table_layout.addWidget(table_widget)
        layout.addWidget(table_group)

        return widget

    def _create_misc_tab(self):
        """Create the miscellaneous widgets tab."""
        widget = QWidget()
        layout = QVBoxLayout(widget)
        layout.setSpacing(20)

        # Sliders
        slider_group = QGroupBox("Sliders")
        slider_layout = QVBoxLayout(slider_group)

        h_slider = QSlider(
            Qt.Orientation.Horizontal if hasattr(Qt, "Orientation") else Qt.Horizontal
        )
        h_slider.setRange(0, 100)
        h_slider.setValue(50)
        slider_layout.addWidget(h_slider)

        layout.addWidget(slider_group)

        # Progress bars
        progress_group = QGroupBox("Progress Bars")
        progress_layout = QVBoxLayout(progress_group)

        progress1 = QProgressBar()
        progress1.setValue(25)
        progress_layout.addWidget(progress1)

        progress2 = QProgressBar()
        progress2.setValue(75)
        progress_layout.addWidget(progress2)

        progress3 = QProgressBar()
        progress3.setValue(100)
        progress_layout.addWidget(progress3)

        layout.addWidget(progress_group)

        # Labels
        label_group = QGroupBox("Labels")
        label_layout = QVBoxLayout(label_group)

        normal_label = QLabel("Normal label")
        label_layout.addWidget(normal_label)

        hint_label = QLabel("Hint label (secondary text)")
        hint_label.setProperty("hint", True)
        label_layout.addWidget(hint_label)

        muted_label = QLabel("Muted label (disabled appearance)")
        muted_label.setProperty("muted", True)
        label_layout.addWidget(muted_label)

        layout.addWidget(label_group)

        layout.addStretch()
        return widget


def main():
    """Main entry point."""
    app = QApplication(sys.argv)

    # Apply the modern style
    qt_modern_style.apply(app)

    # Create and show window
    window = WidgetShowcase()
    window.show()

    sys.exit(app.exec() if hasattr(app, "exec") else app.exec_())


if __name__ == "__main__":
    main()
