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

class ControlPage(ScrollableFrame):
    """
    Primary control page for Radar Modes, Functions and Parameters.
    """
    def __init__(self, parent, controller):
        super().__init__(parent, controller)
        self.grid_columnconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure(2, weight=1)
        self._build_page()

    def _build_page(self):
        # --- COLUMN 0: Modal Commands ---
        self.frame_cmds = ttk.LabelFrame(self.scrollable_content, text="Modal Commands")
        self.frame_cmds.grid(row=0, column=0, padx=10, pady=10, sticky="nsew")
        
        commands = [
            "stand_by", "silence", "mode", "rws_submode", 
            "acm_submode", "gm_submode", "des_ctrl", "sar", 
            "freeze", "ibit", "ale_blanking", "deg_perf", 
            "last_acquisition_fail", "transition", "emergency"
        ]
        for cmd in commands:
            w = create_widget_by_id(self.frame_cmds, cmd)
            if w:
                w.pack(fill=tk.X, padx=5, pady=2)

        # --- COLUMN 1: Modal Functions ---
        self.frame_funcs = ttk.LabelFrame(self.scrollable_content, text="Modal Functions")
        self.frame_funcs.grid(row=0, column=1, padx=10, pady=10, sticky="nsew")
        
        functions = [
            "range_scale", "velocity_scale", "scan_width", "bars", 
            "expand", "zoom", "sar_map_orientation", "ground_tgt_reject", 
            "min_det_ground_tgt", "power_up_stop", "spot", 
            "sar_enabled", "ghost_enabled", "dtt_enabled"
        ]
        for func in functions:
            w = create_widget_by_id(self.frame_funcs, func)
            if w:
                w.pack(fill=tk.X, padx=5, pady=2)

        # --- COLUMN 1 (Lower): Parameters ---
        self.frame_params = ttk.LabelFrame(self.scrollable_content, text="Parameters")
        self.frame_params.grid(row=1, column=1, padx=10, pady=10, sticky="nsew")
        
        params = [
            "alt_block", "tgt_history", "video_intensity", 
            "if_gain", "mov_tgt_gain", "beacon_delay", "beacon_code"
        ]
        for p in params:
            w = create_widget_by_id(self.frame_params, p)
            if w:
                w.pack(fill=tk.X, padx=5, pady=2)

        # --- COLUMN 0 (Lower): Frequency ---
        self.frame_freq = ttk.LabelFrame(self.scrollable_content, text="Channels & Frequency")
        self.frame_freq.grid(row=1, column=0, padx=10, pady=10, sticky="nsew")
        
        freq_list = [
            "channel", "frequency_agility", "freq_grouping_selection", 
            "waveform", "lprf_threshold", "prf_lookup"
        ]
        for f in freq_list:
            w = create_widget_by_id(self.frame_freq, f)
            if w:
                w.pack(fill=tk.X, padx=5, pady=2)

        # --- COLUMN 2: Quick Controls ---
        self.frame_quick = ttk.LabelFrame(self.scrollable_content, text="Quick Navigation")
        self.frame_quick.grid(row=0, column=2, rowspan=2, padx=10, pady=10, sticky="nsew")
        
        quicks = [
            "ptaz", "speed", "altitude", "pitch", "roll", 
            "crs_slave", "crs_az", "crs_rng"
        ]
        for q in quicks:
            w = create_widget_by_id(self.frame_quick, q)
            if w:
                w.pack(fill=tk.X, padx=5, pady=2)