# radar_data_reader/gui/config_frames.py

import tkinter as tk
from tkinter import ttk
import os


class GReconverterOptionsFrame(ttk.Frame):
    """A reusable frame containing all advanced options for g_reconverter."""

    def __init__(self, master, config_vars: dict):
        super().__init__(master)
        self.vars = config_vars
        self._create_widgets()

    def _create_widgets(self):
        notebook = ttk.Notebook(self)
        notebook.pack(fill=tk.BOTH, expand=True, pady=5)

        processing_tab = ttk.Frame(notebook, padding="10")
        advanced_tab = ttk.Frame(notebook, padding="10")
        video_tab = ttk.Frame(notebook, padding="10")
        debug_tab = ttk.Frame(notebook, padding="10")

        notebook.add(processing_tab, text="Processing")
        notebook.add(advanced_tab, text="Advanced")
        notebook.add(video_tab, text="Video")
        notebook.add(debug_tab, text="Debug/Control")

        self._setup_processing_widgets(processing_tab)
        self._setup_advanced_widgets(advanced_tab)
        self._setup_video_widgets(video_tab)
        self._setup_debug_control_widgets(debug_tab)

    def _setup_processing_widgets(self, parent):
        parent.columnconfigure((0, 1, 2), weight=1)
        flags = [
            ("analyze_only", "Analyze Only (/a)"),
            ("no_sign", "Don't Save Signal (/nosign)"),
            ("use_fw_header", "Use FW Header (/hfw)"),
            ("fix_dsp_bug", "Fix DSP Geopos Bug (/$pbug)"),
            ("sync_signal_flows", "Sync Signal Flows (/h)"),
            ("extract_sw_only", "Extract Only SW Data (/sw)"),
            ("extract_dsp_only", "Extract Only DSP Data (/dsp)"),
            ("stop_on_discontinuity", "Stop on Discontinuity (/ss)"),
            ("dry_run", "Dry Run (/dryrun)"),
            ("start_from_stby", "Start Save from STBY (/z)"),
            ("fix_sata_date", "Fix SATA File Date (/d)"),
            ("examine_data", "Examine Some Data (/x)"),
        ]
        row, col = 0, 0
        for key, text in flags:
            ttk.Checkbutton(parent, text=text, variable=self.vars[key]).grid(
                row=row, column=col, sticky="w", padx=5, pady=2
            )
            row += 1
            if row > 4:
                row, col = 0, col + 1

        pp_frame = ttk.Frame(parent)
        pp_frame.grid(row=row, column=col, sticky="w", columnspan=2, pady=2)
        ttk.Checkbutton(
            pp_frame,
            text="Post Process History (/p)",
            variable=self.vars["post_process"],
        ).pack(side=tk.LEFT)
        ttk.Label(pp_frame, text="Level:").pack(side=tk.LEFT, padx=(10, 2))
        ttk.Entry(pp_frame, textvariable=self.vars["post_process_level"], width=5).pack(
            side=tk.LEFT
        )

    def _setup_advanced_widgets(self, parent):
        parent.columnconfigure(1, weight=1)
        ttk.Label(parent, text="JSON Config File (/j):").grid(
            row=0, column=0, sticky="w", padx=5, pady=5
        )
        ttk.Entry(parent, textvariable=self.vars["json_config_file"]).grid(
            row=0, column=1, sticky="ew", padx=5
        )
        ttk.Label(parent, text="Max Batches (/m):").grid(
            row=1, column=0, sticky="w", padx=5, pady=5
        )
        ttk.Entry(parent, textvariable=self.vars["max_batches"], width=10).grid(
            row=1, column=1, sticky="w", padx=5
        )
        ttk.Label(parent, text="PRI Number (/r):").grid(
            row=2, column=0, sticky="w", padx=5, pady=5
        )
        ttk.Entry(parent, textvariable=self.vars["pri_number"], width=10).grid(
            row=2, column=1, sticky="w", padx=5
        )
        ttk.Label(parent, text="RBIN Number (/c):").grid(
            row=3, column=0, sticky="w", padx=5, pady=5
        )
        ttk.Entry(parent, textvariable=self.vars["rbin_number"], width=10).grid(
            row=3, column=1, sticky="w", padx=5
        )

    def _setup_video_widgets(self, parent):
        parent.columnconfigure((0, 1), weight=1)
        flags = [
            ("video_out_to_rec", "Output to Rec Results (/vout)"),
            ("video_show", "Show Video (/vshow)"),
            ("video_save", "Save Video (/vsave)"),
            ("video_fix_framerate", "Fix Frame Rate (/vframe)"),
            ("gps_save_track", "GPS: Save Track (/gps)"),
            ("fix_hr_debug", "HR: Fix Debug Data (/fixhr)"),
            ("sar_save_only", "SAR: Save Only SAR (/sar)"),
            ("video_add_subtitles", "Add AVI Subtitles (/vst)"),
        ]
        row, col = 0, 0
        for key, text in flags:
            ttk.Checkbutton(parent, text=text, variable=self.vars[key]).grid(
                row=row, column=col, sticky="w", padx=5, pady=2
            )
            row += 1
            if row > 4:
                row, col = 0, col + 1

    def _setup_debug_control_widgets(self, parent):
        parent.columnconfigure((0, 1), weight=1)
        flags = [
            ("verbose", "Verbose Output (//v)"),
            ("debug_messages", "Debug Messages (//d)"),
            ("quiet", "Quiet Mode (//q)"),
            ("mask_warnings", "Mask Warnings (//w)"),
            ("mask_errors", "Mask Errors (//e)"),
            ("silent_overwrite", "Silently Overwrite Output (//o)"),
        ]
        row, col = 0, 0
        for key, text in flags:
            ttk.Checkbutton(parent, text=text, variable=self.vars[key]).grid(
                row=row, column=col, sticky="w", padx=5, pady=2
            )
            row += 1
            if row > 3:
                row, col = 0, col + 1

        # Aggiungiamo la nuova checkbox per la profilatura
        profiling_check = ttk.Checkbutton(
            parent,
            text="Enable Python Worker Profiling (Dev)",
            variable=self.vars["enable_python_worker_profiling"],
        )
        profiling_check.grid(row=row, column=col, sticky="w", padx=5, pady=10)
