print '*** testJunoPolarPlotEvents ***'
#/usr/local/jdk1.8/bin/java -Djava.awt.headless=true -cp /home/jbf/local/autoplot/autoplot.jar org.autoplot.AutoplotUI --testPngFilename=test_071_001 --script=/home/jbf/ct/hudson/script/test071//testJunoPolarPlotEvents.jy
from org.netbeans.jemmy.operators import JFrameOperator, JLabelOperator, JTextFieldOperator, JButtonOperator, DialogOperator
import org.netbeans.jemmy.TimeoutExpiredException
from org.netbeans.jemmy import QueueTool
from org.autoplot.util.jemmy import RegexComponentChooser
from org.autoplot import AutoplotUI

from org.das2.util.filesystem import KeyChain

from java.io import File
print 'write file to ', File('.').getAbsolutePath()

def sleeping(d,msg=''):
   setStatus( 'busy: sleeping...%d %s' % ( d, msg ) )
   for i in xrange(d/100):
      sleep(100)
      setStatus( 'busy: sleeping...%d %s' % (d-i*100,msg) )

sleep(100)
def readPass(f):
    o= open(f,'r')
    s= o.readlines()
    return s[0].strip()

from org.das2.util import CredentialsManager
cm = CredentialsManager.getMannager();
junoPass= readPass( '/home/jbf/ct/hudson/script/test071/junoPass.txt' )

sLocId = 'http://jupiter.physics.uiowa.edu/das/server|Juno Magnetospheric Working Group'
cm.setHttpBasicHashRaw( sLocId, junoPass ) 

sLocId = 'https://jupiter.physics.uiowa.edu/das/server|Juno Magnetospheric Working Group'
cm.setHttpBasicHashRaw( sLocId, junoPass ) 

app= getViewWindow()
mainFrame = JFrameOperator(app)

#uri= "https://abbith.physics.uiowa.edu/jbf/juno/-/blob/master/team/wigglePlot/wigglePlotSapClipFast.jy?orbit=06"
uri= "https://research-git.uiowa.edu/abbith/juno/-/blob/main/team/wigglePlot/wigglePlotSapClipFast.jy?orbit=06"
JTextFieldOperator( app.getDataSetSelector().getEditor() ).setText(uri)
JButtonOperator( app.getDataSetSelector().getGoButton() ).push()

sleeping(10000,'init script')

waitUntilIdle()

print 'initial canvas dimensions: %d,%d' % ( dom.canvases[0].getWidth(), dom.canvases[0].getHeight() )
c= dom.canvases[0].controller.dasCanvas
print 'initial dasCanvas dimensions: %d,%d' % (  c.getWidth(), c.getHeight() )

frame = DialogOperator( RegexComponentChooser( "Run Script.*") )
QueueTool().waitEmpty(100)

JButtonOperator( frame, "OK" ).push()

sleeping(1000, 'run script')
waitUntilIdle()

sleeping(17000, 'load data') # wait for spectrogram to load.
waitUntilIdle()

setCanvasSize( 724,656 )

from java.awt import Robot
from java.awt.event import InputEvent

c= dom.canvases[0].controller.dasCanvas
canvasPos= { 'x':c.getLocationOnScreen().x, 'y':c.getLocationOnScreen().y }

print canvasPos
print 'pe0: ', dom.plotElements[0].controller.renderer.dataSet

count=5
while count>0 and len(dom.plotElements)<2:
    sleeping( 2000, 'wait more for two elements' )    
    count= count-1

if len(dom.plotElements)<2:
    raise Exception('expected two plot elements')
    
print 'pe1: ', dom.plotElements[1].controller.renderer.dataSet
   
sleeping(24000,'get ready to click and drag')

waitUntilIdle()

c= dom.canvases[0].controller.dasCanvas
width= c.getWidth()
height= c.getHeight()
print 'write canvas to image: w,h=%d,%d' % ( width,height )
#dom.canvases[0].controller.dasCanvas.writeToPng('testJunoPolarPlotEvents.png')

print 'okay'

writeToPng('testWigglePlot.png')

from java.lang import System
System.exit(0)