from dash import Dash, html, dcc, dash_table
import dash_bootstrap_components as dbc
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np

# Read Data
df = pd.read_excel('evaluation.xlsx', sheet_name='results')

# Add graph data
course = ["{} <br> n={}".format(c,s) for c, s in zip(df['Course'], df['Submissions'])]
avg = df['Avg. Score']
max = df['Max Score']

# Relative data
max_rel = df['Max Score'].sum()
avg_rel = np.array([c/s*100 for c, s in zip(df['Avg. Score'], df['Max Score'])])
avg_rel_sum = df['Avg. Score'].sum() / max_rel * 100

# Make traces for graph
avg_trace = go.Bar(x=course, y=avg, xaxis='x1', yaxis='y1',
                marker=dict(color='#0099ff'),
                name='Average Score<br>over all Students')
max_trace = go.Bar(x=course, y=max, xaxis='x1', yaxis='y1',
                marker=dict(color='#404040'),
                name='Possible Max Score<br>Per Notebook')
avg_rel_trace = go.Scatter(x=course, y=avg_rel, xaxis='x1', yaxis='y2',
                marker=dict(color='#e00030'),
                name='Average Score (in %)<br>Per Notebook')

perc_trace = go.Scatter(x=course, y=avg_rel, xaxis='x1', yaxis='y2',
                        mode="text+lines",
                        marker=dict(color='#e00030'),
                        name='Average Score (in %)<br>Per Notebook',
                        text=[str(n) for n in avg_rel], texttemplate='%{text:.0f}% ',
                        textposition=[
                            'top center', 'top right', 'middle left',
                            'middle left', 'middle left', 'top left',
                            'top left', 'bottom left'
                        ],
                        textfont={'size': 12})

def bar():
    fig = make_subplots(specs=[[{"secondary_y": True}]])

    fig.update_layout(template="plotly_dark")
    
    # Y-Axis Title
    fig.update_yaxes(title_text="<b>Relative Score (in %)</b>", secondary_y=True)
    fig.update_yaxes(title_text="<b>Score</b>", secondary_y=False)

    # Add Plots
    fig.add_trace(avg_trace)
    fig.add_trace(max_trace)
    fig.add_trace(perc_trace)
    #fig.add_trace(avg_rel_trace)
    return fig


# Create Dash
app = Dash(__name__, external_stylesheets=[dbc.themes.CYBORG, dbc.icons.FONT_AWESOME])

header = dbc.Row([
        html.H1('Einführung in die Programmierung für Nicht-Informatiker*innen', className="text-primary text-center fs-3"),
        html.H2('WiSe 23/24 Results', className="text-secondary text-center fs-4")
    ])


app.layout = dbc.Container([
    header,
    dbc.Row([dcc.Graph(figure=bar())]),
    html.Br(),
    dbc.Row([
        dbc.Alert([
            html.I(className="bi bi-info-circle-fill me-2"), 
            html.Big("{:.0f}% of all tasks were solved correctly.".format(avg_rel_sum), className="text-center")
        ],
        color='info', className="d-flex align-items-center")
    ])
])



if __name__ == '__main__':
    #app.run(debug=True)
    #from plotly.offline.offline import _plot_html
    import plotly
    plotly.offline.plot(
      bar(),
      show_link=False,
      filename = 'eval.html'
    )