#!/usr/bin/env python
# -*- coding: utf8 -*-

#    This file is part of CatchX.
#
#    CatchX is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    CatchX is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with CatchX.  If not, see <http://www.gnu.org/licenses/>.


#Import stuff
import pygtk
pygtk.require('2.0')
import gtk
import function
import thread
import time

gtk.gdk.threads_init()

#Set a few needed variables
version = 0.15
name = "CatchX" #Just if we have to change the name for somewhat reason
threads = [] #The threads
port = 20211
#The code
print "Starting " + name + " v" + str(version)

#Now we ask the user whether he wants to connect to an exisiting server or if he wants to create one
class catchx:
	def __init__(self):
		def start_server(widget, data=None):                 #DEPRECATED!
			#print "button %s was pressed" % data
			#thread.start_new_thread(server.server, (port,)) #The server runs in a new thread...
			#thread = server.gserver()
			#threads.append(thread)
			#thread.start() 
			#thread.run()
			#time.sleep(0) #Thanks Waldteufel :)
			return True

		def connect_server(widget, data):
			
			def changebuttonstate(widget):
				if adressentry.get_text() <> "" and passentry.get_text() <> "" and nameentry.get_text() <> "" and roomentry.get_text() <> "":
					connectbutton.set_sensitive(True)
				else:
					connectbutton.set_sensitive(False)
					
			def sendinit(widget):
				server = adressentry.get_text()
				password = passentry.get_text()
				nickname = nameentry.get_text()
				room = roomentry.get_text()
				s = function.init_connection(server)
			#	if s == False:
				#	label.set_text("Couldn't connect to the server!")
				##	thread.start_new_thread(function.blink_window, (window,))
				cliwindow.remove(loginbox)
				infobox = gtk.VBox(False, 0)
				cliwindow.add(infobox)
				label = gtk.Label(str)
				label.set_text("Loading game data...")
				infobox.add(label)
				cliwindow.show_all()
				gdata = function.get_gamedata(s, password, nickname, room, cliwindow)
				if gdata == "err_running":
					label.set_text("Game is already running")
				elif gdata == "err_login":
					label.set_text("Wrong password or the room doesn't exist")
				else:
					cliwindow.remove(infobox)
					kret = thread.start_new_thread(function.keepalive, (s,gdata[0]))
					#kret = function.keepalive(s, gdata[0])
					time.sleep(0)
					#function.load_mappoints()
					function.load_maingui(cliwindow, gdata[1], gdata[0], version, s)
					


			print "button %s was pressed" % data
			cliwindow.remove(hbox)
			loginbox = gtk.VBox(False, 0)
			cliwindow.add(loginbox)
			label = gtk.Label(str)
			label.set_text("Please enter the address and the password of the server you want to connect to.\nYou should also choose a name that will be seen by the other players.")
			loginbox.add(label)
			
			adressframe = gtk.Frame("Server")
			roomframe = gtk.Frame("Room Name")
			passframe = gtk.Frame("Password")
			nameframe = gtk.Frame("Name")
			
			adressentry = gtk.Entry()
			roomentry = gtk.Entry()
			passentry = gtk.Entry()
			nameentry = gtk.Entry()
			
			adressentry.set_text("cxmaster.netstyn.de")
			#adressentry.set_sensitive(False)
			
			adressframe.add(adressentry)
			roomframe.add(roomentry)
			passframe.add(passentry)
			nameframe.add(nameentry)
			
			adressentry.connect("changed", changebuttonstate)
			roomentry.connect("changed", changebuttonstate)
			passentry.connect("changed", changebuttonstate)
			nameentry.connect("changed", changebuttonstate)
			
			loginbox.add(adressframe)
			loginbox.add(roomframe)
			loginbox.add(passframe)
			loginbox.add(nameframe)
			
			connectbutton = gtk.Button("Connect")
			connectbutton.set_sensitive(False)
			connectbutton.connect("clicked", sendinit)
			loginbox.add(connectbutton)

			loginbox.show_all()
			
			roomentry.grab_focus() #So you dont have to click first
			
		cliwindow = function.create_window(name)
		hbox = gtk.VBox(False, 0)
		cliwindow.add(hbox)

		label = gtk.Label(str)
		label.set_text("Please choose whether you want to participate in an existing game or if you want to create a new room.")
		hbox.add(label)
		
		connect_button = gtk.Button("Join room")
		start_button = gtk.Button("Open new Room")

		connect_button.connect("clicked", connect_server, "cgame")
		start_button.connect("clicked", start_server, "ngame")
		
		hbox.pack_start(connect_button, True, True, 0)
		hbox.pack_start(start_button, True, True, 0)

		cliwindow.show_all()
		cliwindow.connect("destroy", lambda wid: gtk.main_quit())

def main():
	gtk.main()
	return 0
   
if __name__ == "__main__":
	catchx()
	main()
