"""Cursor controls and values table component.

Provides dual X-axis cursor inputs and a table showing interpolated
values at both cursor positions for all plotted channels.
"""

from __future__ import annotations

from typing import Any

import dash_bootstrap_components as dbc
from dash import html

from impakt.channel.model import Channel


def build_cursor_panel() -> dbc.Card:
    """Build the cursor controls and values display card."""
    return dbc.Card(
        [
            dbc.CardHeader(
                [
                    html.Span("X-Axis Cursors", className="fw-bold"),
                ],
                className="py-2",
            ),
            dbc.CardBody(
                [
                    dbc.Row(
                        [
                            dbc.Col(
                                [
                                    dbc.InputGroup(
                                        [
                                            dbc.InputGroupText("x1", style={"fontSize": "12px"}),
                                            dbc.Input(
                                                id="cursor-x1",
                                                type="number",
                                                step=0.001,
                                                value=0.0,
                                                size="sm",
                                                style={"fontSize": "12px"},
                                            ),
                                            dbc.InputGroupText("s", style={"fontSize": "12px"}),
                                        ],
                                        size="sm",
                                    ),
                                ],
                                width=5,
                            ),
                            dbc.Col(
                                [
                                    dbc.InputGroup(
                                        [
                                            dbc.InputGroupText("x2", style={"fontSize": "12px"}),
                                            dbc.Input(
                                                id="cursor-x2",
                                                type="number",
                                                step=0.001,
                                                value=0.1,
                                                size="sm",
                                                style={"fontSize": "12px"},
                                            ),
                                            dbc.InputGroupText("s", style={"fontSize": "12px"}),
                                        ],
                                        size="sm",
                                    ),
                                ],
                                width=5,
                            ),
                            dbc.Col(
                                [
                                    dbc.Button(
                                        "Update",
                                        id="cursor-update-btn",
                                        color="primary",
                                        size="sm",
                                        className="w-100",
                                        style={"fontSize": "12px"},
                                    ),
                                ],
                                width=2,
                            ),
                        ],
                        className="mb-2",
                    ),
                    html.Div(id="cursor-values-table"),
                ],
                className="py-2",
            ),
        ]
    )


def build_cursor_values_table(
    channels: list[tuple[str, Channel]],
    x1: float,
    x2: float,
) -> html.Table | html.Div:
    """Build the cursor values table from resolved channels and positions.

    Args:
        channels: List of (label, channel) tuples.
        x1: First cursor position.
        x2: Second cursor position.

    Returns:
        An HTML table or a placeholder div.
    """
    if not channels:
        return html.Div("No channels plotted", className="text-muted small")

    rows = []
    for label, ch in channels:
        v1 = ch.value_at(x1)
        v2 = ch.value_at(x2)
        delta = v2 - v1

        rows.append(
            html.Tr(
                [
                    html.Td(
                        label,
                        style={
                            "fontSize": "11px",
                            "maxWidth": "180px",
                            "overflow": "hidden",
                            "textOverflow": "ellipsis",
                            "whiteSpace": "nowrap",
                        },
                    ),
                    html.Td(
                        f"{v1:.4f}",
                        style={"fontSize": "11px", "fontFamily": "monospace", "textAlign": "right"},
                    ),
                    html.Td(
                        f"{v2:.4f}",
                        style={"fontSize": "11px", "fontFamily": "monospace", "textAlign": "right"},
                    ),
                    html.Td(
                        f"{delta:+.4f}",
                        style={"fontSize": "11px", "fontFamily": "monospace", "textAlign": "right"},
                    ),
                    html.Td(ch.unit, style={"fontSize": "11px"}),
                ]
            )
        )

    return html.Table(
        [
            html.Thead(
                html.Tr(
                    [
                        html.Th("Channel", style={"fontSize": "10px"}),
                        html.Th(f"@ {x1:.4f}s", style={"fontSize": "10px", "textAlign": "right"}),
                        html.Th(f"@ {x2:.4f}s", style={"fontSize": "10px", "textAlign": "right"}),
                        html.Th("Delta", style={"fontSize": "10px", "textAlign": "right"}),
                        html.Th("Unit", style={"fontSize": "10px"}),
                    ]
                )
            ),
            html.Tbody(rows),
        ],
        className="table table-sm table-hover mb-0",
    )
