# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk
from pymsc.utils.gui_helpers import ScrollableFrame
from pymsc.gui.components.widget_factory import create_widget_by_id

class MissionPage(ScrollableFrame):
    """
    Navigation and Mission data page (SNU, Cursor, Antenna, Timetags).
    """
    def __init__(self, parent, controller):
        super().__init__(parent, controller)
        self.grid_columnconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)
        self._build_page()

    def _build_page(self):
        # LEFT COLUMN (Cursor and Attitude)
        # 1. Cursor Position (Labels)
        self.f1 = ttk.LabelFrame(self.scrollable_content, text="Cursor Position")
        self.f1.grid(row=0, column=0, padx=10, pady=5, sticky="nsew")
        for c in ["crs_x/y", "crs_range/azimuth", "crs_lat/lon"]:
            create_widget_by_id(self.f1, c).pack(fill=tk.X, padx=5, pady=2)

        # 2. Attitude (SNU)
        self.f2 = ttk.LabelFrame(self.scrollable_content, text="Attitude (SNU)")
        self.f2.grid(row=1, column=0, padx=10, pady=5, sticky="nsew")
        atts = [
            "attitude", "true_heading", "magnetic_heading", 
            "platform_azimuth", "yaw_snu", "pitch_snu", "roll_snu"
        ]
        for a in atts:
            create_widget_by_id(self.f2, a).pack(fill=tk.X, padx=5, pady=2)

        # 3. Timetags
        self.f3 = ttk.LabelFrame(self.scrollable_content, text="Timetags")
        self.f3.grid(row=2, column=0, padx=10, pady=5, sticky="nsew")
        times = ["timetag_inu", "timetag_ptaz", "timetag_pitch", "timetag_roll", "timetag_ppos"]
        for t in times:
            create_widget_by_id(self.f3, t).pack(fill=tk.X, padx=5, pady=2)

        # RIGHT COLUMN (Antenna and Velocities)
        # 4. Altitude
        self.f4 = ttk.LabelFrame(self.scrollable_content, text="Altitude")
        self.f4.grid(row=0, column=1, padx=10, pady=5, sticky="nsew")
        alts = ["alt_corrected", "alt_barometric", "alt_radio"]
        for al in alts:
            create_widget_by_id(self.f4, al).pack(fill=tk.X, padx=5, pady=2)

        # 5. Velocities
        self.f5 = ttk.LabelFrame(self.scrollable_content, text="Velocities")
        self.f5.grid(row=1, column=1, padx=10, pady=5, sticky="nsew")
        vels = ["tas", "cas", "nav_data_invalid", "vx/vy/vz", "accx/accy/accz", "nx/ny/nz", "wind"]
        for v in vels:
            create_widget_by_id(self.f5, v).pack(fill=tk.X, padx=5, pady=2)

        # 6. Mission & Antenna
        self.f6 = ttk.LabelFrame(self.scrollable_content, text="Mission & Antenna")
        self.f6.grid(row=2, column=1, padx=10, pady=5, sticky="nsew")
        mis = [
            "antenna_tb", "antenna", "cursor_rates", "cursor_slave", 
            "a_c_lat_lon", "spoi_lat_lot", "spoi_altitude", "clearance_plane"
        ]
        for m in mis:
            create_widget_by_id(self.f6, m).pack(fill=tk.X, padx=5, pady=2)