import types
import tkinter as tk

from target_simulator.gui.connection_settings_window import (
    ConnectionSettingsWindow,
)
from target_simulator.gui.main_view import MainView


class DummyConfigManager:
    def __init__(self):
        self.saved = None

    def save_connection_settings(self, cfg):
        self.saved = cfg

    def get_connection_settings(self):
        return {}

    def get_scenario_names(self):
        return []

    def get_general_settings(self):
        return {}

    def get_scenario(self, name):
        return None


def test_connection_settings_load_and_save(monkeypatch):
    # Build a dummy master with minimal geometry methods
    class Master:
        def __init__(self):
            self.config_manager = None

        def winfo_rootx(self):
            return 0

        def winfo_rooty(self):
            return 0

        def winfo_width(self):
            return 100

        def winfo_height(self):
            return 100

        def update_idletasks(self):
            return None

        def bind(self, *a, **k):
            return None

    master = Master()

    cfg = {"target": {"type": "tftp", "tftp": {"ip": "10.0.0.1", "port": 69}}, "lru": {}}
    cm = DummyConfigManager()

    # Provide a master_view with update_connection_settings and geometry helpers
    class MasterView:
        def __init__(self):
            self._last_cfg = None

        def update_connection_settings(self, c):
            self._last_cfg = c

        def winfo_rootx(self):
            return 0

        def winfo_rooty(self):
            return 0

        def winfo_width(self):
            return 200

        def winfo_height(self):
            return 200

        def update_idletasks(self):
            return None

    master_view = MasterView()
    win = ConnectionSettingsWindow(master_view, cm, cfg)
    # Simulate changing a field
    win.target_vars["tftp_ip"].set("192.168.0.5")
    win._on_save()
    # ConnectionSettingsWindow delegates saving to master_view.update_connection_settings
    assert getattr(master_view, "_last_cfg", None) is not None
    assert master_view._last_cfg["target"]["tftp"]["ip"] == "192.168.0.5"


def test_main_view_update_connection_settings(monkeypatch):
    # monkeypatch ConfigManager inside MainView to avoid file IO
    fake_cm = types.SimpleNamespace(
        get_general_settings=lambda: {},
        get_connection_settings=lambda: {},
        get_scenario_names=lambda: [],
        get_scenario=lambda name: None,
    )
    # add save_connection_settings stub
    def _save(cfg):
        fake_cm.saved = cfg

    fake_cm.save_connection_settings = _save
    monkeypatch.setattr("target_simulator.gui.main_view.ConfigManager", lambda: fake_cm)
    mv = MainView()
    new_cfg = {"target": {"type": "tftp"}, "lru": {"type": "tftp"}}
    # call update_connection_settings should set connection_config and call _initialize_communicators
    mv.update_connection_settings(new_cfg)
    assert mv.connection_config == new_cfg
import pytest
import tkinter as tk
from target_simulator.gui.connection_settings_window import ConnectionSettingsWindow


def test_connection_settings_window_init():
    root = tk.Tk()
    win = ConnectionSettingsWindow(root, config_manager=None, connection_config={})
    assert win is not None
