import json
import unittest
from pathlib import Path


LAYOUTS = Path(__file__).resolve().parents[1] / "layouts"


def load_layout(layout_id):
    return json.loads((LAYOUTS / f"{layout_id}.json").read_text(encoding="utf-8"))


class PlatformLayoutTests(unittest.TestCase):
    def test_cupertino_uses_mac_like_dock_and_desktop_items(self):
        layout = load_layout("cupertino")
        dock = layout["dash-to-dock@micxgx.gmail.com"]
        desktop = layout["gtk4-ding@smedius.gitlab.com"]

        self.assertEqual(dock.get("show-trash"), True)
        self.assertEqual(desktop.get("show-home"), False)

    def test_redmond_desktop_icons_match_windows_defaults(self):
        layout_ids = [
            "redmond_2007",
            "redmond_2009",
            "redmond_2015",
            "redmond_2021_centered",
            "redmond_2021_left",
        ]

        for layout_id in layout_ids:
            with self.subTest(layout=layout_id):
                desktop = load_layout(layout_id)["gtk4-ding@smedius.gitlab.com"]
                self.assertEqual(desktop.get("show-trash"), True)
                expected_home = layout_id in {"redmond_2015", "redmond_2021_centered"}
                self.assertEqual(desktop.get("show-home"), expected_home)
                self.assertEqual(desktop.get("start-corner"), "top-left")

    def test_legacy_redmond_uses_single_monitor_taskbar_and_vista_show_desktop(self):
        for layout_id in ["redmond_2007", "redmond_2009"]:
            with self.subTest(layout=layout_id):
                layout = load_layout(layout_id)
                panel = layout["dash-to-panel@jderose9.github.com"]
                menu = layout["arcmenu@arcmenu.com"]
                self.assertEqual(panel.get("multi-monitors"), False)
                self.assertEqual(menu.get("multi-monitor"), False)

        vista_panel = load_layout("redmond_2007")["dash-to-panel@jderose9.github.com"]
        desktop_button = next(
            element
            for element in vista_panel["panel-element-positions"]
            if element["element"] == "desktopButton"
        )
        self.assertEqual(desktop_button["visible"], False)

    def test_centered_windows_11_menu_fits_a_1280_by_800_vm(self):
        menu = load_layout("redmond_2021_centered")["arcmenu@arcmenu.com"]
        self.assertEqual(menu["force-menu-location"], "BottomCentered")
        self.assertLessEqual(menu["menu-height"], 600)

    def test_centered_windows_11_orders_start_activities_and_apps_in_one_group(self):
        panel = load_layout("redmond_2021_centered")["dash-to-panel@jderose9.github.com"]
        elements = panel["panel-element-positions"]
        positions = {
            element["element"]: element["position"]
            for element in elements
        }
        order = [element["element"] for element in elements]

        for element in ["leftBox", "activitiesButton", "taskbar", "centerBox"]:
            self.assertEqual(positions[element], "centerMonitor")
        self.assertEqual(
            order[order.index("leftBox"):order.index("centerBox") + 1],
            ["leftBox", "activitiesButton", "taskbar", "centerBox"],
        )

    def test_dock_layouts_allow_the_session_background_color(self):
        for layout_id in ["cupertino", "elementary", "ubuntu_2024"]:
            with self.subTest(layout=layout_id):
                dock = load_layout(layout_id)["dash-to-dock@micxgx.gmail.com"]
                self.assertEqual(dock["apply-custom-theme"], False)


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