import re
import unittest
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
OVERRIDE = ROOT / "10_stillos.gschema.override"
TOP_BAR_COLOR = "#222226"


def section_values(section_name):
    text = OVERRIDE.read_text(encoding="utf-8")
    match = re.search(
        rf"^\[{re.escape(section_name)}\]\n(?P<body>.*?)(?=^\[|\Z)",
        text,
        flags=re.MULTILINE | re.DOTALL,
    )
    if not match:
        return {}

    values = {}
    for line in match.group("body").splitlines():
        line = line.strip()
        if not line or line.startswith("#"):
            continue
        key, value = line.split("=", 1)
        values[key.strip()] = value.strip().strip("'\"")
    return values


class ThemeDefaultsTests(unittest.TestCase):
    def test_dash_to_dock_matches_panel_and_top_bar_color(self):
        panel = section_values("org.gnome.shell.extensions.dash-to-panel:stillos")
        dock = section_values("org.gnome.shell.extensions.dash-to-dock:stillos")

        self.assertEqual(panel["trans-bg-color"], TOP_BAR_COLOR)
        self.assertIn("background-color", dock)
        self.assertEqual(dock["background-color"], panel["trans-bg-color"])
        self.assertEqual(dock["custom-background-color"], "true")
        self.assertIn("apply-custom-theme", dock)
        self.assertEqual(dock["apply-custom-theme"], "false")

    def test_dash_to_dock_matches_panel_opacity(self):
        panel = section_values("org.gnome.shell.extensions.dash-to-panel:stillos")
        dock = section_values("org.gnome.shell.extensions.dash-to-dock:stillos")

        self.assertEqual(panel["trans-panel-opacity"], "1.0")
        self.assertIn("background-opacity", dock)
        self.assertEqual(dock["background-opacity"], panel["trans-panel-opacity"])


if __name__ == "__main__":
    unittest.main()
