
set APSVIPEmulationOn 0
set APSVIPEmulationAbort 0
set APSVIPEmulatedCurrent 0
set APSVIPEmulationFilling 0
set APSVIPEmulationTopup 0

proc FillForVIP {args} {
    global APSVIPEmulationOn  APSVIPEmulationAbort APSVIPEmulationFilling APSVIPEmulatedCurrent APSVIPEmulationTopup
    APSDisableWidget .vip.userFrame.bot.frame
    .vip.userFrame.bot.frame.fill.button configure -bg red
    .vip.userFrame.bot.frame.stop.button configure -bg green
    .vip.userFrame.bot.frame.dump.button configure -bg green
    .vip.userFrame.bot.frame.topup.button configure -bg red
    APSEnableWidget .vip.userFrame.bot.frame
    if $APSVIPEmulationFilling return
    bell
    set APSVIPEmulationAbort 0
    if $APSVIPEmulationOn {
        # pretend to fill
        set APSVIPEmulationFilling 1
        set APSVIPEmulationTopup 0
        after 250
        update
        pv getw APSVIPEmulatedCurrent 
        set currentLimit 100
        while {$APSVIPEmulatedCurrent<$currentLimit} {
            if $APSVIPEmulationAbort break
            set APSVIPEmulatedCurrent [expr $APSVIPEmulatedCurrent*0.9997 + 1 + (0.5-rand())/10.0]
            pv putw APSVIPEmulatedCurrent
            update
            after 250
        }
        .vip.userFrame.bot.frame.fill.button configure -bg green
        .vip.userFrame.bot.frame.stop.button configure -bg red
        .vip.userFrame.bot.frame.dump.button configure -bg red
        .vip.userFrame.bot.frame.topup.button configure -bg green

        set APSVIPEmulationFilling 0
        APSVIPEmulationDoDamping
    } else {
        # do fill
        global currentStop cycles multiplicity multiplets multipletStart multipletSpacing
        global bpmGroupMode bpmGroupBuckets bpmGroupCurrent bpmGroupSum bpmGroupSumUse
        global IOCStopMode softwareMode pauseAfterBPMGroup gapLimit
        if [pv getw S35BeamCurrent] {
            return -code error "Problem reading beam current"
        }
        if $S35BeamCurrent<1 {
            SetPreset -name 1+22S
            set APSVIPEmulationFilling 1
            RunTimingModeInjection \
              -stop $currentStop -cycles $cycles -multiplicity $multiplicity \
              -multiplets $multiplets -multipletStart $multipletStart -multipletSpacing $multipletSpacing \
              -bpmGroupMode $bpmGroupMode -bpmGroupBuckets $bpmGroupBuckets \
              -bpmGroupCurrent $bpmGroupCurrent \
              -bpmGroupSum $bpmGroupSum -bpmGroupSumUse $bpmGroupSumUse \
              -stopMode $IOCStopMode \
              -softwareMode $softwareMode -pauseAfterBPMGroup 0 \
              -gapLimit $gapLimit -noChecks 1
            set APSVIPEmulationFilling 0
        } else {
            SetPreset -name 1+22STopup
            set APSVIPEmulationFilling 1
            RunTimingModeInjection \
              -stop $currentStop -cycles $cycles -multiplicity $multiplicity \
              -multiplets $multiplets -multipletStart $multipletStart -multipletSpacing $multipletSpacing \
              -bpmGroupMode $bpmGroupMode -bpmGroupBuckets $bpmGroupBuckets \
              -bpmGroupCurrent $bpmGroupCurrent \
              -bpmGroupSum $bpmGroupSum -bpmGroupSumUse $bpmGroupSumUse \
              -stopMode $IOCStopMode \
              -softwareMode $softwareMode -pauseAfterBPMGroup 0 \
              -gapLimit $gapLimit -noChecks 1
            set APSVIPEmulationFilling 0
        }
    }
}

proc TopUpForVIP {args} {
    global APSVIPEmulationOn  APSVIPEmulationAbort APSVIPEmulationFilling APSVIPEmulatedCurrent APSVIPEmulationTopup
    APSDisableWidget .vip.userFrame.bot.frame
    .vip.userFrame.bot.frame.fill.button configure -bg red
    .vip.userFrame.bot.frame.stop.button configure -bg green
    .vip.userFrame.bot.frame.dump.button configure -bg green
    .vip.userFrame.bot.frame.topup.button configure -bg red
    APSEnableWidget .vip.userFrame.bot.frame
    if $APSVIPEmulationFilling return
    bell
    set APSVIPEmulationAbort 0
    if $APSVIPEmulationOn {
        # pretend to fill
        set APSVIPEmulationFilling 1
        set APSVIPEmulationTopup 1
        after 250
        update
        pv getw APSVIPEmulatedCurrent 
        set currentLimit 100
        while {$APSVIPEmulatedCurrent<$currentLimit} {
            if $APSVIPEmulationAbort break
            set APSVIPEmulatedCurrent [expr $APSVIPEmulatedCurrent*0.9997 + 1 + (0.5-rand())/10.0]
            pv putw APSVIPEmulatedCurrent
            update
            after 250
        }

        set APSVIPEmulationFilling 0
        APSVIPEmulationDoDamping
    } else {
        APSSetVarAndUpdate status "This button is only used for VIP emulations"
        bell
    }
}

