import pandas as pd
import numpy as np

from jinja2 import Template

from bokeh.io import output_file, show, vform
from bokeh.plotting import *
from bokeh.charts import *
from bokeh.models import *
from bokeh.models.widgets import CheckboxButtonGroup
from bokeh.embed import components
from bokeh.resources import INLINE
from bokeh.util.browser import view

###### -- Difined color palette-- ########
Fblue = "#3fa0d4"
Fdark_green = "#47994b"
Flight_green = "#88ba14"
Fgrey_blue = "#437c92"
Fturquoise ="#0aaba4"
Fsand ="#dabe86"
Fbrown ="#59392a"
Fred ="#C90119"
Fsand20 ="#f7f2e7"
###### -- end -- ########


########## DATA ################

###### -- Import -- ########
with pd.ExcelFile('/home/patricep/Documents/OCE042_SeaBird_E04-029_Day_RawData.xlsx') as xlsx:
    df1 = pd.read_excel(xlsx, 'Day')
    df2 = pd.read_excel(xlsx, 'Night')
###### -- end -- ########

###### -- Create Column Data Source that will be used by the plot -- ########

data = dict(Pressure=[],
            Depth=[],
            Temperature=[],
            Salinity=[],
            Conductivity=[],
            Density=[],
            DescentRate=[],
            SoundVelocity=[],
            Oxygenraw=[],
            Oxygenmgl=[],
            Oxygenpercent=[],
            Flag=[]
            )

source = ColumnDataSource(data)


###### -- end -- ########
###### -- Night -- ########
data2 = dict(Pressure=df2["Pressure"],
            Depth=df2["Depth"],
            Temperature=df2["Temperature"],
            Salinity=df2["Salinity"],
            Conductivity=df2["Conductivity"],
            Density=df2["Density"],
            DescentRate=df2["Descent Rate"],
            SoundVelocity=df2["Sound Velocity"],
            Oxygenraw=df2["Oxygen raw"],
            Oxygenmgl=df2["Oxygen [mg/l]"],
            Oxygenpercent=df2["Oxygen [%]"],
            Flag=df2["Flag"]
            )

source2 = ColumnDataSource(data2)

###### -- end -- ########

###### -- Plot option -- ########


TOOLS = 'crosshair,pan,wheel_zoom,box_zoom,reset,hover,save'

plot_options = dict(y_axis_label="Depth [m]", background_fill_color=Fsand20, width=450, height=450, title=None, tools=TOOLS)

###### -- end -- ########

###### -- Set up Ratio button to show/hide cast -- ########
cast_button_group = RadioButtonGroup(
        labels=["Both", "Downcast", "Upcast"], active=0)

b = vform(cast_button_group)
###### -- end -- ########

###### -- Set up callbacks -- ########
def update_data(old, new):
    cast_val = cast_button_group.active
    new_df = pd.read_excel(xlsx, 'Day')

    if (cast_button_group.active == 1):
        new_df = new_df.loc[df1['Flag'] == "Downcast"].to_dict()
    elif (cast_button_group.active == 2):
        new_df = dfU.loc[df1['Flag'] == "Upcast"].to_dict()
    else:
        new_df = new_df.to_dict()

    df = new_df
    source.date = dict(
            Pressure=df["Pressure"],
            Depth=df["Depth"],
            Temperature=df["Temperature"],
            Salinity=df["Salinity"],
            Conductivity=df["Conductivity"],
            Density=df["Density"],
            DescentRate=df["Descent Rate"],
            SoundVelocity=df["Sound Velocity"],
            Oxygenraw=df["Oxygen raw"],
            Oxygenmgl=df["Oxygen [mg/l]"],
            Oxygenpercent=df["Oxygen [%]"],
            Flag=df["Flag"]
            )
    
    
controls = [cast_button_group]
for control in controls:
    control.on_click(update_data)
###### -- end -- ########


########## PLOTS ################

###### -- First one -- ########
line0 = figure(**plot_options)
line0.line('Temperature', 'Depth', source=source, color=Fred)
line0.xaxis.axis_label = "Temperature [°C]"
line0.y_range.bounds = 'auto'
line0.axis.axis_label_text_font_size = "14pt"
line0.axis.axis_label_text_font_style = "bold"
###### -- end -- ########
 
###### -- linked panning is expressed by sharing ranges between plots.  -- ########
line1 = figure(y_range=line0.y_range, **plot_options)
line1.line('Salinity', 'Depth', source=source, color=Fblue)
line1.xaxis.axis_label = "Salinity [PSU]"
line1.axis.axis_label_text_font_size = "14pt"
line1.axis.axis_label_text_font_style = "bold"

line2 = figure(y_range=line0.y_range, **plot_options)
line2.line('Conductivity', 'Depth', source=source, color=Fturquoise)
line2.xaxis.axis_label = "Conductivity [S/m]"
line2.axis.axis_label_text_font_size = "14pt"
line2.axis.axis_label_text_font_style = "bold"

line3 = figure(y_range=line0.y_range, **plot_options)
line3.line('Density', 'Depth', source=source, color=Fbrown)
line3.xaxis.axis_label = "Density [kg/m^3]"
line3.axis.axis_label_text_font_size = "14pt"
line3.axis.axis_label_text_font_style = "bold"

line4 = figure(y_range=line0.y_range, **plot_options)
line4.line('SoundVelocity', 'Depth', source=source, color=Fgrey_blue)
line4.xaxis.axis_label = "Sound Velocity [Delgrosso, m/s]"
line4.axis.axis_label_text_font_size = "14pt"
line4.axis.axis_label_text_font_style = "bold"

line5 = figure(y_range=line0.y_range, **plot_options)
line5.line('Oxygenmgl', 'Depth', source=source, color=Fdark_green)
line5.xaxis.axis_label = "Dissolved Oxygen [mg/l]"
line5.axis.axis_label_text_font_size = "14pt"
line5.axis.axis_label_text_font_style = "bold"

line6 = figure(y_range=line0.y_range, **plot_options)
line6.line('Oxygenpercent', 'Depth', source=source, color=Flight_green)
line6.xaxis.axis_label = "Dissolved Oxygen [% saturation]"
line6.axis.axis_label_text_font_size = "14pt"
line6.axis.axis_label_text_font_style = "bold"
###### -- end -- ########

###### -- Grid plot option -- ########
p = gridplot([[line0, line1, line3, line5],
             [line2, None, line4, line6]]
        )
###### -- end -- ########

###### -- Hover tool option -- ########
hover = p.select(dict(type=HoverTool))
hover.mode = 'mouse'
hover.tooltips = [
    ("Depth", "$y{1.1}"), # this will format as 1-decimal float
    ("Measure", "$x{1.1}") # this will format as 1-decimal float
]
###### -- end -- ########

###### -- Export to HTLM -- ########
output_file("Test.html", title="Test example")
###### -- end -- ########

###### -- Show plot -- ########
show(vform(b, p))
###### -- end -- ########

