import pytest
import tkinter as tk
from target_simulator.gui.target_list_frame import TargetListFrame


def test_target_list_frame_init():
    root = tk.Tk()
    frame = TargetListFrame(root)
    assert frame is not None
    root.destroy()


def test_get_targets_returns_cache():
    root = tk.Tk()
    frame = TargetListFrame(root)
    frame.targets_cache = ["dummy_target"]
    assert frame.get_targets() == ["dummy_target"]
    root.destroy()


def test_update_target_list_updates_cache():
    root = tk.Tk()
    frame = TargetListFrame(root)
    from target_simulator.core.models import Target

    t = Target(target_id=1)
    t.current_range_nm = 10.0
    t.current_azimuth_deg = 45.0
    t.current_velocity_fps = 100.0
    t.current_heading_deg = 90.0
    t.current_altitude_ft = 5000.0
    frame.update_target_list([t])
    assert frame.targets_cache == [t]
    root.destroy()


def test_targets_changed_callback_called():
    root = tk.Tk()
    called = {}

    def cb(targets):
        called["ok"] = True

    frame = TargetListFrame(root, targets_changed_callback=cb)
    frame.targets_cache = []
    from target_simulator.core.models import Target

    t = Target(target_id=2)
    t.current_range_nm = 20.0
    t.current_azimuth_deg = 90.0
    t.current_velocity_fps = 200.0
    t.current_heading_deg = 180.0
    t.current_altitude_ft = 10000.0
    frame.update_target_list([t])
    if frame.targets_changed_callback:
        frame.targets_changed_callback(frame.targets_cache)
    assert called.get("ok")
    root.destroy()


def test_on_remove_click_no_selection(monkeypatch):
    root = tk.Tk()
    frame = TargetListFrame(root)
    frame.tree.focus = lambda: ""
    called = {}

    def warn(title, msg, parent=None):
        called["warn"] = True

    monkeypatch.setattr("tkinter.messagebox.showwarning", warn)
    frame._on_remove_click()
    assert called.get("warn")
    root.destroy()
