"""Math expression builder component.

Allows users to create derived channels from mathematical expressions.
Variables are bound to selected channels via dropdowns.

Example: sqrt(ax**2 + az**2) with ax=Head Accel X, az=Head Accel Z
"""

from __future__ import annotations

from typing import Any

import dash_bootstrap_components as dbc
from dash import dcc, html

from impakt.web.state import AppState


def build_math_panel(app_state: AppState) -> dbc.Card:
    """Build the math expression builder panel."""

    # Build channel options for variable binding
    channel_options = [{"label": "—", "value": ""}]
    for loaded in app_state.tests:
        prefix = f"[{loaded.test_id}] " if len(app_state.tests) > 1 else ""
        for ch in loaded.data:
            label = ch.code.short_label if ch.code.is_valid else ch.name
            channel_options.append(
                {
                    "label": f"{prefix}{label}",
                    "value": f"{loaded.test_id}::{ch.name}",
                }
            )

    return dbc.Card(
        [
            dbc.CardHeader("Math Expression", className="fw-bold py-2"),
            dbc.CardBody(
                [
                    dbc.Label("Expression", size="sm", className="mb-1"),
                    dbc.Textarea(
                        id="math-expression",
                        placeholder="sqrt(a**2 + b**2)\na + b * 0.5\nabs(a) - abs(b)",
                        size="sm",
                        className="mb-2",
                        style={"fontSize": "12px", "fontFamily": "monospace", "height": "60px"},
                    ),
                    # Variable bindings
                    dbc.Label("Variables", size="sm", className="mb-1"),
                    dbc.Row(
                        [
                            dbc.Col(
                                html.Span(
                                    "a =", style={"fontSize": "12px", "fontFamily": "monospace"}
                                ),
                                width=2,
                            ),
                            dbc.Col(
                                dbc.Select(
                                    id="math-var-a",
                                    options=channel_options,
                                    value="",
                                    size="sm",
                                    style={"fontSize": "11px"},
                                ),
                                width=10,
                            ),
                        ],
                        className="mb-1",
                    ),
                    dbc.Row(
                        [
                            dbc.Col(
                                html.Span(
                                    "b =", style={"fontSize": "12px", "fontFamily": "monospace"}
                                ),
                                width=2,
                            ),
                            dbc.Col(
                                dbc.Select(
                                    id="math-var-b",
                                    options=channel_options,
                                    value="",
                                    size="sm",
                                    style={"fontSize": "11px"},
                                ),
                                width=10,
                            ),
                        ],
                        className="mb-1",
                    ),
                    dbc.Row(
                        [
                            dbc.Col(
                                html.Span(
                                    "c =", style={"fontSize": "12px", "fontFamily": "monospace"}
                                ),
                                width=2,
                            ),
                            dbc.Col(
                                dbc.Select(
                                    id="math-var-c",
                                    options=channel_options,
                                    value="",
                                    size="sm",
                                    style={"fontSize": "11px"},
                                ),
                                width=10,
                            ),
                        ],
                        className="mb-2",
                    ),
                    # Result name and unit
                    dbc.Row(
                        [
                            dbc.Col(
                                dbc.Input(
                                    id="math-result-name",
                                    placeholder="Result name",
                                    size="sm",
                                    style={"fontSize": "12px"},
                                ),
                                width=7,
                            ),
                            dbc.Col(
                                dbc.Input(
                                    id="math-result-unit",
                                    placeholder="Unit",
                                    size="sm",
                                    style={"fontSize": "12px"},
                                ),
                                width=5,
                            ),
                        ],
                        className="mb-2",
                    ),
                    dbc.Button(
                        "Compute & Plot",
                        id="math-compute-btn",
                        color="primary",
                        size="sm",
                        className="w-100",
                        style={"fontSize": "11px"},
                    ),
                    html.Div(id="math-status", className="mt-2"),
                    html.Div(
                        [
                            html.Hr(style={"margin": "8px 0"}),
                            html.Span(
                                "Available functions: ",
                                style={"fontSize": "10px", "fontWeight": "bold"},
                            ),
                            html.Span(
                                "abs, sqrt, sin, cos, tan, exp, log, log10, max, min, "
                                "clip, sign, cumsum, diff, gradient, mean, std, pi, e",
                                style={"fontSize": "10px", "color": "#666"},
                            ),
                        ]
                    ),
                ],
                className="py-2",
            ),
        ],
        className="mb-2",
    )
