# Run this app with `python app.py` and
# visit http://127.0.0.1:8050/ in your web browser.

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd

import DyMat
import postproc


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

# TODO: use argparse or somethng else to get filename
modelica_result_path = "kiss_1_res.mat"
modelica_result = DyMat.DyMatFile(modelica_result_path)

# parameters for visualisation
tmax = 1000
every = 5

# -----------------------------------------
# Cell temperatures vs. time, as facet plot
# ----------------------------------------- 
k = 0   # plot fwd cells only
df_tcell = postproc.read_tcell(modelica_result, every=every).query(f'k=={k} & t<{tmax}')
df_tcell["Tcell"] -= 273.15
fig_tcell = px.line(df_tcell, x="t", y="Tcell",     
              facet_row="j",
              facet_col="i",
              title='Cell Temperature (°C)')


# ------------------------------------------
# Fluid temperatures vs. time, as facet plot
# ------------------------------------------
# limit to time < 1000 and last fluid segment
pos = 3  # pipe has three segments, show last segment only
df_tfluid = postproc.read_tfluid(modelica_result, every=every).query(f't<{tmax} & pos=={pos}')
# convert to °C
df_tfluid["Tfluid"] -= 273.15
fig_tfluid = px.line(df_tfluid, x="t", y="Tfluid",     
              facet_row="pipe",
              facet_col="bar",
              title='Fluid Temperature (°C)')


# ----------------------------------------------------------
# Temperature of walls between pipes vs. time, as facet plot
# ----------------------------------------------------------
df_twall = postproc.read_twall(modelica_result, every=every).query(f't < {tmax}')
# convert to °C
df_twall["Twall"] -= 273.15

fig_twall = px.line(df_twall, x="t", y="Twall",     
              facet_row="pipe",
              facet_col="bar",
              title='Wall Temp (°C)')


# ----------------------------------------------------------
# Heat flow in walls between pipes vs. time, as facet plot
# ----------------------------------------------------------
df_qwall = postproc.read_qwall(modelica_result, every=every).query(f't < {tmax}')

fig_qwall = px.line(df_qwall, x="t", y="Q_flow",     
              facet_row="wall",
              facet_col="bar",
              line_group="port",
              color="port",
              title='Heat flow in walls between pipes (W)')


# -----------------
# Page layout 
# -----------------
app.layout = html.Div([
    html.H1(children='Battery Pack Results'),
    html.P(children=f"Result file: {modelica_result_path}"),
    html.H2(children="Cell Temperatures"),
    dcc.Graph(
        id='tcell_vs_t',
        figure=fig_tcell
    ),
    html.H2(children="Fluid Temperatures"),
    dcc.Graph(
        id='tfluid_vs_t',
        figure=fig_tfluid
    ), 
    html.H2(children="Temperature in walls between pipes"),
    dcc.Graph(
        id='twall_vs_t',
        figure=fig_twall
    ), 
    html.H2(children="Heat flow in walls between pipes"),
    html.P(children="port a is to upper pipe, port b to lower pipe"),
    dcc.Graph(
        id='qwall_vs_t',
        figure=fig_qwall
    ),     
])

if __name__ == '__main__':
    app.run_server(debug=True)
