#SCRIPT:  /home/jbf/ct/hudson/script/test071//testJunoPolarPlotEvents.jy
setScriptDescription('Uses Java Robot to control the mouse to simulate human interaction')

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)
from org.das2.util import CredentialsManager
cm = CredentialsManager.getMannager();
sLocId = 'http://jupiter.physics.uiowa.edu/das/server|Juno Magnetospheric Working Group'
def readPass(f):
    o= open(f,'r')
    s= o.readlines()
    return s[0].strip()
junoPass= readPass( '/home/jbf/ct/hudson/script/test071/junoPass.txt' )
cm.setHttpBasicHashRaw( sLocId, junoPass ) 

app= getViewWindow()
mainFrame = JFrameOperator(app)

uri= "http://www-pw.physics.uiowa.edu/~jbf/juno/u/bill/20170224/junoPolarPlotEvents.jy?pole='N'&orbitNumber=6&param='B'&freqs=''&reduce='F'&rlim=10"
JTextFieldOperator( app.getDataSetSelector().getEditor() ).setText(uri)
JButtonOperator( app.getDataSetSelector().getGoButton() ).push()

sleeping(1000,'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(20000, 'load data') # wait for spectrogram to load.
waitUntilIdle()

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 }

safecount=100
while ( safecount>0 and dom.plotElements[0].controller.renderer.dataSet==None ):
   safecount= safecount-1
   sleep(100)
   print '  more sleep '+str(safecount)

while ( safecount>0 and dom.plotElements[1].controller.renderer.dataSet==None ):
   safecount= safecount-1
   sleep(100)
   print '  more sleep '+str(safecount)

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



sleeping(20000,'get ready to click and drag')

r= Robot()

for i in xrange(100):
    xx= int( canvasPos['x']+100 + (100-i) )
    yy= int( canvasPos['y']+100 + 2*(100-i ))
    r.mouseMove(xx,yy)
    sleep(10)
    
r.mouseMove(canvasPos['x']+100,canvasPos['y']+100)
sleep(500)
r.mousePress(InputEvent.BUTTON1_MASK)
sleep(500)
count=3
#while ( dom.plots[0].controller.dasPlot.getDasMouseInputAdapter().getActive()==None and count>0 ):
#    print 'huh line77'
#    r.mouseRelease(InputEvent.BUTTON1_MASK)
#    sleep(500)
#    r.mousePress(InputEvent.BUTTON1_MASK)
#    sleep(500)
#    count= count-1
for i in xrange(0,100,5):
   r.mouseMove(canvasPos['x']+100+i,canvasPos['y']+100)
   sleep(10)
sleep(100)
r.mouseRelease(InputEvent.BUTTON1_MASK)

sleeping(1000,'get ready for options OK')

try:
   frame = DialogOperator( RegexComponentChooser( "options") )
   JButtonOperator( frame, "OK" ).push()
except org.netbeans.jemmy.TimeoutExpiredException, e: 
   print 'fail at line 76'

waitUntilIdle()

r.mouseMove(canvasPos['x']+400,canvasPos['y']+400)
sleep(100)
r.mousePress(InputEvent.BUTTON1_MASK)
sleep(100)

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')
writeToPng('testJunoPolarPlotEvents.png')

sleep(100)
r.mouseRelease(InputEvent.BUTTON1_MASK)

print 'okay'
sleeping(1000)

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