"""
 @file
 @brief This file contains a custom title bar used by dock widgets
 @author Jonathan Thomas <jonathan@openshot.org>

 @section LICENSE

 Copyright (c) 2008-2024 OpenShot Studios, LLC
 (http://www.openshotstudios.com). This file is part of
 OpenShot Video Editor (http://www.openshot.org), an open-source project
 dedicated to delivering high quality video editing and animation solutions
 to the world.

 OpenShot Video Editor is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 OpenShot Video Editor is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with OpenShot Library.  If not, see <http://www.gnu.org/licenses/>.
 """

from PyQt5.QtWidgets import QWidget, QHBoxLayout, QPushButton, QLabel

from classes.app import get_app


class HiddenTitleBar(QWidget):
    def __init__(self, dock_widget, title_text=""):
        super().__init__()
        self.dock_widget = dock_widget
        self._tr = None
        self.dragging = False  # Flag for dragging
        self.start_pos = None

        # Set up a horizontal layout
        layout = QHBoxLayout(self)

        # Add a QLabel for the title (optional, based on title_text)
        self.title_label = QLabel(title_text)
        if title_text:
            self.title_label.setObjectName("dock-title-label")
        else:
            self.title_label.setObjectName("dock-title-handle")

        # Add the QLabel to the layout
        layout.addWidget(self.title_label)

        # Keep title in sync with dock widget
        self.dock_widget.windowTitleChanged.connect(self.update_title)

        # Add a spacer to push buttons to the right
        layout.addStretch()

        # Add close and undock buttons
        self.close_button = QPushButton()
        self.undock_button = QPushButton()

        # Set object names for styling via stylesheets
        self.close_button.setObjectName("dock-close-button")
        self.undock_button.setObjectName("dock-float-button")

        # Connect the buttons to the appropriate actions
        self.close_button.clicked.connect(self.dock_widget.close)
        self.undock_button.clicked.connect(self.toggle_dock_state)

        # Add buttons to the layout
        layout.addWidget(self.undock_button)
        layout.addWidget(self.close_button)

        # Set margins and reduce height for the title bar
        layout.setContentsMargins(0, 0, 0, 0)
        self.setFixedHeight(20)  # Reduced height
        self._update_accessible_labels()

    def update_title(self, text):
        """Update label text when dock title changes."""
        self.title_label.setText(text)

    def _update_accessible_labels(self):
        if self._tr is None:
            self._tr = get_app()._tr
        _ = self._tr
        close_label = _("Close")
        self.close_button.setAccessibleName(close_label)

        if self.dock_widget.isFloating():
            float_label = _("Dock")
        else:
            float_label = _("Float")
        self.undock_button.setAccessibleName(float_label)

    def toggle_dock_state(self):
        """Toggle between docked and floating states."""
        if self.dock_widget.isFloating():
            # Restore the default title bar when docking
            self.dock_widget.setFloating(False)
        else:
            # Float the widget and apply custom title bar
            self.dock_widget.setFloating(True)
        self._update_accessible_labels()
