
proc SelectOptimizationLevel {} {
    global WDOptLevel InternalWindowing DialogFrame DefPadX DefPadY ActiveDialog 

    if {$ActiveDialog=="Optimization Level"} {return}

    if {$InternalWindowing} {
        set WDOptLevel $DialogFrame.optlevel
        if {![winfo exists $WDOptLevel]} {
            frame $WDOptLevel
        }
        
        RegisterDialog "Optimization Level" $WDOptLevel SelectOptimizationLevel {}
    } else {
        set WDOptLevel .optlevel
        if {![winfo exists $WDOptLevel]} {
            toplevel $WDOptLevel
            wm title $WDOptLevel "Optimization Level"
            wm resizable $WDOptLevel 0 0
        }
    }
    
    frame $WDOptLevel.solve
    frame $WDOptLevel.bb
    checkbutton $WDOptLevel.local \
        -text "Apply Local Search" -variable goblinMethLocal -anchor w
    frame $WDOptLevel.candidates
    pack $WDOptLevel.solve $WDOptLevel.bb $WDOptLevel.candidates $WDOptLevel.local \
        -side top -fill x -padx  $DefPadX -pady 5m

    label $WDOptLevel.solve.label -text "Optimization Level:" -anchor w
    radiobutton $WDOptLevel.solve.heuristic -text "Only Heuristic Solutions" \
        -variable goblinMethSolve -value 0 -anchor w
    radiobutton $WDOptLevel.solve.lowerBound -text "Compute Lower Bounds" \
        -variable goblinMethSolve -value 1 -anchor w
    radiobutton $WDOptLevel.solve.exact -text "Branch and Bound" \
        -variable goblinMethSolve -value 2 -anchor w
    pack $WDOptLevel.solve.label \
        $WDOptLevel.solve.heuristic \
        $WDOptLevel.solve.lowerBound \
        $WDOptLevel.solve.exact \
        -fill x

    label $WDOptLevel.bb.label1 -text "B&B Iterations (1000s):" \
        -anchor w
    grid $WDOptLevel.bb.label1 -in $WDOptLevel.bb  \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $WDOptLevel.bb.iterations -textvariable goblinMaxBBIterations \
        -bg white -width 5
    grid $WDOptLevel.bb.iterations -in $WDOptLevel.bb \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDOptLevel.bb.label2 -text "Active B&B Nodes:" \
        -anchor w
    grid $WDOptLevel.bb.label2 -in $WDOptLevel.bb  \
        -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $WDOptLevel.bb.nodes -textvariable goblinMaxBBNodes \
        -bg white -width 5
    grid $WDOptLevel.bb.nodes -in $WDOptLevel.bb \
        -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    
    radiobutton $WDOptLevel.candidates.disable \
        -text "Do Not Use Candidate Subgraphs" -variable goblinMethCandidates \
        -value -1 -anchor w
    frame $WDOptLevel.candidates.frame
    pack $WDOptLevel.candidates.disable \
        -fill x
    pack $WDOptLevel.candidates.frame -side left -pady 1m
    label $WDOptLevel.candidates.frame.label \
        -text "Minimum Degree in Subgraph:" \
        -anchor w
    grid $WDOptLevel.candidates.frame.label \
        -in $WDOptLevel.candidates.frame \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $WDOptLevel.candidates.frame.entry \
        -textvariable goblinMethCandidates -bg white -width 2
    grid $WDOptLevel.candidates.frame.entry \
        -in $WDOptLevel.candidates.frame \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m


    frame $WDOptLevel.buttons
    button $WDOptLevel.buttons.done -text "Done" -command { \
        if {[QualifiedInt $goblinMethCandidates -1 50000] && \
            [QualifiedInt $goblinMaxBBIterations 1 50000] && \
            [QualifiedInt $goblinMaxBBNodes 1 50000]} {
            if {$InternalWindowing} {
                DestroyActiveDialog
            } else {
                destroy $WDOptLevel
            }
        }
    }
    
    pack $WDOptLevel.buttons.done -side left -fill x -padx 3m
     
    pack $WDOptLevel.buttons -padx 3m -pady 3m -side bottom

    if {$InternalWindowing} {
        pack $WDOptLevel -anchor nw
        AlignActiveDialog
    }
}

if {![info exists WDOptLevel] || ![winfo exists $WDOptLevel]} {SelectOptimizationLevel}
