# target_simulator/gui/radar_config_window.py

"""
Configuration window for radar settings.
"""
import tkinter as tk
from tkinter import ttk, messagebox


class RadarConfigWindow(tk.Toplevel):
    """A Toplevel window for configuring radar parameters."""

    def __init__(self, parent, current_scan_limit: int, current_max_range: int):
        super().__init__(parent)
        self.title("Radar Configuration")
        self.geometry("300x200")
        self.transient(parent)
        self.grab_set()

        self.protocol("WM_DELETE_WINDOW", self._on_cancel)
        self.scan_limit = None
        self.max_range = None

        # Store current values passed from MainView
        self.current_scan_limit = current_scan_limit
        self.current_max_range = current_max_range

        self._create_widgets()

        self.wait_window(self)

    def _create_widgets(self):
        """Create and layout the widgets in the window."""
        main_frame = ttk.Frame(self, padding="10")
        main_frame.pack(fill=tk.BOTH, expand=True)

        # Scan Sector Limit
        ttk.Label(main_frame, text="Scan Sector Limit (°): ±").grid(
            row=0, column=0, sticky=tk.W, pady=5
        )
        # Use the passed-in value to initialize the StringVar
        self.scan_limit_var = tk.StringVar(value=str(self.current_scan_limit))
        scan_limit_entry = ttk.Entry(
            main_frame, textvariable=self.scan_limit_var, width=10
        )
        scan_limit_entry.grid(row=0, column=1, sticky=tk.E, pady=5)

        # Max Range
        ttk.Label(main_frame, text="Max Range (NM):").grid(
            row=1, column=0, sticky=tk.W, pady=5
        )
        # Use the passed-in value to initialize the StringVar
        self.max_range_var = tk.StringVar(value=str(self.current_max_range))
        max_range_entry = ttk.Entry(
            main_frame, textvariable=self.max_range_var, width=10
        )
        max_range_entry.grid(row=1, column=1, sticky=tk.E, pady=5)

        # Button Frame
        button_frame = ttk.Frame(main_frame)
        button_frame.grid(row=2, column=0, columnspan=2, pady=(20, 0))

        ok_button = ttk.Button(button_frame, text="OK", command=self._on_ok)
        ok_button.pack(side=tk.LEFT, padx=5)

        cancel_button = ttk.Button(button_frame, text="Cancel", command=self._on_cancel)
        cancel_button.pack(side=tk.LEFT, padx=5)

    def _on_ok(self):
        """Handle OK button click."""
        try:
            scan_val = int(self.scan_limit_var.get())
            range_val = int(self.max_range_var.get())

            if not (0 < scan_val <= 180):
                messagebox.showerror(
                    "Invalid Value",
                    "Scan Sector Limit must be between 1 and 180.",
                    parent=self,
                )
                return
            if not (range_val > 0):
                messagebox.showerror(
                    "Invalid Value", "Max Range must be a positive number.", parent=self
                )
                return

            self.scan_limit = scan_val
            self.max_range = range_val
            self.destroy()
        except ValueError:
            messagebox.showerror(
                "Invalid Input",
                "Please enter valid numbers for both fields.",
                parent=self,
            )

    def _on_cancel(self):
        """Handle Cancel button click or window close."""
        self.scan_limit = None
        self.max_range = None
        self.destroy()
