"test dialog"

from dialog.crop import Crop
import gi

gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, GLib, Gtk  # pylint: disable=wrong-import-position
from loop_helpers import safe_mainloop


def test_1():
    "test dialog"
    dialog = Crop(transient_for=Gtk.Window(), page_width=100, page_height=100)
    assert isinstance(dialog, Crop), "Created dialog"
    assert dialog.page_width == 100, "default page-width"
    assert dialog.page_height == 100, "default page-height"

    flag = False
    mlp = safe_mainloop(2000)

    def on_changed_selection(*_):
        nonlocal flag
        flag = True
        mlp.quit()

    dialog.connect("changed-selection", on_changed_selection)
    selection = Gdk.Rectangle()
    selection.x, selection.y, selection.width, selection.height = 10, 10, 10, 10
    dialog.selection = selection
    mlp.run()
    assert flag, "updating selection emits signal"
    assert dialog._sb_x.get_value() == 10, "updating selection changes spinbutton"


def test_coverage():
    "test coverage for edge cases"
    dialog = Crop(transient_for=Gtk.Window(), page_width=100, page_height=100)

    # Line 72: page_width.setter early return
    dialog.page_width = 100
    dialog.page_width = 100

    # Line 92: page_height.setter early return
    dialog.page_height = 100
    dialog.page_height = 100

    # Line 50: selection.setter early return
    rect = Gdk.Rectangle()
    rect.x, rect.y, rect.width, rect.height = 1, 1, 1, 1
    dialog.selection = rect
    dialog.selection = rect

    # Line 142: on_sb_selector_value_changed if self.selection is None
    dialog._selection = None
    # This should trigger on_sb_selector_value_changed which sets self.selection
    # We call it directly to hit line 142
    dialog.on_sb_selector_value_changed(dialog._sb_x, "x")
    assert dialog.selection is not None


def test_crop_selector_with_existing_selection():
    "test on_sb_selector_value_changed when selection already exists"
    dialog = Crop(transient_for=Gtk.Window(), page_width=100, page_height=100)

    # Set an initial selection
    rect = Gdk.Rectangle()
    rect.x, rect.y, rect.width, rect.height = 10, 20, 30, 40
    dialog.selection = rect

    # Now change a spinbutton value — should copy existing selection attributes
    dialog._sb_x.set_value(50)
    dialog.on_sb_selector_value_changed(dialog._sb_x, "x")
    assert dialog.selection.x == 50
    assert dialog.selection.y == 20
    assert dialog.selection.width == 30
    assert dialog.selection.height == 40
