import tkinter as tk
import unittest

from gui.app import ProcessMonitorApp
from gui.widgets import ReadOnlyDetailsDialog


class TestGuiResize(unittest.TestCase):
    def setUp(self):
        try:
            self.app = ProcessMonitorApp()
        except tk.TclError as exc:
            self.skipTest(f"Tk display unavailable: {exc}")
        self.app.deiconify()
        self.app.update_idletasks()

    def tearDown(self):
        if hasattr(self, "app") and self.app.winfo_exists():
            self.app._close()

    def test_root_is_freely_resizable_above_safe_minimum(self):
        self.assertEqual((True, True), tuple(bool(int(value)) for value in self.app.resizable()))
        self.assertEqual((1080, 680), tuple(self.app.minsize()))
        observed = []
        for geometry in ("1080x680", "1280x720", "1480x900", "1920x1080", "2200x1200"):
            self.app.geometry(geometry); self.app.update()
            observed.append((self.app.winfo_width(), self.app.winfo_height()))
            self.assertGreater(self.app.content.winfo_width(), 0)
            self.assertGreater(self.app.content.winfo_height(), 0)
        self.assertEqual(sorted(observed), observed)
        self.assertGreater(observed[-1][0], observed[0][0])
        self.assertGreater(observed[-1][1], observed[0][1])

    def test_every_page_remains_navigable_after_live_resizes(self):
        for geometry in ("1080x680", "1920x1080", "1280x720"):
            self.app.geometry(geometry); self.app.update()
            for name, page in self.app.pages.items():
                self.app.show_page(name); self.app.update_idletasks()
                self.assertEqual("grid", page.winfo_manager())
                self.assertEqual("SelectedNav.TButton", self.app.nav_buttons[name].cget("style"))

    def test_information_heavy_details_dialogs_are_resizable_and_read_only(self):
        for title in ("Process details", "Alert details"):
            dialog = ReadOnlyDetailsDialog(self.app, title, "Command line: example\nMatched rules:\n- TEST")
            dialog.update_idletasks()
            self.assertEqual((True, True), tuple(bool(int(value)) for value in dialog.resizable()))
            self.assertEqual("disabled", str(dialog.text.cget("state")))
            dialog.geometry("900x700"); dialog.update_idletasks()
            self.assertTrue(dialog.geometry().startswith("900x700"))
            dialog.destroy()


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