import ipywidgets as widgets
from IPython.display import clear_output
from ipywidgets import GridspecLayout,Layout


def create_dict(**widgets):
    return widgets
    
# The smu configuration page
def page_1():
    first_page =  GridspecLayout(5,6)
    
    #first column
    first_page[0,0]= widgets.Label("UNIT",layout=Layout(height='auto', width='auto'))
    first_page[1,0] = widgets.Label('SMU1',layout=Layout(height='auto', width='auto'))
    first_page[2,0] = widgets.Label('SMU2',layout=Layout(height='auto', width='auto'))
    first_page[3,0] = widgets.Label('SMU3',layout=Layout(height='auto', width='auto'))
    first_page[4,0] = widgets.Label('SMU4',layout=Layout(height='auto', width='auto'))
    
    #second column voltage name
    first_page[0,1]= widgets.Label('VNAME',layout=Layout(height='auto', width='auto'))
    for i in range(1,5):
        first_page[i,1]= widgets.Text(layout=Layout(height='auto', width='auto'), value =f'V{i}' )

    #Third column Iname
    first_page[0,2]= widgets.Label('INAME',layout=Layout(height='auto', width='auto'))
    for i in range(1,5):
        first_page[i,2]= widgets.Text(layout=Layout(height='auto', width='auto'),value = f'I{i}')

   
    #Fourth column mode 
    options_mode = ['V','I','COMM','VPULSE','IPULSE']
    first_page[0,3]= widgets.Label('MODE',layout=Layout(height='auto', width='auto'))
    first_page[1,3]= widgets.Dropdown(layout=Layout(height='auto', width='auto'),options = options_mode,value = 'COMM') #smu1
    first_page[2,3]= widgets.Dropdown(layout=Layout(height='auto', width='auto'),options = options_mode,value = 'I') #smu2
    first_page[3,3]= widgets.Dropdown(layout=Layout(height='auto', width='auto'),options = options_mode,value = 'V') #smu3
    first_page[4,3]= widgets.Dropdown(layout=Layout(height='auto', width='auto'),options = options_mode,value = 'V') #smu4
    

    #Fifth column function
    options_function= ['VAR1','VAR2','VARD','CONS']
    first_page[0,4]= widgets.Label('FUNC',layout=Layout(height='auto', width='auto'))
    first_page[1,4]= widgets.Dropdown(layout=Layout(height='auto', width='auto'),options = options_function,value ='CONS') #smu1
    first_page[2,4]= widgets.Dropdown(layout=Layout(height='auto', width='auto'),options = options_function,value ='VAR2') #smu2
    first_page[3,4]= widgets.Dropdown(layout=Layout(height='auto', width='auto'),options = options_function,value ='VAR1') #smu3
    first_page[4,4]= widgets.Dropdown(layout=Layout(height='auto', width='auto'),options = options_function,value ='CONS') #smu4

    # Sixth column ask if smus are disabled
    first_page[0,5] = widgets.Label("DISABLE")
    for i in range(1,5):
        first_page[i,5]= widgets.Checkbox(layout=Layout(height='auto', width='auto'),value = False,indent = False)

    return first_page


#page 2 the user function page
def page_2():
    second_page = GridspecLayout(7,3)

    # first line (descriptions)
    second_page[0,0]= widgets.Label('NAME',layout=Layout(height='auto', width='auto'))
    second_page[0,1]= widgets.Label('UNIT',layout=Layout(height='auto', width='auto'))
    second_page[0,2]= widgets.Label('DEFINITION',layout=Layout(height='auto', width='auto'))

    # Iterate through the lines and columns for text boxes 
    for i in range(1,7):
        for j in range(3):
            second_page[i,j] = widgets.Text(layout=Layout(height='auto', width='auto'))

    return second_page

