'''Recieves an alert from the alert server and shows the alert to the user.'''
from Tkinter import *
import client, time, sys

try: host=sys.argv[1] #make sure it exsists
except IndexError:
    host = raw_input("What is the IP address of the alert server? ") #Input it manually

try: port=int(sys.argv[2]) #convert the first argument following the server to int
except IndexError:
    while True:
        try: port = int(raw_input("What port is the alert server on? ")) #Input it manually
        except ValueError: print "Sorry but that's not an interger. Try again."
        else: break #once we got it error free, we're done here
except ValueError:
    raise ValueError, "%s isn't an interger. Aborting now." % sys.argv[2] #If there's an error, am doing it my way

recvier = client.Client(host=host, port=port)#we got that above
while True:
    recvier.send("Info") #ask the alert server
    recvied=recvier.recv() #get an alert
    if recvied != 'None': #'None' means there are no new messages
        #now to make a pop up window
        root=Tk() #make the window and the title
        root.title('Alert!')
        
        w = root.winfo_screenwidth() #put it in the middle and make it 200x100
        h = root.winfo_screenheight()
        x = w/2 - 100
        y = h/2 - 50
        root.geometry("200x100+%d+%d" % (x, y))

        root.state("normal") #make it restored, in front, and flashing
        root.wm_attributes("-topmost", 1)
        
        frame = Frame(root) #make a frame
        frame.grid()

        Label(frame, text=recvied[7:], anchor=N).grid() #Every message starts with "Alert: ", so I have to take that out
        mainloop()
   
    time.sleep(.01)#we don't want to over load it by going to fast
