# ui.py
"""
This module handles the creation and layout of the Tkinter user interface.
It defines the widgets and their arrangement within the main window.
"""

import tkinter as tk
from tkinter import ttk
import config  # Import the config module

class ControlPanel(ttk.Frame):
    """
    A class to represent the control panel containing SAR parameters and information.
    """
    def __init__(self, parent, app, *args, **kwargs):
        """
        Initializes the ControlPanel.

        Args:
            parent (tk.Widget): The parent widget.
            app (App): The main application instance.
        """
        super().__init__(parent, *args, **kwargs)
        self.app = app
        self.init_ui()

    def init_ui(self):
        """
        Initializes the user interface elements within the control panel.
        """
        self.pack(side=tk.TOP, fill=tk.X)

        # --- SAR Parameters Frame ---
        self.sar_params_frame = ttk.Labelframe(self, text="SAR Parameters", padding=10)
        self.sar_params_frame.pack(side=tk.LEFT, padx=10, fill=tk.X)

        # --- SAR Info Frame ---
        self.sar_info_frame = ttk.Labelframe(self, text="SAR Info", padding=10)
        self.sar_info_frame.pack(side=tk.TOP, padx=10, fill=tk.X)

        # --- Layout all the parameter widgets in a grid ---
        row = 0

        # --- Test Image Checkbox ---
        self.test_image_var = tk.IntVar(value=0)  # Initial value: Static image
        self.test_image_check = ttk.Checkbutton(self.sar_params_frame, text="Test Image",
                                                 variable=self.test_image_var, command=self.app.update_image_mode)
        self.test_image_check.grid(row=row, column=0, columnspan=2, sticky=tk.W, padx=5, pady=2)
        row += 1

        # --- SAR Size Control ---
        self.sar_size_label = ttk.Label(self.sar_params_frame, text="SAR Size:")
        self.sar_size_label.grid(row=row, column=0, sticky=tk.W, padx=5, pady=2)

        self.sar_size_combo = ttk.Combobox(self.sar_params_frame, values=config.SAR_SIZE_FACTORS, state="readonly",
                                            width=5)
        self.sar_size_combo.set(config.DEFAULT_SAR_SIZE)  # Initial value (1/2 of the original size)
        self.sar_size_combo.grid(row=row, column=1, sticky=tk.E, padx=5, pady=2)
        self.sar_size_combo.bind("<<ComboboxSelected>>", self.app.update_sar_size)
        row += 1

        # --- Contrast Control ---
        self.contrast_label = ttk.Label(self.sar_params_frame, text="Contrast:")
        self.contrast_label.grid(row=row, column=0, sticky=tk.W, padx=5, pady=2)
        self.contrast_scale = ttk.Scale(self.sar_params_frame, orient=tk.HORIZONTAL, length=200,
                                         from_=0.1, to=3.0, command=self.app.update_contrast, value=config.DEFAULT_SAR_CONTRAST)
        self.contrast_scale.grid(row=row, column=1, sticky=tk.E, padx=5, pady=2)
        row += 1

        # --- Brightness Control ---
        self.brightness_label = ttk.Label(self.sar_params_frame, text="Brightness:")
        self.brightness_label.grid(row=row, column=0, sticky=tk.W, padx=5, pady=2)
        self.brightness_scale = ttk.Scale(self.sar_params_frame, orient=tk.HORIZONTAL, length=200,
                                           from_=-100, to=100, command=self.app.update_brightness, value=config.DEFAULT_SAR_BRIGHTNESS)
        self.brightness_scale.grid(row=row, column=1, sticky=tk.E, padx=5, pady=2)
        row += 1

        # --- Color Palette Control ---
        self.palette_label = ttk.Label(self.sar_params_frame, text="Palette:")
        self.palette_label.grid(row=row, column=0, sticky=tk.W, padx=5, pady=2)
        self.palette_combo = ttk.Combobox(self.sar_params_frame, values=config.COLOR_PALETTES, state="readonly", width=8)
        self.palette_combo.set(config.DEFAULT_SAR_PALETTE)  # Initial value
        self.palette_combo.grid(row=row, column=1, sticky=tk.E, padx=5, pady=2)
        self.palette_combo.bind("<<ComboboxSelected>>", self.app.update_sar_palette)
        row += 1

        # --- SAR Center Lat/Lon Label ---
        self.sar_center_label = ttk.Label(self.sar_info_frame,
                                           text=f"Center: Lat={config.SAR_CENTER_LAT:.4f}, Lon={config.SAR_CENTER_LON:.4f}")
        self.sar_center_label.pack(side=tk.TOP, anchor=tk.W)

        # --- Mouse Lat/Lon Label ---
        self.mouse_latlon_label = ttk.Label(self.sar_info_frame, text="Mouse: Lat=N/A, Lon=N/A")
        self.mouse_latlon_label.pack(side=tk.TOP, anchor=tk.W)

    def set_mouse_coordinates(self, latitude, longitude):
        """
        Updates the mouse coordinates label.

        Args:
            latitude (float): The latitude of the mouse position.
            longitude (float): The longitude of the mouse position.
        """
        self.mouse_latlon_label.config(text=f"Mouse: Lat={latitude:.4f}, Lon={longitude:.4f}")

class StatusBar(ttk.Label):
    """
    A class to represent the status bar at the bottom of the main window.
    """
    def __init__(self, parent, *args, **kwargs):
        """
        Initializes the StatusBar.

        Args:
            parent (tk.Widget): The parent widget.
        """
        super().__init__(parent, *args, **kwargs)
        self.init_ui()

    def init_ui(self):
        """
        Initializes the status bar with the initial message and appearance.
        """
        self.config(text=config.INITIAL_STATUS_MESSAGE, relief=tk.SUNKEN, anchor=tk.W)
        self.pack(side=tk.BOTTOM, fill=tk.X)

    def set_status_text(self, text):
        """
        Sets the text of the status bar.

        Args:
            text (str): The text to display in the status bar.
        """
        self.config(text=text)


def create_main_window(title, min_width, min_height, x_pos, y_pos):
    """
    Creates the main Tkinter window.

    Args:
        title (str): The title of the window.
        min_width (int): The minimum width of the window.
        min_height (int): The minimum height of the window.
        x_pos (int): The x-coordinate of the window's position.
        y_pos (int): The y-coordinate of the window's position.

    Returns:
        tk.Tk: The main Tkinter window.
    """
    root = tk.Tk()
    root.title(title)
    root.minsize(min_width, min_height)
    root.geometry(f"+{x_pos}+{y_pos}")
    return root