proc StopForVIP {args} {
    global APSVIPEmulationOn APSVIPEmulationAbort APSVIPEmulationTopup
    APSDisableWidget .vip.userFrame.bot.frame
    APSEnableWidget .vip.userFrame.bot.frame
    bell
    if $APSVIPEmulationOn {
        # pretend to stop fill
        set APSVIPEmulationAbort 1
        set APSVIPEmulationTopup 0
        .vip.userFrame.bot.frame.fill.button configure -bg green
        .vip.userFrame.bot.frame.stop.button configure -bg red
        .vip.userFrame.bot.frame.dump.button configure -bg red
        .vip.userFrame.bot.frame.topup.button configure -bg green

    } else {
        # stop fill
        AbortInjection
    }
}

proc DumpForVIP {args} {
    global APSVIPEmulationOn
    StopForVIP
    APSDisableWidget .vip.userFrame.bot.frame
    update
    after 1000
    .vip.userFrame.bot.frame.fill.button configure -bg green
    .vip.userFrame.bot.frame.topup.button configure -bg green
    .vip.userFrame.bot.frame.stop.button configure -bg red
    .vip.userFrame.bot.frame.dump.button configure -bg red
    APSEnableWidget .vip.userFrame.bot.frame
    bell
    if $APSVIPEmulationOn {
        # pretend to dump fill
        set APSVIPEmulatedCurrent 0
        exec cavput -list=PTB:PH1:SetpointAO=0
    } else {
        # dump fill
        DumpBeam
    }
}

proc APSVIPEmulationDoDamping {} {
    global APSVIPEmulatedCurrent APSVIPEmulationFilling APSVIPEmulatedCurrent APSVIPEmulationTopup
    if $APSVIPEmulationFilling return
    pv getw APSVIPEmulatedCurrent
    set APSVIPEmulatedCurrent [expr $APSVIPEmulatedCurrent*0.9997]
    pv putw APSVIPEmulatedCurrent
    if {$APSVIPEmulationTopup} {
        if {$APSVIPEmulatedCurrent < 98} {
            set APSVIPEmulatedCurrent 100
            pv putw APSVIPEmulatedCurrent
        }
    }
    after 250 APSVIPEmulationDoDamping
}


proc APSMakeVIPFillInterface {args} {
    set emulation 0
    APSStrictParseArguments {emulation}

    global APSVIPEmulationOn
    set APSVIPEmulationOn $emulation

    
    if $emulation {
        global APSVIPEmulatedCurrent
        APSApplication .vip -name "Advanced Photon Source filling tool" 
        pv linkw APSVIPEmulatedCurrent PTB:PH1:SetpointAO
        APSVIPEmulationDoDamping
    } else {
        APSApplication .vip -name "Advanced Photon Source Filling Tool" 
    }

    APSMenubarAddMenu .options -parent .vip.menu -text Displays
    .vip.menu.options.menu add command -label "Logger" -command \
      "VIPDemoLoggerAndDisplay -emulation $emulation"
    
    set topLevel .vip.userFrame

    APSFrame .top -parent $topLevel -relief flat
    label $topLevel.top.frame.banner1 -text "Advanced Photon Source" \
      -font "Times 32"
    pack ${topLevel}.top.frame.banner1 
    label ${topLevel}.top.frame.banner2 -text "Argonne National Laboratory" \
      -font "Times 32"
    pack ${topLevel}.top.frame.banner2

    APSFrame .bot -parent ${topLevel}

    APSButton .fill -parent ${topLevel}.bot.frame \
      -text "Fill" -command "FillForVIP" \
      -packOption "-side left"
    ${topLevel}.bot.frame.fill.button configure \
      -font "Times 45" -width 5 \
      -height 1 -bg green

    APSButton .topup -parent ${topLevel}.bot.frame \
      -text "TopUp" -command "TopUpForVIP" \
      -packOption "-side left"
    ${topLevel}.bot.frame.topup.button configure \
      -font "Times 45" \
      -width  6 -height 1 -bg green

    APSButton .stop -parent ${topLevel}.bot.frame \
      -text "Stop" -command "StopForVIP" \
      -packOption "-side left"
    ${topLevel}.bot.frame.stop.button configure \
      -font "Times 45" \
      -width 5 -height 1 -bg red 

    APSButton .dump -parent ${topLevel}.bot.frame \
      -text "Dump" -command "DumpForVIP" \
      -packOption "-side left"
    ${topLevel}.bot.frame.dump.button configure \
      -font "Times 45" \
      -width  5 -height 1 -bg red


}

proc VIPDemoLoggerAndDisplay {args} {
    set emulation 0
    APSStrictParseArguments {emulation}

    if $emulation {
        set monitorFile /home/helios/oagData/VIPDemoFiles/VIPDemoEmulation.mon 
    } else {
        set monitorFile /home/helios/oagData/VIPDemoFiles/VIPDemoReal.mon 
    }
    set tmpFile /tmp/[APSTmpString]
    set loggerPID [exec sddslogger $monitorFile -sampleinterval=0.5,s -time=2,hour $tmpFile &]
    after 1000
    dp_atexit append "exec kill -9 $loggerPID"
    set plotterPID [exec sddsplot "-device=motif,-movie true" -repeat \
                      -column=Time,BeamCurrent -graph=impulse,thick=3,type=2 \
                      -clip=0,250,invert -scale=-125,5,-2.5,110 -mode=x=eoffset \
                      "-topline=Advanced Photon Source" "-title=Operations Analysis Group" \
                      $tmpFile &]
    dp_atexit append "exec kill -9 $plotterPID"
}
