import plotly.graph_objects as go


import plotly.io as pio


INK = "#1C1917"


MUTED = "#57534E"


WARM_EDITORIAL_PALETTE = [
    "#0F766E",
    "#C2410C",
    "#1D4ED8",
    "#A16207",
    "#7E22CE",
    "#475569",
    "#0369A1",
    "#1C1917",
]


CANVAS = "#FFF7ED"


RULE = "#E7DDD2"


pio.templates["warm_editorial"] = go.layout.Template(
    layout=go.Layout(
        colorway=WARM_EDITORIAL_PALETTE,
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "size": 13, "color": INK},
        title={
            "x": 0,
            "xanchor": "left",
            "font": {"family": "Georgia, 'Times New Roman', serif", "size": 18, "color": INK},
        },
        paper_bgcolor=CANVAS,
        plot_bgcolor=CANVAS,
        xaxis={
            "showgrid": False,
            "showline": True,
            "linecolor": "#A8A29E",
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "automargin": True,
        },
        yaxis={
            "showgrid": True,
            "gridcolor": RULE,
            "gridwidth": 1,
            "showline": False,
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "zeroline": False,
            "automargin": True,
        },
    )
)


def load_data():
    return [
        ("Login", "Access", 20, 16),
        ("Password", "Access", 12, 14),
        ("Network", "Connection", 15, 13),
        ("Wi-Fi", "Connection", 9, 8),
        ("Scanner", "Equipment", 0, 3),
        ("Printing", "Equipment", None, 4),
    ]


def make_before(data):
    names, _, current, previous = zip(*data, strict=True)
    total = sum(v for v in current if v is not None)
    shares = [None if v is None else 100 * v / total for v in current]
    fig = go.Figure(go.Bar(x=names, y=shares, name="Share of tags"))
    for label, counts, dash in [("This month", current, "solid"), ("Last month", previous, "dash")]:
        fig.add_trace(
            go.Scatter(
                x=names,
                y=counts,
                mode="lines+markers",
                name=label,
                line=dict(dash=dash),
                marker_symbol=["circle", "diamond", "square", "triangle-up", "cross", "x"],
            )
        )
    fig.update_layout(
        template="simple_white",
        title_text="Share of service desk requests",
        yaxis_title="Percent",
        showlegend=True,
    )
    return fig


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