"""
Unit tests for the About Dialog module
Tests AboutDialog class functionality
"""

import sys
from unittest.mock import MagicMock, patch


# Test AboutDialog class
class TestAboutDialog:
    """Tests for the AboutDialog class"""

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_init(self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init):
        """Test AboutDialog initialization"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle") as mock_title:
            with patch.object(AboutDialog, "setMinimumWidth") as mock_width:
                with patch.object(AboutDialog, "setMinimumHeight") as mock_height:
                    with patch.object(AboutDialog, "setLayout"):
                        _dialog = AboutDialog()  # noqa: F841

                        mock_title.assert_called_once_with("About Argus Overview")
                        mock_width.assert_called_once_with(500)
                        mock_height.assert_called_once_with(400)

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_setup_ui_creates_labels(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test that _setup_ui creates the expected labels"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        _dialog = AboutDialog()  # noqa: F841

                        # Labels should be created (title, version, platform, etc.)
                        assert mock_label.call_count >= 5

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_setup_ui_creates_buttons(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test that _setup_ui creates buttons"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        _dialog = AboutDialog()  # noqa: F841

                        # Should create at least 2 buttons (Coffee, Close)
                        assert mock_button.call_count >= 2

    @patch("argus_overview.ui.about_dialog.QDesktopServices")
    @patch("argus_overview.ui.about_dialog.QUrl")
    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_open_donation_link(
        self,
        mock_font,
        mock_hbox,
        mock_button,
        mock_label,
        mock_vbox,
        mock_dialog_init,
        mock_qurl,
        mock_desktop,
    ):
        """Test that _open_donation_link opens correct URL"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        dialog = AboutDialog()

                        dialog._open_donation_link()

                        mock_qurl.assert_called_once_with("https://buymeacoffee.com/aretedriver")
                        mock_desktop.openUrl.assert_called_once()


# Test platform detection
class TestPlatformDetection:
    """Tests for platform detection in AboutDialog"""

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_platform_linux(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test platform detection on Linux"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        with patch.object(sys, "platform", "linux"):
            from argus_overview.ui.about_dialog import AboutDialog

            with patch.object(AboutDialog, "setWindowTitle"):
                with patch.object(AboutDialog, "setMinimumWidth"):
                    with patch.object(AboutDialog, "setMinimumHeight"):
                        with patch.object(AboutDialog, "setLayout"):
                            _dialog = AboutDialog()  # noqa: F841

                            # Check that Label was called with Linux
                            label_calls = [str(call) for call in mock_label.call_args_list]
                            any("Linux" in str(call) for call in label_calls)
                            # May or may not detect Linux depending on import timing
                            assert mock_label.called

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_platform_windows(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test platform detection on Windows"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        # Note: Can't easily change sys.platform after import
        # This test just verifies the dialog can be created
        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        dialog = AboutDialog()
                        assert dialog is not None


# Test dialog behavior
class TestDialogBehavior:
    """Tests for dialog behavior"""

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_close_button_connects_to_accept(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test that close button connects to accept"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        mock_close_btn = MagicMock()
        mock_coffee_btn = MagicMock()

        # Return different mock buttons for different calls
        mock_button.side_effect = [mock_coffee_btn, mock_close_btn]

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        with patch.object(AboutDialog, "accept"):
                            AboutDialog()

                            # Close button should have clicked.connect called
                            mock_close_btn.clicked.connect.assert_called()

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_coffee_button_connects_to_donation(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test that coffee button connects to donation link handler"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        mock_coffee_btn = MagicMock()
        mock_close_btn = MagicMock()

        mock_button.side_effect = [mock_coffee_btn, mock_close_btn]

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        AboutDialog()

                        # Coffee button should have clicked.connect called
                        mock_coffee_btn.clicked.connect.assert_called()


# Test content verification
class TestDialogContent:
    """Tests for dialog content"""

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_title_label_content(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test that title label has correct text"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        AboutDialog()

                        # First QLabel call should be for title
                        calls = mock_label.call_args_list
                        title_call = calls[0]
                        assert "Argus Overview" in str(title_call)

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_version_label_content(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test that version label has version info"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        AboutDialog()

                        calls = mock_label.call_args_list
                        # Check that one of the labels contains version
                        label_texts = [str(call) for call in calls]
                        has_version = any("Version" in text for text in label_texts)
                        assert has_version

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_github_link_content(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test that GitHub link is included"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        AboutDialog()

                        calls = mock_label.call_args_list
                        label_texts = [str(call) for call in calls]
                        has_github = any("github.com" in text.lower() for text in label_texts)
                        assert has_github


# Test edge cases
class TestEdgeCases:
    """Tests for edge cases"""

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_init_with_parent(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test initialization with parent widget"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        from argus_overview.ui.about_dialog import AboutDialog

        mock_parent = MagicMock()

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        AboutDialog(parent=mock_parent)

                        # Should pass parent to QDialog.__init__
                        mock_dialog_init.assert_called_once_with(mock_parent)

    @patch("argus_overview.ui.about_dialog.QDialog.__init__")
    @patch("argus_overview.ui.about_dialog.QVBoxLayout")
    @patch("argus_overview.ui.about_dialog.QLabel")
    @patch("argus_overview.ui.about_dialog.QPushButton")
    @patch("argus_overview.ui.about_dialog.QHBoxLayout")
    @patch("argus_overview.ui.about_dialog.QFont")
    def test_init_without_parent(
        self, mock_font, mock_hbox, mock_button, mock_label, mock_vbox, mock_dialog_init
    ):
        """Test initialization without parent widget"""
        mock_dialog_init.return_value = None
        mock_vbox_instance = MagicMock()
        mock_vbox.return_value = mock_vbox_instance

        from argus_overview.ui.about_dialog import AboutDialog

        with patch.object(AboutDialog, "setWindowTitle"):
            with patch.object(AboutDialog, "setMinimumWidth"):
                with patch.object(AboutDialog, "setMinimumHeight"):
                    with patch.object(AboutDialog, "setLayout"):
                        AboutDialog()

                        # Should pass None to QDialog.__init__
                        mock_dialog_init.assert_called_once_with(None)
