import plotly.graph_objects as go


Row = tuple[str, int]


def value_table(data: list[Row]) -> dict[str, list]:
    return {"columns": ["Site", "Surface reflectance (%)"], "rows": [list(row) for row in data]}


def _style_figure(fig: go.Figure, **layout: object) -> go.Figure:
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": "Surface reflectance by site", "x": 0.02, "xanchor": "left"},
        margin={"l": 72, "r": 32, "t": 68, "b": 60},
        showlegend=False,
        **layout,
    )
    return fig


def load_data() -> list[Row]:
    return [("West", 30), ("North", 50), ("Center", 42), ("South", 50), ("East", 68)]


def _reflectance_swatches(data: list[Row], surrounds: list[str]) -> go.Figure:
    fig = go.Figure()
    for index, ((site, reflectance), surround) in enumerate(zip(data, surrounds, strict=True)):
        fig.add_shape(
            type="rect",
            x0=index - 0.43,
            x1=index + 0.43,
            y0=0.08,
            y1=0.92,
            fillcolor=surround,
            line_width=0,
            layer="below",
        )
        gray = round(255 * reflectance / 100)
        fig.add_scatter(
            x=[index],
            y=[0.5],
            mode="markers",
            marker={"symbol": "square", "size": 30, "color": f"rgb({gray},{gray},{gray})"},
            customdata=[[site, reflectance]],
            hovertemplate="%{customdata[0]}: %{customdata[1]}%<extra></extra>",
        )
    return _style_figure(
        fig,
        height=250,
        xaxis={
            "tickvals": list(range(len(data))),
            "ticktext": [row[0] for row in data],
            "tickangle": 0,
            "range": [-0.5, len(data) - 0.5],
            "showline": False,
            "ticks": "",
        },
        yaxis={"visible": False, "range": [0, 1]},
    )


def make_before(data: list[Row]) -> go.Figure:
    return _reflectance_swatches(data, ["#ededed", "#f8f8f8", "#ededed", "#252525", "#ededed"])


if __name__ == "__main__":
    print(make_before(load_data()).to_json())
