#!/usr/bin/python3
"""Tests for slpkg.dialog_box.DialogBox (when python-dialog is unavailable)."""

from pathlib import Path
from unittest.mock import MagicMock

from slpkg.dialog_box import DialogBox


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def make_dialog_box(*, dialog_available: bool = False) -> DialogBox:
    """Construct DialogBox bypassing __init__ (avoids config_load / Dialog())."""
    obj: DialogBox = object.__new__(DialogBox)
    obj.prog_name = "slpkg"
    # Simulate dialog unavailable (d=None) or available (d=MagicMock)
    obj.d = MagicMock() if dialog_available else None
    return obj


# ---------------------------------------------------------------------------
# checklist — dialog unavailable
# ---------------------------------------------------------------------------


def test_checklist_no_dialog_returns_empty_tuple() -> None:
    obj = make_dialog_box(dialog_available=False)
    code, tags = obj.checklist("text", "title", 10, 70, 0, [])
    assert code == ""
    assert tags == []


# ---------------------------------------------------------------------------
# checklist — dialog available
# ---------------------------------------------------------------------------


def test_checklist_delegates_to_dialog(capsys) -> None:
    obj = make_dialog_box(dialog_available=True)
    obj.d.checklist.return_value = ("ok", ["ffmpeg"])
    code, tags = obj.checklist("text", "title", 10, 70, 0, [("ffmpeg", "6.1", True, "help")])
    assert code == "ok"
    assert "ffmpeg" in tags
    obj.d.checklist.assert_called_once()


# ---------------------------------------------------------------------------
# mixedform — dialog unavailable
# ---------------------------------------------------------------------------


def test_mixedform_no_dialog_returns_empty_tuple() -> None:
    obj = make_dialog_box(dialog_available=False)
    code, tags = obj.mixedform("text", "title", [], 0, 0, 0)
    assert code == ""
    assert tags == []


# ---------------------------------------------------------------------------
# mixedform — dialog available
# ---------------------------------------------------------------------------


def test_mixedform_delegates_to_dialog() -> None:
    obj = make_dialog_box(dialog_available=True)
    obj.d.mixedform.return_value = ("ok", ["value1"])
    code, tags = obj.mixedform("text", "title", [("key", 1, 1, "val", 1, 10, 40, 200, "0x0", "help")], 0, 0, 0)
    assert code == "ok"
    assert tags == ["value1"]
    obj.d.mixedform.assert_called_once()


# ---------------------------------------------------------------------------
# msgbox — dialog unavailable
# ---------------------------------------------------------------------------


def test_msgbox_no_dialog_prints_fallback(capsys) -> None:
    obj = make_dialog_box(dialog_available=False)
    obj.msgbox("Something failed", 10, 50)
    out = capsys.readouterr().out
    assert "Something failed" in out


def test_msgbox_no_dialog_returns_none() -> None:
    obj = make_dialog_box(dialog_available=False)
    result = obj.msgbox("msg", 5, 40)
    assert result is None


# ---------------------------------------------------------------------------
# msgbox — dialog available
# ---------------------------------------------------------------------------


def test_msgbox_delegates_to_dialog() -> None:
    obj = make_dialog_box(dialog_available=True)
    obj.msgbox("msg", 5, 40)
    obj.d.msgbox.assert_called_once_with("msg", 5, 40)


# ---------------------------------------------------------------------------
# textbox — dialog unavailable
# ---------------------------------------------------------------------------


def test_textbox_no_dialog_prints_fallback(capsys) -> None:
    obj = make_dialog_box(dialog_available=False)
    obj.textbox("/etc/slpkg/slpkg.toml", 40, 60)
    out = capsys.readouterr().out
    assert "slpkg.toml" in out


def test_textbox_no_dialog_accepts_path(capsys) -> None:
    obj = make_dialog_box(dialog_available=False)
    obj.textbox(Path("/etc/slpkg/slpkg.toml"), 40, 60)
    out = capsys.readouterr().out
    assert "slpkg.toml" in out


# ---------------------------------------------------------------------------
# textbox — dialog available
# ---------------------------------------------------------------------------


def test_textbox_delegates_to_dialog() -> None:
    obj = make_dialog_box(dialog_available=True)
    obj.textbox("/etc/slpkg/slpkg.toml", 40, 60)
    obj.d.textbox.assert_called_once_with("/etc/slpkg/slpkg.toml", 40, 60)
