#!/usr/bin/python
from __future__ import print_function
import time,sys,string,serial.tools.list_ports
from threading import Thread
if sys.version_info.major < 3:
        import gtk
        import appindicator
else:
        from gi.repository import Gtk as gtk
        from gi.repository import AppIndicator3 as appindicator
from subprocess import call


class ThreadedLauncher(Thread):
	def __init__(self,args):
		self.args=args
		Thread.__init__(self)
	def run(self):
		call(self.args)


class messenger():
	def __init__(self):
		self.ind = appindicator.Indicator("SEELablet-indicator", "SEELablet-menu", appindicator.CATEGORY_APPLICATION_STATUS)
		#self.ind.set_status(appindicator.STATUS_ATTENTION)
		self.ind.set_status(appindicator.STATUS_ACTIVE)
		self.ind.set_attention_icon("system-devices-panel-alert")
		self.ind.set_icon("system-devices-panel-information")
		self.ind.set_label("SEEL")

		self.menu_setup()
		self.ind.set_menu(self.menu)
		self.launched_threads = []

	def menu_setup(self):
		self.menu = gtk.Menu()
		
		self.menu_entries=[]
		self.menu_group=None
		#self.devmenu = gtk.MenuItem('Devices')
		#self.devmenu.show()
		
		#self.devlist = gtk.Menu()
		
		#self.devmenu.set_submenu(self.devlist)
		#self.menu.append(self.devmenu)

		self.__addsep__()

		self.scope_menu = gtk.MenuItem(label="_Experiments_" , use_underline=True)
		self.scope_menu.connect("activate", self.launch_experiments)
		self.scope_menu.show()
		self.menu.append(self.scope_menu)

		self.__addsep__()

		#add quit button
		self.quit_item = gtk.MenuItem(label="_Quit_" , use_underline=True)
		self.quit_item.connect("activate", self.quit)
		self.quit_item.show()
		self.menu.append(self.quit_item)
		

	def __addsep__(self):
		#add separator
		sep = gtk.SeparatorMenuItem()
		sep.show()
		self.menu.append(sep)		#add a separator

	def launch_experiments(self,widget):
		if self.menu_group:
			for a in self.menu_group.get_group():
				if a.get_active():
					L = a.get_label()
					T=ThreadedLauncher(["Experiments", "-P",L])
					T.start()
					self.launched_threads.append(T)
	def main(self):
		gtk.timeout_add(500, self.check_for_devices)   #check every 500ms
		gtk.main()

		
	def check_for_devices(self):
		#print ('check msg')
		L = serial.tools.list_ports.comports()
		shortlist=[]
		for a in L:
			if ('ACM' in a[1]) and ('04d8:00df' in a[2]):
				shortlist.append(a)

		total = len(shortlist)
		for a in self.menu_entries[total:]:
			self.menu.remove(a)
		self.menu_entries = self.menu_entries[:total]
		
		for a in range(total-len(self.menu_entries)):
			rp=gtk.RadioMenuItem( label='assas',group=self.menu_group)
			if self.menu_group==None:rp.set_active(True)
			self.menu_group = rp
			#menu_item.show()

			rp.show()
			self.menu.insert(rp,0)
			self.menu_entries.append(rp)

		for a in range(total):
			self.menu_entries[a].set_label(shortlist[a][0])
			#self.menu_entries[a].handler_disconnect(0)
			#self.menu_entries[a].connect("activate", self.device_selected, a)


		return True

	def device_selected(self,devnum,num):
		print (devnum,num)

	def quit(self,widget):
		print ('bye bye')
		sys.exit(0)


	
		
A=messenger()
A.main()

