#!/usr/bin/python

import gobject
from gtk import *
import gtk.gdk
import gnome.ui

PIXMAP_ROOT = ''

def coods(i):
	"Computes x,y coods of square on board from index"
	if i < 6:
		row = i/3
		col = 2 + (i%3)
	elif i < 27:
		row = 2 + (i-6)/7
		col = (i-6)%7
	else:
		row = 5 + (i-27)/3
		col = 2 + (i%3)
	return row,col

def indexof(r,c):
	"Computes index from row and col"
	if r < 2:
		return r*3+c-2
	elif r < 5:
		return 6 + (r-2)*7 + c
	else:
		return 25 + (r-5)*3 + c

class Bsquare:
	"Square on Brainvita board"
	
	def __init__(self,i,fl,wn,tb,bvita):
		self.row, self.col = coods(i)
		self.index = i
		self.filled = fl
		self.pmaps = [None, None, None, None]
		self.image = 0

		ebox = EventBox()
		box = Image()
		box.set_from_pixbuf(bvita.bpix)
		fixed = Fixed()
		tb.attach(ebox,self.col,self.col+1,self.row,self.row+1)
		ebox.add(fixed)
		fixed.put(box,0,0)
		ebox.set_events(gdk.BUTTON_PRESS_MASK)
		ebox.connect("button_press_event",bvita.callback,self)
		
		for j in range(4):
			self.pmaps[j] = Image()
			self.pmaps[j].set_from_pixbuf(bvita.pix[j])
			fixed.put(self.pmaps[j],5,5)

		box.show()
		if self.filled:
			self.pmaps[self.image].show()
		fixed.show()
		ebox.show()
	
	def rotate(self):
		self.pmaps[self.image].hide()
		self.image = (self.image+1)%4
		self.pmaps[self.image].show()
	
	def stop(self):
		self.pmaps[self.image].hide()
		self.image = 0
		self.pmaps[0].show()

	def gone_out(self):
		self.pmaps[self.image].hide()
		self.image = 0
		self.filled = 0
	
	def came_in(self):
		self.pmaps[self.image].show()
		self.filled = 1
	
	def reset(self, filled):
		self.pmaps[self.image].hide()
		self.image = 0
		self.filled = filled
		if filled:
			self.pmaps[self.image].show()

class Bvita:
	"Game of Brainvita"
	
	mleft = 32				# Marbles left
	sel_square = None		# The marble selected to be moved
	squares = []
	
	def __init__(self):
		win = gnome.ui.App('Brainvita', 'Brainvita')
#		win.set_size_request(294,294)
		win.connect("delete_event",main_quit)
		win.connect("destroy",main_quit)

		self.create_menus(win)
		grid = Table(7,7,TRUE)
		win.set_contents(grid)
		self.get_pixmaps(win)
		self.set_squares(win,grid)

		grid.show()
		win.show()
		timeout_add(120,self.animate)
		
	def create_menus(self, app):
		gameinfo = [
			('GameMenu', None, '_Game'),
			('HelpMenu', None, '_Help'),
			('New', gtk.STOCK_NEW, '_New', '<control>N', 'Start a New Game', 
			self.reset_squares),
			('Quit', gtk.STOCK_QUIT, '_Quit', '<control>Q', 'Quit Game', gtk.main_quit),
			('Save', gtk.STOCK_SAVE, '_Save', '<control>S', 'Save current game to file', 
			 self.not_yet),
			('Load', gtk.STOCK_OPEN, '_Load', '<control>O', 'Load game from file', 
			 self.not_yet),
			('About', gtk.STOCK_DIALOG_INFO, '_About', None, 'About Untee', self.about)
		]
		uiinfo = '''<ui>
					<menubar name = 'MenuBar'>
					<menu action = 'GameMenu'>
						<menuitem action = 'New'/>
						<menuitem action = 'Save'/>
						<menuitem action = 'Load'/>
						<menuitem action = 'Quit'/>
					</menu>
					<menu action = 'HelpMenu'>
						<menuitem action = 'About'/>
					</menu>
					</menubar>
					<toolbar name = 'ToolBar'>
						<toolitem action = 'New'/>
						<toolitem action = 'Quit'/>
						<separator/>
						<toolitem action = 'Save'/>
						<toolitem action = 'Load'/>
						<separator/>
						<toolitem action = 'About'/>
					</toolbar>
					</ui>
				'''
		act_grp = gtk.ActionGroup('MainGroup')
		act_grp.add_actions(gameinfo)
		uim = gtk.UIManager()
		uim.insert_action_group(act_grp, 0)
		uim.add_ui_from_string(uiinfo)
		tbar = uim.get_widget('/ToolBar')
		tbar.set_tooltips(True)
		tbar.show()
		mbar = uim.get_widget('/MenuBar')
		mbar.show()
		app.set_toolbar(tbar)
		app.set_menus(mbar)
		
		return

	def about(self,i):
		win = gnome.ui.About('Brainvita','0.3','GNU Public License', 'A brain teaser',
			['Vaibhav Vaish', 'vaibhav@stanford.edu'])
		win.show()
	
	def not_yet(self,i):
		gnome.ui.gnome_error_dialog('This feature is not supported\n in this version of Brainvita')
		return

	def get_pixmaps(self,win):
		p1 = gdk.pixbuf_new_from_file(PIXMAP_ROOT+"red1.xpm")
		p2 = gdk.pixbuf_new_from_file(PIXMAP_ROOT+"red2.xpm")
		p3 = gdk.pixbuf_new_from_file(PIXMAP_ROOT+"red3.xpm")
		p4 = gdk.pixbuf_new_from_file(PIXMAP_ROOT+"red4.xpm")
		self.pix = [p1,p2,p3,p4]
		self.bpix = gdk.pixbuf_new_from_file(PIXMAP_ROOT+"box.xpm")

	def set_squares(self,w,f):
		for i in range(33):
			filled = (i != 16)
			self.squares.append(Bsquare(i,filled,w,f,self))

	def reset_squares(self, i):
		for i in range(33):
			filled = (i != 16)
			self.squares[i].reset(filled)

	def callback(self,eb,ev,sq):
		if (sq.filled):
			if sq.index == self.sel_square:
				self.sel_square = None
				sq.stop()
			else:
				ind = self.sel_square
				self.sel_square = sq.index
				if (ind != None):
					self.squares[ind].stop()

		elif (self.sel_square != None):
			self.move_to(sq.index)
	
	def animate(self):
		if self.sel_square != None:
			self.squares[self.sel_square].rotate()
		return 1

	def move_to(self,goal):
		curr = self.squares[self.sel_square]
		dest = self.squares[goal]
		
		r1,c1 = curr.row,curr.col
		r2,c2 = dest.row,dest.col
		r,c = (r1+r2)/2, (c1+c2)/2
		ind = indexof(r,c)
		empty = self.squares[ind].filled
		self.sel_square = None

		if (empty and ((r1==r2 and abs(c1-c2)==2) or (c1==c2 and abs(r1-r2) == 2))):
			# Valid move!
			curr.gone_out()
			self.squares[ind].gone_out()
			dest.came_in()
			self.mleft = self.mleft - 1
		else:
			curr.stop()

a = Bvita()
main()
