import javax.swing.JSlider as JS
import java.awt.event.ItemEvent as IE
import java.awt.event.ActionListener as AL
import javax.swing.event.ChangeListener as CL
import java.awt.event.ItemListener as IL

//*
//automator script for plurifocal TWS
//
//plurifocal
//		you can use sendMSG(node)
//nodes
//		it is an array of nodes with
//		location.x
//		location.x
//		location.x
//		value
//		active
//		id
//		netID
//		range
//		parameters
//serial
//		you can use serialOut(String)
//active
//		it is a checkbox 
//sliders
//		it is an array of JSlider
//buttons
//		it is an array of JButton
//
//
//*


//GLOBALS
isActive = active.isSelected()
valSlider00 = 0
valSlider01 = 0
valSlider02 = 0
valSlider03 = 0

//ACTIONS
void allToValue(val){
	println " all by one"
	Thread.start{
		nodes.each{
			it.value = val;
			plurifocal.sendMSG(it)
			sleep(20)
		}
	}
}

void zeroToCurrent(){
	def duration = valSlider02 * 20 + 1.0
	def resolution = 4 //units per second
	def ticks = (int)(duration * resolution)
	def sleepTime = (int)(1000 / resolution)
	def counter = 0
	def currentNodeValues = nodes.collect{it.value}
	Thread.start{
		while(counter++ < ticks){
			nodes.eachWithIndex{nd,i ->
				nd.value = counter / ticks * currentNodeValues[i]
				plurifocal.sendMSG(nd)
			}
			sleep(sleepTime)
		}
	}
}

void currentToZero(){
	def duration = valSlider02 * 20 + 1.0
	def resolution = 4 //units per second
	def ticks = (int)(duration * resolution)
	def sleepTime = (int)(1000 / resolution)
	def counter = ticks
	def currentNodeValues = nodes.collect{it.value}
	Thread.start{
		while(counter-- > 0){
			nodes.eachWithIndex{nd,i ->
				nd.value = counter / ticks * currentNodeValues[i]
				plurifocal.sendMSG(nd)
			}
			sleep(sleepTime)
		}
	}
}

void randomSequencer(){
	def rnd = new Random()
	def currentNodesPlaying = nodes.findAll{nd -> nd.value > 0}
	def currentNodesValues = currentNodesPlaying.collect{it.value}
	def duration = valSlider02 * 59 + 1.0
	def oscilation = valSlider00 * 4 + 1.0
	def currentTime = 0
	Thread.start{
		while(currentTime < duration){
			if(rnd.nextBoolean()){
				//set one of the playing to zero
				def ind1 = rnd.nextInt(currentNodesPlaying.size)
				currentNodesPlaying[ind1].value = 0
				plurifocal.sendMSG(currentNodesPlaying[ind1])
			}
		
			else {
				//set one of the playing to init value
				def ind2 = rnd.nextInt(currentNodesPlaying.size)
				currentNodesPlaying[ind2].value = currentNodesValues[ind2]
				plurifocal.sendMSG(currentNodesPlaying[ind2])
			}
			
			currentTime+= oscilation
			sleep((int)(oscilation * 1000))
		}
	}
}

//LISTENERS
listenerActive = {sc -> 
	if(sc.getStateChange() == IE.SELECTED){
		println "now active"
		isActive=true
	}
	else {
		println "now not active"
		isActive=false
	}
}

listenerB0 = {ap ->
	println "b0 pressed"
	zeroToCurrent()
}

listenerB1 = {ap ->
	println "b1 pressed"
	currentToZero()
}

listenerB2 = {ap ->
	println "b2 pressed"
	randomSequencer()
}

listenerB3 = {ap ->
	println "b3 pressed"
}

listenerB4 = {ap ->
	println "b4 pressed"
}

listenerB5 = {ap ->
	println "b5 pressed"
}

listenerB6 = {ap ->
	println "b6 pressed"
}

listenerB7 = {ap ->
	println "b7 pressed"
}

listenerS0 = {sc ->
	JS source = (JS) sc.getSource();
	if(!source.getValueIsAdjusting()){
		println("s0 " + source.getValue() / 1000.0)
		valSlider00 = source.getValue() / 1000.0
	}
}

listenerS1 = {sc ->
	JS source = (JS) sc.getSource();
	if(!source.getValueIsAdjusting()){
		println("s1 " + source.getValue() / 1000.0)
		valSlider01 = source.getValue() / 1000.0
	}
}

listenerS2 = {sc ->
	JS source = (JS) sc.getSource();
	if(!source.getValueIsAdjusting()){
		println("s2 " + source.getValue() / 1000.0)
		valSlider02 = source.getValue() / 1000.0
	}
}

listenerS3 = {sc ->
	JS source = (JS) sc.getSource();
	if(!source.getValueIsAdjusting()){
		println("s3 " + source.getValue() / 1000.0)
		valSlider03 = source.getValue() / 1000.0
		allToValue(valSlider03)
	}
}

active.itemStateChanged = listenerActive
buttons[0].actionPerformed = listenerB0
buttons[1].actionPerformed = listenerB1
buttons[2].actionPerformed = listenerB2
buttons[3].actionPerformed = listenerB3
buttons[4].actionPerformed = listenerB4
buttons[5].actionPerformed = listenerB5
buttons[6].actionPerformed = listenerB6
buttons[7].actionPerformed = listenerB7

sliders[0].stateChanged = listenerS0
sliders[1].stateChanged = listenerS1
sliders[2].stateChanged = listenerS2
sliders[3].stateChanged = listenerS3

void kill(){
	active.removeItemListener(listenerActive as IL)
	buttons[0].removeActionListener(listenerB0 as AL)
	buttons[1].removeActionListener(listenerB1 as AL)
	buttons[2].removeActionListener(listenerB2 as AL)
	buttons[3].removeActionListener(listenerB3 as AL)
	buttons[4].removeActionListener(listenerB4 as AL)
	buttons[5].removeActionListener(listenerB5 as AL)
	buttons[6].removeActionListener(listenerB6 as AL)
	buttons[7].removeActionListener(listenerB7 as AL)
	sliders[0].removeChangeListener(listenerS0 as CL)
	sliders[1].removeChangeListener(listenerS1 as CL)
	sliders[2].removeChangeListener(listenerS2 as CL)
	sliders[3].removeChangeListener(listenerS3 as CL)
}

