from IPython.display import display
from ipywidgets import widgets as wid
import matplotlib.pyplot as plt
import numpy as np
import c_curves
import welding
import jominy_stick
import extruded_profile
import constants as c
import time
import warnings

warnings.filterwarnings('ignore')

# Measuring some runtimes and calculating values before displaying anything
# To make the program run more smoothly
t0 = time.process_time()
Tt_weld_curves, weld_yvals, Tt_weld_times = welding.get_Tt_curves()
t_Tt_weld_curves = time.process_time() - t0
num_Tt_weld_vals = np.sum([np.product(temps.shape) for temps in Tt_weld_curves])

t0 = time.process_time()
Xq_curves, qvd_line, Xq_y_vals = welding.get_Xq_curves()
t_Xq_curves = time.process_time() - t0

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_V2(T_min = T_min, T_max = T_max)

    elif case == 'T-t Weld':
        print('Calculating T(q,y,t) in ', num_Tt_weld_vals, ' points (all combinations of q, y and t) took ', round(t_Tt_weld_curves,3), 's', sep='')
        welding.plot_Tt_V3(qvd, Tt_weld_curves, weld_yvals, Tt_weld_times, ylog=ylog)

        #Alternative plotting functions for comparability when testing runtime
        #welding.plot_Tt_V1(qvd, ylog=ylog)
        #welding.plot_Tt_V2(qvd, ylog=ylog)

    elif case == 'X-q Weld':
        print('Calculating X(q,y) in ', np.product(Xq_curves.shape), ' points (all displayed) took ', round(t_Xq_curves,3), 's', sep='')
        welding.plot_Xq(Xq_curves, qvd_line, Xq_y_vals)

    elif case == 'T-t Jominy stick':
        jominy_stick.plot_Tt()

    elif case == 'X-x Jominy stick':
        jominy_stick.plot_Xx()

    elif case == 'T-t Extrusion':
        extruded_profile.plot_Tt()

    elif case == "X-h Extrusion":
        extruded_profile.plot_Xh()




def run():
    case = wid.Dropdown(options=['X-q Weld', 'X-x Jominy stick', "X-h Extrusion", 'C-curves', 'T-t Weld', 'T-t Jominy stick', 'T-t Extrusion'], 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':
            case_controls = wid.HBox([qvd, ylog])
        elif case =='X-q Weld':
            case_controls = wid.HBox([])
        elif case == 'T-t Jominy stick':
            case_controls = wid.HBox([])
        elif case == 'X-x Jominy stick':
            case_controls = wid.HBox([])
        elif case == 'T-t Extrusion':
            case_controls = wid.HBox([])
        elif case == "X-h Extrusion":
            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)