import threading
import mapNode
import sys
import mapGlobal
import mouseNode
from time import sleep
from socket import *
from subprocess import call
import coreStartup

# This class is in charge of broadcasting the map to all other mice in the maze
class coreStartupObj(threading.Thread):

    COMMAND_DELAY=5

    #This function broadcasts a copy of the map every WAIT_TO_SEND seconds
    def run(self):
        if self.production==1:
            if self.step==0:
                print("Starting core...")
                core2=coreStartup.coreStartupObj(self.production,self.step+1,self.image_file,self.distance)
                core2.start()
                if self.distance==0:
                    call("core-gui /home/core/code/coreConfig/default_short.xml", shell=True)
                elif self.distance==1:
                    call("core-gui /home/core/code/coreConfig/default_med.xml", shell=True)
                else:
                    call("core-gui /home/core/code/coreConfig/default_long.xml", shell=True)
            else:
                sleep(self.COMMAND_DELAY)
                print("Setting background...")
                call("coresendmsg node number=5 xpos=195 ypos=195", shell=True)
                call("coresendmsg node number=5 icon=/home/core/code/mazeFiles/"+self.image_file, shell=True)
                sleep(self.COMMAND_DELAY)
                print("Starting node 1...")
                call("coresendmsg exec node=1 num=1001 cmd='python /home/core/code/WSNTRENT/WSN_MM/MICROMOUSE/combo/mouse.py 1'", shell=True)
                call("coresendmsg node number=1 icon=/home/core/code/coreConfig/car.png", shell=True)
                sleep(self.COMMAND_DELAY)
                print("Starting node 2...")
                call("coresendmsg exec node=2 num=1001 cmd='python /home/core/code/WSNTRENT/WSN_MM/MICROMOUSE/combo/mouse.py 1'", shell=True)
                call("coresendmsg node number=2 icon=/home/core/code/coreConfig/car.png", shell=True)
                sleep(self.COMMAND_DELAY)
                print("Starting node 3...")
                call("coresendmsg exec node=3 num=1001 cmd='python /home/core/code/WSNTRENT/WSN_MM/MICROMOUSE/combo/mouse.py 1'", shell=True)
                call("coresendmsg node number=3 icon=/home/core/code/coreConfig/car.png", shell=True)
                sleep(self.COMMAND_DELAY)
                print("Starting node 4...")
                call("coresendmsg exec node=4 num=1001 cmd='python /home/core/code/WSNTRENT/WSN_MM/MICROMOUSE/combo/mouse.py 1'", shell=True)
                call("coresendmsg node number=4 icon=/home/core/code/coreConfig/car.png", shell=True)




    def __init__(self,production,step,image_file,distance):
        threading.Thread.__init__(self)
        self.production=production
        self.step=step
        self.distance=distance
        self.image_file=image_file

    
    