"""Transform controls panel component.

Provides global transform defaults and per-channel override capability.
"""

from __future__ import annotations

import dash_bootstrap_components as dbc
from dash import dcc, html


def build_transform_panel() -> dbc.Card:
    """Build the transform controls card."""
    return dbc.Card(
        [
            dbc.CardHeader("Transforms", className="fw-bold py-2"),
            dbc.CardBody(
                [
                    # Global CFC Filter
                    dbc.Label("CFC Filter (global)", size="sm", className="mb-1"),
                    dbc.Select(
                        id="cfc-select",
                        options=[
                            {"label": "None (raw)", "value": "none"},
                            {"label": "CFC 60 (100 Hz)", "value": "60"},
                            {"label": "CFC 180 (300 Hz)", "value": "180"},
                            {"label": "CFC 600 (1000 Hz)", "value": "600"},
                            {"label": "CFC 1000 (1650 Hz)", "value": "1000"},
                        ],
                        value="none",
                        size="sm",
                        className="mb-2",
                    ),
                    # Resultant toggle
                    dbc.Checkbox(
                        id="show-resultant",
                        label="Compute resultant",
                        value=False,
                        className="mb-1",
                    ),
                    # Y-align toggle
                    dbc.Checkbox(
                        id="y-align-check",
                        label="Y-axis zero correction",
                        value=False,
                        className="mb-2",
                    ),
                    html.Hr(style={"margin": "8px 0"}),
                    # X-align controls
                    dbc.Label("X-Axis Alignment", size="sm", className="mb-1"),
                    dbc.Select(
                        id="x-align-method",
                        options=[
                            {"label": "None", "value": "none"},
                            {"label": "Manual offset", "value": "manual"},
                            {"label": "Threshold crossing", "value": "threshold"},
                        ],
                        value="none",
                        size="sm",
                        className="mb-1",
                    ),
                    dbc.Input(
                        id="x-align-value",
                        type="number",
                        placeholder="Time offset (s) or threshold",
                        size="sm",
                        step=0.001,
                        className="mb-2",
                    ),
                    html.Hr(style={"margin": "8px 0"}),
                    # Per-channel overrides
                    dbc.Label("Per-Channel Overrides", size="sm", className="mb-1"),
                    html.Div(
                        id="per-channel-overrides",
                        children=[
                            html.Div(
                                "Select channels to set individual filters",
                                className="text-muted",
                                style={"fontSize": "10px"},
                            ),
                        ],
                    ),
                    # Hidden store for per-channel override data
                    dcc.Store(id="channel-overrides-store", data={}),
                ],
                className="py-2",
            ),
        ]
    )


def build_per_channel_override_rows(
    selected_keys: list[str],
    overrides: dict[str, dict[str, str]],
) -> list:
    """Build per-channel override controls for selected channels.

    Shows a compact row per selected channel with a CFC dropdown override.
    """
    if not selected_keys:
        return [
            html.Div("No channels selected", className="text-muted", style={"fontSize": "10px"})
        ]

    rows = []
    for key in selected_keys[:10]:  # Limit to 10 to avoid overwhelming the panel
        ch_name = key.split("::")[-1] if "::" in key else key
        # Truncate long names
        display_name = ch_name if len(ch_name) <= 18 else ch_name[:16] + ".."

        current_cfc = overrides.get(key, {}).get("cfc", "")

        rows.append(
            html.Div(
                [
                    html.Span(
                        display_name,
                        style={
                            "fontSize": "10px",
                            "fontFamily": "monospace",
                            "width": "130px",
                            "display": "inline-block",
                            "overflow": "hidden",
                            "textOverflow": "ellipsis",
                            "whiteSpace": "nowrap",
                            "verticalAlign": "middle",
                        },
                    ),
                    dbc.Select(
                        id={"type": "ch-cfc-override", "index": key},
                        options=[
                            {"label": "—", "value": ""},
                            {"label": "60", "value": "60"},
                            {"label": "180", "value": "180"},
                            {"label": "600", "value": "600"},
                            {"label": "1000", "value": "1000"},
                        ],
                        value=current_cfc,
                        size="sm",
                        style={
                            "fontSize": "10px",
                            "width": "70px",
                            "display": "inline-block",
                            "verticalAlign": "middle",
                            "marginLeft": "4px",
                        },
                    ),
                ],
                className="mb-1",
            )
        )

    if len(selected_keys) > 10:
        rows.append(
            html.Div(
                f"+{len(selected_keys) - 10} more",
                className="text-muted",
                style={"fontSize": "10px"},
            )
        )

    return rows
