import pandas as pd
import numpy as np
import sys
sys.path.insert(1, '/')
import pdm_viz.EDA.EDA as eda
sys.path.insert(1, '/app')
import plotly.graph_objects as go
import datetime

tagList = ['MV26_PDI-1535A-02.PV_mean', 'MV26_PDI-1535B-02.PV_mean']

df = pd.read_csv('/app/data/preprocessed/temp.csv').set_index('datetime')

def plotSeries(train, hr=5):
    df = pd.read_csv('/app/data/preprocessed/temp.csv').set_index('datetime')
    lastReg = datetime.datetime.strptime(np.max(df.index), '%Y-%m-%d %H:%M:%S')
    print(lastReg)
    preds = pd.read_csv('/app/backend/simulator/data/predictions/differential.csv', index_col=0)
    preds.index = pd.to_datetime(preds.index)
    preds = preds.loc[preds.index <= (lastReg + datetime.timedelta(hours=hr+1)), :]
    if train == 'A':
        plotDf = df.loc[:, [tagList[0]]]
        predDf = preds.loc[:, [tagList[0]]]
        titles = ['DP Train A']
    elif train == 'B':
        plotDf = df.loc[:, [tagList[1]]]
        predDf = preds.loc[:, [tagList[1]]]
        titles = ['DP Train B']

    ts = eda.timeSeries(plotDf, yAxisTitles=titles, logo=False)
    ts.fig.add_trace(go.Scatter(x=predDf.index, y=predDf.iloc[:, 0], line=dict(width=1, color='#DA1A5D'), mode='lines', name='Predicted'),
                                row=1, col=1)
    ts.fig.update_layout(
        title=None,
    # {'text':'<b>MV26</b>',
    #            'x': 0.5,
    #            'y': 0.9,
    #            'xanchor': 'center'},
        margin={'t': 10},
        font_family="Roboto",
        font_color="#FFFFFF",
        title_font_family="Roboto",
        title_font_color="#FFFFFF",
        title_font_size=20,
        paper_bgcolor='rgba(0,0,0,0)',
        plot_bgcolor='rgba(0,0,0,0)'
    )
    return ts.fig

def plotMEx(train):
    idx = df.index
    if train == 'A':
        plotDf = df.loc[:, [tagList[0]]].sort_values(by=tagList[0], ascending=False)
        titles = ['Remaining Usefull Life (Hours)']
    elif train == 'B':
        plotDf = df.loc[:, [tagList[1]]].sort_values(by=tagList[1], ascending=False)
        titles = ['Remaining Usefull Life (Hours)']
    plotDf.index = df.index = idx
    ts = eda.timeSeries(plotDf, yAxisTitles=titles, logo=False)
    ts.fig.update_layout(
        title=None,
        margin={'t': 10},
        font_family="Roboto",
        font_color="#FFFFFF",
        title_font_family="Roboto",
        title_font_color="#FFFFFF",
        title_font_size=20,
        paper_bgcolor='rgba(0,0,0,0)',
        plot_bgcolor='rgba(0,0,0,0)'
    )
    return ts.fig
    return