import tkinter as tk
import types

from target_simulator.gui.trajectory_editor_window import TrajectoryEditorWindow
from target_simulator.core.models import Waypoint, ManeuverType, Target


class DummyMaster:
    def __init__(self):
        self.config_manager = None
    
    def bind(self, *a, **k):
        return None


def make_waypoint_fly_to(range_nm=10.0, az=0.0, alt=0.0):
    wp = Waypoint()
    wp.maneuver_type = ManeuverType.FLY_TO_POINT
    wp.target_range_nm = range_nm
    wp.target_azimuth_deg = az
    wp.target_altitude_ft = alt
    return wp


def test_spline_toggle_requires_four_waypoints(monkeypatch):
    master = DummyMaster()
    # create editor without blocking on wait_window by monkeypatching wait_window
    monkeypatch.setattr(tk.Toplevel, 'wait_window', lambda self, w: None)
    # instantiate with empty existing ids and a small range
    ed = TrajectoryEditorWindow(master, existing_ids=[], max_range_nm=50)
    # ensure waypoints < 4
    ed.waypoints = [make_waypoint_fly_to() for _ in range(3)]
    ed.use_spline_var.set(True)
    # call toggle which should disable spline if <4
    ed._on_spline_toggle()
    assert ed.use_spline_var.get() is False


def test_get_total_simulation_time_calls_model():
    master = DummyMaster()
    # monkeypatch wait_window again
    tk.Toplevel.wait_window = lambda self, w: None
    ed = TrajectoryEditorWindow(master, existing_ids=[], max_range_nm=100)
    # set some waypoints and check total time calculation
    ed.waypoints = [make_waypoint_fly_to() for _ in range(4)]
    # calling internal method should not raise
    t = ed._get_total_simulation_time()
    assert isinstance(t, float)
