import pytest
from unittest.mock import MagicMock
from src import theme_2 as theme
from src import paths as paths_mod


def test_theme_apply_sets_rounding_and_padding(monkeypatch):
    mock_style = MagicMock()
    mock_imgui = MagicMock()
    mock_imgui.get_style.return_value = mock_style
    mock_imgui.ImVec2.side_effect = lambda x, y: (x, y)
    monkeypatch.setattr(theme, "imgui", mock_imgui)

    theme.apply("ImGui Dark")

    assert mock_style.window_rounding == 6.0
    assert mock_style.child_rounding == 4.0
    assert mock_style.frame_rounding == 4.0
    assert mock_style.popup_rounding == 4.0
    assert mock_style.scrollbar_rounding == 12.0
    assert mock_style.grab_rounding == 4.0
    assert mock_style.tab_rounding == 4.0

    assert mock_style.window_border_size == 1.0
    assert mock_style.frame_border_size == 1.0
    assert mock_style.popup_border_size == 1.0

    assert mock_style.window_padding == (8.0, 8.0)
    assert mock_style.frame_padding == (8.0, 4.0)
    assert mock_style.item_spacing == (8.0, 4.0)
    assert mock_style.item_inner_spacing == (4.0, 4.0)
    assert mock_style.scrollbar_size == 14.0


def test_themes_load_from_toml(tmp_path, monkeypatch):
    from src import paths as paths_mod
    themes_dir = tmp_path / "themes"
    themes_dir.mkdir()
    (themes_dir / "solarized_dark.toml").write_text(
        'syntax_palette = "dark"\n'
        '[colors]\n'
        'window_bg = [0, 43, 54]\n'
        'text = [147, 161, 161]\n'
    )
    (themes_dir / "broken.toml").write_text('syntax_palette = "dark"\n')

    monkeypatch.setattr(theme, "get_global_themes_path", lambda: themes_dir)
    theme.load_themes_from_disk()
    names = theme.get_palette_names()
    assert "solarized_dark" in names
    assert "broken" not in names


def test_get_syntax_palette_for_theme(tmp_path, monkeypatch):
    from src import paths as paths_mod
    themes_dir = tmp_path / "themes"
    themes_dir.mkdir()
    (themes_dir / "solarized_light.toml").write_text(
        'syntax_palette = "light"\n[colors]\nwindow_bg = [253, 246, 227]\n'
    )
    from unittest.mock import MagicMock
    mock_style = MagicMock()
    mock_imgui = MagicMock()
    mock_imgui.get_style.return_value = mock_style
    mock_imgui.ImVec2.side_effect = lambda x, y: (x, y)
    monkeypatch.setattr(theme, "imgui", mock_imgui)
    monkeypatch.setattr(theme, "get_global_themes_path", lambda: themes_dir)
    theme.load_themes_from_disk()
    assert theme.get_syntax_palette_for_theme("solarized_light") == "light"
    assert theme.get_syntax_palette_for_theme("ImGui Dark") == "dark"


def test_get_syntax_palette_for_unknown_theme_returns_default(tmp_path, monkeypatch):
    from src import paths as paths_mod
    themes_dir = tmp_path / "themes"
    themes_dir.mkdir()
    monkeypatch.setattr(theme, "get_global_themes_path", lambda: themes_dir)
    theme.load_themes_from_disk()
    assert theme.get_syntax_palette_for_theme("NonExistent") == "dark"


def test_all_four_new_themes_registered(tmp_path, monkeypatch):
    from src import paths as paths_mod

    themes_dir = tmp_path / "themes"
    themes_dir.mkdir()
    for name in ["solarized_dark", "solarized_light", "gruvbox_dark", "moss"]:
        (themes_dir / f"{name}.toml").write_text(
            'syntax_palette = "dark"\n[colors]\nwindow_bg = [0, 0, 0]\n'
        )
    monkeypatch.setattr(theme, "get_global_themes_path", lambda: themes_dir)
    theme.load_themes_from_disk()
    names = theme.get_palette_names()
    for name in ["solarized_dark", "solarized_light", "gruvbox_dark", "moss"]:
        assert name in names, f"missing theme: {name}"