#parameters setting
def page_3():
    third_page = GridspecLayout(24,5)

    # First Line
    third_page[0,0]=widgets.Dropdown(options = ['SWEEP','SAMPLING',"STRESS"], value = 'SWEEP',description ="MEASUREMENT MODE",layout=Layout(height='auto', width='auto'),style = {'description_width': 'initial'}) 
    third_page[0,1] = widgets.Dropdown(options = ['SHORt','MEDium','LONG'], value = 'MEDium',description ="INTEGRATION TIME",layout=Layout(height='auto', width='auto'),style = {'description_width': 'initial'})
    third_page[0,2] = widgets.FloatText(0, description = "STRESS DURATION",layout=Layout(height='auto', width='auto'),style = {'description_width': 'initial'})

    # Second Line
    third_page[2,:] = widgets.Label("SWEEP SETUP",layout=Layout(height='auto', width='auto'))

    # Third Line
    third_page[3,0] = widgets.Label("VAR1",layout=Layout(height='auto', width='auto'))
    third_page[3,1] = widgets.Label("VAR2",layout=Layout(height='auto', width='auto'))
    third_page[3,2] = widgets.Label("VAR1'",layout=Layout(height='auto', width='auto'))
    third_page[3,3] = widgets.Label("SMU PULSE",layout=Layout(height='auto', width='auto'))

    # Fourth Line
    third_page[4,0] = widgets.FloatText(0, description = 'START',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[4,1] = widgets.FloatText(0, description = 'START',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[4,2] = widgets.FloatText(description = 'OFFSET',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[4,3] = widgets.FloatText(description = 'PERIOD',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))

    # Fifth line
    third_page[5,0] = widgets.FloatText(5,description = 'STOP',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[5,1] = widgets.FloatText(500e-6, description = 'STOP',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[5,2] = widgets.FloatText(description = 'RATIO',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[5,3] = widgets.FloatText(description = 'WIDTH',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))

    # Sixth Line
    third_page[6,0] = widgets.FloatText(0.1,description = 'STEP',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[6,1] = widgets.FloatText(6,description = 'NO OF STEPS',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    #skip 5,2
    third_page[6,3] = widgets.FloatText(5,description = 'BASE',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))

    #Seventh Line
    third_page[7,0] = widgets.FloatText(100e-3,description = 'COMPLIANCE',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[7,1] = widgets.FloatText(5,description = 'COMPLIANCE',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[7,2] = widgets.FloatText(description = 'COMPLIANCE',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    #no more pulse

    # Eighth Line
    third_page[8,0] = widgets.FloatText(0,description = 'POWER COMP',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[8,1] = widgets.FloatText(0,description = 'POWER COMP',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[8,2] = widgets.FloatText(description = 'POWER COMP',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))

    third_page[9,0] = widgets.Checkbox(False,description = 'HYSTERISIS',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))

    ## Sampling Parameters
    third_page[11,:] = widgets.Label("SAMPLING PARAMETERS")    
    third_page[12,0] = widgets.Dropdown(options=["LINear","L10","L25","L50","THINnedout"], value = 'LINear',description = 'MODE',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[13,0] = widgets.FloatText(1e-3,description = "INITIAL INTERVAL", style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[14,0] = widgets.IntText(1001,description = "NO. OF SAMPLES", style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[15,0] = widgets.FloatText(0,description = "TOTAL SAMP. TIME", style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[16,0] = widgets.FloatText(0,description = "HOLD TIME", style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    third_page[17,0] = widgets.Checkbox(True,description = "FILTER", style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))


    # Set constant SMU for all measurement in case they are not disabled

    third_page[19,:] = widgets.Label("CONSTANT",layout=Layout(height='auto', width='auto'))
    third_page[20,0] = widgets.Label("UNIT",layout=Layout(height='auto', width='auto'))
    third_page[20,1] = widgets.Label("SMU1",layout=Layout(height='auto', width='auto'))
    third_page[20,2] = widgets.Label("SMU2",layout=Layout(height='auto', width='auto'))
    third_page[20,3] = widgets.Label("SMU3",layout=Layout(height='auto', width='auto'))
    third_page[20,4] = widgets.Label("SMU4",layout=Layout(height='auto', width='auto'))

    third_page[21,0] = widgets.Label("SOURCE",layout=Layout(height='auto', width='auto'))
    third_page[22,0] = widgets.Label("COMPLIANCE",layout=Layout(height='auto', width='auto'))

    for i in range(21,23):
        for j in range(1,5):
            third_page[i,j] = widgets.FloatText(layout=Layout(height='auto', width='auto'))    

    return third_page
    

#display variables
def page_4():
    fourth_page = GridspecLayout(5,4)

    #first line headers
    fourth_page[0,1]= widgets.Label('X',layout=Layout(height='auto', width='auto'))
    fourth_page[0,2]= widgets.Label('Y1',layout=Layout(height='auto', width='auto'))
    fourth_page[0,3]= widgets.Label('Y2',layout=Layout(height='auto', width='auto'))

    #first column 
    fourth_page[1,0] = widgets.Label('NAME',layout=Layout(height='auto', width='auto'))
    fourth_page[2,0] = widgets.Label('SCALE',layout=Layout(height='auto', width='auto'))
    fourth_page[3,0] = widgets.Label('MIN',layout=Layout(height='auto', width='auto'))
    fourth_page[4,0] = widgets.Label('MAX',layout=Layout(height='auto', width='auto'))

    #iterate through the second line (NAME)
    fourth_page[1,1]=widgets.Text(layout=Layout(height='auto', width='auto'),value = 'V3')
    fourth_page[1,2]=widgets.Text(layout=Layout(height='auto', width='auto'),value = 'I3')
    fourth_page[1,3]=widgets.Text(layout=Layout(height='auto', width='auto'))

    #Iterate through the third line (scale) 
    options_scale = ['LIN','LOG']
    for j in range(1,4):
        fourth_page[2,j] = widgets.Dropdown(value = 'LIN',options = options_scale, layout=Layout(height='auto', width='auto'),)

    #iterate throuh the last 2 lines(min-max)
    for j in range(1,4):
        fourth_page[3,j] = widgets.FloatText(value = 0) #min

    fourth_page[4,1]=widgets.FloatText(value = 1) #max X-axis
    fourth_page[4,2]=widgets.FloatText(value = 0.1) #max Y1-axis
    fourth_page[4,3]=widgets.FloatText(value = 0) #max Y2-axis

    return fourth_page


def page_5():
    fifth_page = GridspecLayout(14,2)

    fifth_page[0,0] = widgets.Text(description = 'MEASUREMENT NAME',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    fifth_page[1,0] = widgets.Text(description ='PROCESSING NR.',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto')) 
    fifth_page[2,0] = widgets.Text(description ='SAMPLE SERIES',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))
    fifth_page[3,0] = widgets.Text(description ='DUT',style = {'description_width': 'initial'},layout=Layout(height='auto', width='auto'))

    fifth_page[5,0] = widgets.Label("VARIABLES TO SAVE",layout=Layout(height='auto', width='auto'))
    fifth_page[5,1] = widgets.Label("UNIT",layout=Layout(height='auto', width='auto'))
    for i in range(8):
        for j in range(2):
            fifth_page[6+i,j] = widgets.Text(layout=Layout(height='auto', width='auto'))
        
    return fifth_page

#change state off the grid
def change_state(*grids):
    for grid in grids:
        for i in range(grid.n_rows):
            for j in range(grid.n_columns):
                try:
                    grid[i,j].disabled = not grid[i,j].disabled
                except: 
                    pass