from IPython.display import display
from ipywidgets import widgets as wid
import matplotlib.pyplot as plt
import c_curves
import welding
import constants as c
import time

Tt_weld_curves, weld_yvals, Tt_weld_times = welding.get_Tt_weld_curves()

def main(case, qvd = 0.05, ylog = True, T_min = 300, T_max = 470):
    if case == 'C-curves':
        if T_min >= T_max:
            print("I wont let you put T_min >= T_max, so I'm going to set T_min =", T_max-10)
            T_min = T_max - 10
        c_curves.plot_C_curves(T_min = T_min, T_max = T_max)

    elif case == 'T-t Weld curves':
        welding.plot_Tt_weld_V3(qvd, Tt_weld_curves, weld_yvals, Tt_weld_times, ylog=ylog)
        #welding.plot_Tt_weld_V1(qvd, ylog=ylog)
        #welding.plot_Tt_weld_V2(qvd, ylog=ylog)

    elif case == 'X-q Weld curves':
        welding.plot_X_weld()

def run():
    case = wid.Dropdown(options=['C-curves', 'T-t Weld curves', 'X-q Weld curves'], description = 'Case')
    qvd = wid.FloatSlider(min = c.qvd_slider_min, max = c.qvd_slider_max, step = c.qvd_slider_step, continious_update = False,
                          description = r'$\frac{q_0}{vd} \left[\frac{kJ}{mm^2}\right]$')
    ylog = wid.ToggleButton(value=True, description = 'Logarithmic y-axis')

    T_min = wid.IntSlider(min = 50, max = 460, step = 10, continious_update = False,
                          description = r'$T_{min}$', value = 290)
    T_max = wid.IntSlider(min=60, max=470, step=10, continious_update=False,
                          description=r'$T_{max}$', value = 460)

    display(case)
    output = wid.Output()
    case_output = wid.interactive(main, case = case,
                                  qvd = qvd, ylog = ylog,
                                  T_min = T_min, T_max = T_max)


    def display_case_controls(case):
        case = case['new']
        if case == 'C-curves':
            case_controls = wid.HBox([T_min, T_max])
        elif case == 'T-t Weld curves':
            case_controls = wid.HBox([qvd, ylog])
        elif case =='X-q Weld curves':
            case_controls = wid.HBox([])

        output.clear_output()
        with output:
            display(wid.VBox([case_controls, case_output.children[-1]]))

    case.observe(display_case_controls, names = 'value')
    display(output)