# -*- coding: utf-8 -*-
# ///////////////////////////////////////////////////////////////
"""
Tests unitaires pour le widget OptionSelector.
"""

import pytest
from PySide6.QtCore import Qt
from ezqt_widgets.misc.option_selector import OptionSelector


pytestmark = pytest.mark.unit


class TestOptionSelector:
    """Test cases for OptionSelector widget."""

    def test_option_selector_creation_default(self, qt_widget_cleanup):
        """Test OptionSelector creation with default parameters."""
        items = ["Option 1", "Option 2", "Option 3"]
        selector = OptionSelector(items)

        assert selector.value == "Option 1"  # Default selection
        assert selector.value_id == 0
        assert selector.options == items
        assert selector.default_id == 0
        assert selector.orientation == "horizontal"
        assert selector.animation_duration == 300

    def test_option_selector_creation_custom(self, qt_widget_cleanup):
        """Test OptionSelector creation with custom parameters."""
        items = ["A", "B", "C", "D"]
        selector = OptionSelector(
            items=items, default_id=2, orientation="vertical", animation_duration=500
        )

        assert selector.value == "C"  # Index 2
        assert selector.value_id == 2
        assert selector.options == items
        assert selector.default_id == 2
        assert selector.orientation == "vertical"
        assert selector.animation_duration == 500

    def test_option_selector_add_option(self, qt_widget_cleanup):
        """Test adding options to the selector."""
        items = ["Option 1", "Option 2"]
        selector = OptionSelector(items)

        # Add a new option
        selector.add_option(2, "Option 3")

        assert len(selector.options) == 3
        assert "Option 3" in selector.options

    def test_option_selector_set_value_id(self, qt_widget_cleanup):
        """Test setting value by ID."""
        items = ["Option 1", "Option 2", "Option 3"]
        selector = OptionSelector(items)

        selector.value_id = 2
        assert selector.value == "Option 3"
        assert selector.value_id == 2

    def test_option_selector_set_value(self, qt_widget_cleanup):
        """Test setting value by text."""
        items = ["Option 1", "Option 2", "Option 3"]
        selector = OptionSelector(items)

        selector.value = "Option 2"
        assert selector.value == "Option 2"
        assert selector.value_id == 1

    def test_option_selector_signals(self, qt_widget_cleanup):
        """Test option selector signals."""
        items = ["Option 1", "Option 2"]
        selector = OptionSelector(items)

        clicked_called = False
        value_changed_called = False
        value_id_changed_called = False

        def on_clicked():
            nonlocal clicked_called
            clicked_called = True

        def on_value_changed(value):
            nonlocal value_changed_called
            value_changed_called = True

        def on_value_id_changed(value_id):
            nonlocal value_id_changed_called
            value_id_changed_called = True

        selector.clicked.connect(on_clicked)
        selector.valueChanged.connect(on_value_changed)
        selector.valueIdChanged.connect(on_value_id_changed)

        # Change value
        selector.value = "Option 2"
        assert value_changed_called
        assert value_id_changed_called

    def test_option_selector_properties(self, qt_widget_cleanup):
        """Test option selector properties."""
        items = ["Option 1", "Option 2"]
        selector = OptionSelector(items)

        # Test default_id setter
        selector.default_id = 1
        assert selector.default_id == 1

        # Test orientation setter
        selector.orientation = "vertical"
        assert selector.orientation == "vertical"

        # Test min_width setter
        selector.min_width = 200
        assert selector.min_width == 200

        # Test min_height setter
        selector.min_height = 100
        assert selector.min_height == 100

        # Test animation_duration setter
        selector.animation_duration = 400
        assert selector.animation_duration == 400

    def test_option_selector_toggle_selection(self, qt_widget_cleanup):
        """Test toggling selection."""
        items = ["Option 1", "Option 2", "Option 3"]
        selector = OptionSelector(items)

        # Toggle to option 2
        selector.toggle_selection(1)
        assert selector.value_id == 1
        assert selector.value == "Option 2"

    def test_option_selector_selected_option_property(self, qt_widget_cleanup):
        """Test getting the selected option widget."""
        items = ["Option 1", "Option 2"]
        selector = OptionSelector(items)

        selected_option = selector.selected_option
        assert selected_option is not None
        assert selected_option.text() == "Option 1"

    @pytest.mark.skip(reason="TypeError: 'str' object is not callable")
    def test_option_selector_size_hints(self, qt_widget_cleanup):
        """Test size hint methods."""
        items = ["Option 1", "Option 2"]
        selector = OptionSelector(items)

        size_hint = selector.sizeHint()
        assert size_hint.width() > 0
        assert size_hint.height() > 0

        min_size_hint = selector.minimumSizeHint()
        assert min_size_hint.width() > 0
        assert min_size_hint.height() > 0