def test_solarized_light_uses_light_syntax_palette(tmp_path, monkeypatch):
    from src import paths as paths_mod

    themes_dir = tmp_path / "themes"
    themes_dir.mkdir()
    (themes_dir / "solarized_light.toml").write_text(
        'syntax_palette = "light"\n[colors]\nwindow_bg = [253, 246, 227]\n'
    )
    from unittest.mock import MagicMock
    mock_style = MagicMock()
    mock_imgui = MagicMock()
    mock_imgui.get_style.return_value = mock_style
    mock_imgui.ImVec2.side_effect = lambda x, y: (x, y)
    monkeypatch.setattr(theme, "imgui", mock_imgui)
    monkeypatch.setattr(theme, "get_global_themes_path", lambda: themes_dir)
    theme.load_themes_from_disk()
    assert theme.get_syntax_palette_for_theme("solarized_light") == "light"


def test_solarized_dark_apply_does_not_raise(tmp_path, monkeypatch):
    from src import paths as paths_mod

    themes_dir = tmp_path / "themes"
    themes_dir.mkdir()
    (themes_dir / "solarized_dark.toml").write_text(
        'syntax_palette = "dark"\n[colors]\nwindow_bg = [0, 43, 54]\n'
    )
    from unittest.mock import MagicMock
    mock_style = MagicMock()
    mock_imgui = MagicMock()
    mock_imgui.get_style.return_value = mock_style
    mock_imgui.ImVec2.side_effect = lambda x, y: (x, y)
    monkeypatch.setattr(theme, "imgui", mock_imgui)
    monkeypatch.setattr(theme, "get_global_themes_path", lambda: themes_dir)
    theme.load_themes_from_disk()
    try:
        theme.apply("solarized_dark")
    except Exception as e:
        pytest.fail(f"apply(solarized_dark) raised: {e}")
    assert theme.get_current_palette() == "solarized_dark"


def test_gruvbox_dark_apply_does_not_raise(tmp_path, monkeypatch):
    from src import paths as paths_mod

    themes_dir = tmp_path / "themes"
    themes_dir.mkdir()
    (themes_dir / "gruvbox_dark.toml").write_text(
        'syntax_palette = "retro_blue"\n[colors]\nwindow_bg = [40, 40, 40]\n'
    )
    from unittest.mock import MagicMock
    mock_style = MagicMock()
    mock_imgui = MagicMock()
    mock_imgui.get_style.return_value = mock_style
    mock_imgui.ImVec2.side_effect = lambda x, y: (x, y)
    monkeypatch.setattr(theme, "imgui", mock_imgui)
    monkeypatch.setattr(theme, "get_global_themes_path", lambda: themes_dir)
    theme.load_themes_from_disk()
    theme.apply("gruvbox_dark")
    assert theme.get_current_palette() == "gruvbox_dark"


def test_moss_apply_does_not_raise(tmp_path, monkeypatch):
    from src import paths as paths_mod

    themes_dir = tmp_path / "themes"
    themes_dir.mkdir()
    (themes_dir / "moss.toml").write_text(
        'syntax_palette = "mariana"\n[colors]\nwindow_bg = [40, 47, 49]\n'
    )
    from unittest.mock import MagicMock
    mock_style = MagicMock()
    mock_imgui = MagicMock()
    mock_imgui.get_style.return_value = mock_style
    mock_imgui.ImVec2.side_effect = lambda x, y: (x, y)
    monkeypatch.setattr(theme, "imgui", mock_imgui)
    monkeypatch.setattr(theme, "get_global_themes_path", lambda: themes_dir)
    theme.load_themes_from_disk()
    theme.apply("moss")
    assert theme.get_current_palette() == "moss"


def test_solarized_light_apply_does_not_raise(tmp_path, monkeypatch):
    from src import paths as paths_mod

    themes_dir = tmp_path / "themes"
    themes_dir.mkdir()
    (themes_dir / "solarized_light.toml").write_text(
        'syntax_palette = "light"\n[colors]\nwindow_bg = [253, 246, 227]\n'
    )
    from unittest.mock import MagicMock
    mock_style = MagicMock()
    mock_imgui = MagicMock()
    mock_imgui.get_style.return_value = mock_style
    mock_imgui.ImVec2.side_effect = lambda x, y: (x, y)
    monkeypatch.setattr(theme, "imgui", mock_imgui)
    monkeypatch.setattr(theme, "get_global_themes_path", lambda: themes_dir)
    theme.load_themes_from_disk()
    theme.apply("solarized_light")
    assert theme.get_current_palette() == "solarized_light"
