
require 'GuiState.rb'
require 'StartableThread.rb'




def create_gui_states(top)
    
    states = []

    states << welcome           = GuiState.new("welcome",top)
    states << screen_zero       = SimpleGuiState.new("filter_or_patient",top)
    states << start_prep_diafilt= SimpleGuiState.new("start_prep_diafilt",top)
    states << start_filter_care = SimpleGuiState.new("start_filter_care",top)
    states << filter_prep_progress=SimpleGuiState.new("filter_prep_progress",top)
    states << filter_prep_stopped=SimpleGuiState.new("filter_prep_stopped",top)
    states << filter_data       = SimpleGuiState.new("filter_data",top)
    states << start_prime       = SimpleGuiState.new("start_prime_1",top)
    states << start_prime_2     = SimpleGuiState.new("start_prime_2",top)
    states << prime_progress    = SimpleGuiState.new("prime_progress",top)
    
    
    # ---- Welcome screen ----------------------------------------------------
    
    welcome_text = TkLabel.new(welcome.frame) {
        text "Nephros H2H Module \n Initializing..."
        pack
    }
    welcome_thread = nil
    welcome.on_entry = proc { |state|
        welcome_thread = Thread.new { 
            sleep 1
            state.gen_event("welcome_done") 
        }
    }
    welcome.all_events = [
        [ "welcome_done", proc {|evt,state| 
            welcome_text.configure('text'=>'Nephros H2H Module')
            top.ok_label.configure('text'=>'Hit OK to Continue')
            top.ok_button.configure(
                'command'=>proc{state.gen_event("continue")} )
            nil } ],
        [ "continue", proc{|evt,state| screen_zero} ]
#        [ "continue", proc{|evt,state| filter_data} ]
    ]
    
    
    # ---- Screen zero - choose filter prep or diafiltration -----------------
    
    s = screen_zero
    f = s.frame
    TkLabel.new(screen_zero.frame) {
        image TkPhotoImage.new('file'=> "images/filter_large.gif")
        pack('side'=>'left')    }
    TkLabel.new(screen_zero.frame) {
        image TkPhotoImage.new('file'=> "images/patient_large.gif")
        pack('side'=>'left')    }
    screen_zero.navigation_labels=["Filter Care","","Prepare Diafiltration"]
    screen_zero.next_states=[start_filter_care, nil, start_prep_diafilt]
    
    
    # ---- Start prep for diafiltration --------------------------------------

    s = start_prep_diafilt
    f = s.frame
    s.title = "Start Filter Prep Illustration"
    start_prep_diafilt.navigation_labels=["Back","","Start Filter Prep"]
    start_prep_diafilt.next_states=[screen_zero, nil, nil]
    start_prep_diafilt.forw_action_confirm( "continue" )
    start_prep_diafilt.all_events << [ "continue", proc{ filter_prep_progress } ]



    # ---- Filter Prep In Progress -------------------------------------------
    s = filter_prep_progress    # shortcut to save typing
    f = s.frame
    s.title = 'Filter Prep In Progress'
    p = TkFrame.new(f,'bg'=>'white').pack()
    TkLabel.new(p,'width'=>200).pack
    scale = TkScale.new(p){
        orient 'horizontal'
        showvalue false
        pack('fill'=>'x','expand'=>'on')
    }
    
    # Progress thread will update progress bar
    
    progress_thread       = nil 
    progress_thread_pause = false
    progress              = 0
    s.on_entry = proc {|state|
        if progress_thread == nil           # If entering state 1st time
            progress_thread = Thread.new {  # .. create the thread
                for progress in 1..100
                    sleep 0.1
                    scale.set progress
                    Thread.stop if progress_thread_pause
                end
                state.gen_event("filter_prep_done")
            }
        else                                # If re-entering the state
            progress_thread_pause = false   # just re-activate the thread
            progress_thread.wakeup
        end
    }
    
    s.navigation_labels = ["Stop","" ,"See Filter Data"]
    s.next_states       = [nil   ,nil, filter_data     ]
    s.back_action_confirm( "filter_prep_stop" )
    s.all_events.concat [
        [ "filter_prep_done",   proc {|evt,state| progress_thread       = nil;  start_prime } ],
        [ "filter_prep_stop",   proc {|evt,state| progress_thread_pause = true; filter_prep_stopped} ], 
        [ "STOP",               proc {|evt,state| progress_thread_pause = true; filter_prep_stopped} ], 
        [ "error",              proc {|evt,state| progress_thread_pause = true; filter_prep_stopped} ]  ] 

    
    # ---- filter_prep_stopped -----------------------------------------------
    s = filter_prep_stopped             # shortcut to save typing
    f = s.frame
    f1 = TkFrame.new(f,'width'=>'50p').pack('side'=>'left')
    TkMessage.new(f1, 'text'=>'Filter Prep Not Complete').pack('side'=>'top')
    sc=TkScale.new(f1,'orient'=>'horizontal','showvalue'=>false).pack('fill'=>'x','expand'=>'on')
    TkLabel.new(f) {
        image TkPhotoImage.new('file'=> "images/filter_NOT_large.gif")
        pack 'side'=>'left'     }
    err_1_label = TkMessage.new(f, 'text'=>'').pack('side'=>'left')
    s.navigation_labels=["Startup","","Resume"]
    s.next_states=[screen_zero, nil, filter_prep_progress]
    s.on_entry = proc {
        filter_prep_stopped.title = 
            "Filter Prep Halted by " + ( error_state ? "Fault" : "User" )
        err_1_label.text ( error_state ? error_msg : "" )
        sc.set progress
    }
    s.all_events << ["error_cleared", proc{ filter_prep_progress }]



    # ---- filter_data -------------------------------------------------------
    s = filter_data
    f = s.frame
    s.title = "Filter Data"
    TkMessage.new(f, 'text'=>"4\nTreatments Remaining Today", 'justify'=>'center').pack('side'=>'left')
    TkMessage.new(f, 'text'=>"10\nDays Use Remaining For Filter", 'justify'=>'center').pack('side'=>'left')
    TkMessage.new(f, 'text'=>"18\nHours Before Filter Care", 'justify'=>'center').pack('side'=>'left')
    s.navigation_labels=["","OK",""]
    s.next_states=[nil, filter_prep_progress, nil]
    s.all_events << filter_prep_progress.all_events.assoc("filter_prep_done")



    # ---- start prime -------------------------------------------------------
    s = start_prime                     # shortcut to save typing
    f = s.frame
    s.title = "Setup Dialysate"
    TkLabel.new(f) {
        image TkPhotoImage.new('file'=> "images/prime_dialysate_setup.gif")
        pack('anchor'=>'center')    }
    s.navigation_labels = ["Startup"         , "",  "Next"]
    s.next_states       = [start_prep_diafilt, nil, start_prime_2]
    
    # TODO: If tubing present, disable "Startup" button
    #       After pressing "Next", disable "Startup" button
    #       Add a confirmation screen after "Startup" : "You will have to do filter prep again"
    
    
    # ---- start prime -------------------------------------------------------
    s = start_prime_2                   # shortcut to save typing
    f = s.frame
    s.title = "Continue Setup"
    TkLabel.new(f) {
        image TkPhotoImage.new('file'=> "images/prime_blood_setup.gif")
        pack('anchor'=>'center')    }
    s.navigation_labels = ["Filter Prep", "",  "Start Prime"]
    s.next_states       = [start_prime, nil, prime_progress]
    
    s.back_label2, s.forw_label2 = "Filter Care", "Recirculation"


    # Return the list of all states

    states

end


