"""Subclass of HubSimulFrame, which is generated by wxFormBuilder."""

import wx
import array
import time
import threading

from hubsimul import HubSimul
from hubsimulserver import HubSimulServer

from hubsimulframe import  HubSimulFrame
from bitmap import Bitmap
from hubprotocol import HubProtocol,ParserFactory


# Implementing HubSimulFrame
class HubSimulControl(HubSimulFrame ):
    HubCount = 42
    
    
    def __init__( self, parent):
        HubSimulFrame.__init__( self, parent )
        
        
        panel = self.panelRawBitmap
        sizer = panel.GetSizer()    
        self.bitmap = Bitmap(panel,Bitmap.width,Bitmap.height) 
        sizer.Add( self.bitmap, 1, wx.EXPAND )
        
        # start listeneing on port 1234
        factory = ParserFactory()
        server = HubSimulServer(factory)
        self.protocol = HubProtocol(server)
        self.protocol.addListener(self)


        self.alive = threading.Event()
        self.event = None

        self.hubs = []

        for i in range (HubSimulControl.HubCount):
            hub = HubSimul(i,self.protocol)
            self.hubs.append(hub)
                    
        self.startThread()

        
    def __del__( self ):
        print "destructor ..."
        self.server.shutdown()
        self.server = None

    def handleCommand(self, opcode,payload):    

        if opcode in [HubProtocol.POS_FRAME,HubProtocol.PLAY_FRAME,HubProtocol.LED_FRAME]:
            wx.CallAfter(self.updateBitmapInGuiThread,opcode,payload)
        else:    
            for i in range (HubSimulControl.HubCount):
                hub = self.hubs[i]
                hub.handleCommand(opcode,payload)
            
            
    def updateBitmapInGuiThread(self,command,payload):  
        self.bitmap.fromCompressedBuffer(command,payload)
        self.Refresh()
            
        
    def startThread(self):
        
        """Start the wave  thread"""        
        self.thread = threading.Thread(target=self.TribuneWaveThread)
        self.thread.setDaemon(1)
        self.alive.set()
        self.thread.start()

    def stopThread(self):
        """Stop the receiver thread, wait util it's finished."""
        print "Stop thread ..."
        self.alive.clear()          #clear alive event for thread
        if hasattr(self, 'thread') and self.thread != None:
            self.thread.join()          #wait until thread has finished
            self.thread = None
        print "Stop thread done ..."
        
    def TribuneWaveThread(self):
        print "Starting thread TribuneWaveThread"
        while(True):
            time.sleep(1)

        

            