#!/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 server
import time

#Set a few needed variables
version = 0.012
name = "CatchX" #Just if we have to change the name for somewhat reason
threads = [] #The threads
#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):
			print "button %s was pressed" % data
			#thread.start_new_thread(server.open_server, (str(port),)) #The server runs in a new thread...
			thread = server.gserver()
			threads.append(thread)
			thread.start() 
			thread.run()
			#time.sleep(0) #Thanks Waldteufel :)

		def connect_server(widget, data):
			
			def changebuttonstate(widget):
				if adressentry.get_text() <> "" and passentry.get_text() <> "" and nameentry.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()
				s = function.init_connection(server)
				cliwindow.remove(loginbox)
				infobox = gtk.VBox(False, 0)
				cliwindow.add(infobox)
				label = gtk.Label(str)
				#if s == False:
				#	label.set_text("Couldn't establish a connection to the server.")
				#else:
				label.set_text("Loading game data...")
				infobox.add(label)
				cliwindow.show_all()
				gdata = function.get_gamedata(s, password, nickname)
				if gdata == "err_running":
					label.set_text("Game is already running")
				elif gdata == "err_login":
					label.set_text("Wrong password")
				else:
					cliwindow.remove(infobox)
					#kret = thread.start_new_thread(function.keepalive, (s,gdata[0]))
					#kret = function.keepalive(s, gdata[0])
					function.load_maingui(cliwindow, gdata[1], gdata[0])

			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("Address")
			passframe = gtk.Frame("Password")
			nameframe = gtk.Frame("Name")
			
			adressentry = gtk.Entry()
			passentry = gtk.Entry()
			nameentry = gtk.Entry()
			
			adressframe.add(adressentry)
			passframe.add(passentry)
			nameframe.add(nameentry)
			
			adressentry.connect("changed", changebuttonstate)
			passentry.connect("changed", changebuttonstate)
			nameentry.connect("changed", changebuttonstate)
			
			loginbox.add(adressframe)
			loginbox.add(passframe)
			loginbox.add(nameframe)
			
			connectbutton = gtk.Button("Connect")
			connectbutton.set_sensitive(False)
			connectbutton.connect("clicked", sendinit)
			loginbox.add(connectbutton)

			loginbox.show_all()
			
			adressentry.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 one.")
		hbox.add(label)
		
		connect_button = gtk.Button("Connect to existing game")
		start_button = gtk.Button("Start new game")

		connect_button.connect("clicked", connect_server, "cgame")
		start_button.connect("clicked", start_server, "ngame")
		start_button.set_sensitive(False) #Not implemented yet ;)

		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()