import time, sound, threading, stoppableThread

def soundThread():
	sound.play_effect('arcade:Coin_5')
	time.sleep(.3)
	sound.play_effect('arcade:Coin_5')

def playSound():
	th = threading.Thread(target=soundThread)
	th.start()

def setText(label, textIn):
	stringIn = str(textIn)
	try:
		label.text = stringIn
	except:
		pass
	try:
		label.title = stringIn
	except:
		pass
		
def countdownThread(timeIn, label):
	currentTime = timeIn
	
	while currentTime > 0:
		if label.on_screen and not threading.current_thread().stop:
			setText(label, currentTime)
			time.sleep(1)
			currentTime -= 1
		else:
			break
	
	if label.on_screen and not threading.current_thread().stop:
		setText(label, currentTime)	
		playSound()
		
		setText(label, timeIn)

def countdown(timeIn, label, threadPool):
	th = stoppableThread.StoppableThread(countdownThread, timeIn, label)
	threadPool.append(th)
	th.start()
		

#if __name__ == '__main__':
#	countdown(timeIn=10, label=None)
