import curses
import sys
import time
import os.path
import random
import pickle
from curses import wrapper

gamedims = 22
currPosList = ['0','2']

def main(stdscr):#{
	curses.start_color()
	curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
	curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
	curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_WHITE)
	curses.init_pair(4, curses.COLOR_RED, curses.COLOR_WHITE)
	curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_WHITE)
	curses.init_pair(6, curses.COLOR_MAGENTA, curses.COLOR_WHITE)
	curses.init_pair(7, curses.COLOR_CYAN, curses.COLOR_WHITE)
	curses.init_pair(8, curses.COLOR_GREEN, curses.COLOR_WHITE)
	curses.init_pair(9, curses.COLOR_BLUE, curses.COLOR_CYAN)
	stdscr.clear()
	stdscr.keypad(1)
	curses.curs_set(0)	
	dims = stdscr.getmaxyx()
	if os.path.exists('./constants/size.pkl'):#{
		f = open('./constants/size.pkl')
		global gamedims
		gamedims = pickle.load(f)
	#}
	else:#{
		output = open('./constants/size.pkl', 'w')
		data = gamedims
		pickle.dump(data, output)
		output.close()
	#}
		
	
	while True:
		selection = makeMenu(stdscr, gamemenu)		
		if selection == 1:#{
			sampleselection = 0
			while True:#{
				if sampleselection == curses.KEY_BACKSPACE: break
				initanswer=makeMenu(stdscr, initgame)
				if initanswer == 1:#{
					boardArray=[[0 for j in range(gamedims)]for i in range(gamedims)]
					showProgress(stdscr)
					drawBoard(stdscr, boardArray)
				#}
				elif initanswer == 2:#{
					while True:#{
						sampleselection=makeMenu(stdscr, samples)
						if sampleselection == 1:#{
							print ('1')
						#}
						elif sampleselection == 2:#{
							print ('2')
						#}
						elif sampleselection == 3:#{
							print ('3')
						#}
						elif sampleselection == 4:#{
							print ('4')
						#}
						elif sampleselection == 5:#{
							print ('5')
						#}
						elif sampleselection == 0:
							break
					#}
				#}
				elif initanswer == 0:#{
					break
				#}
			#}
		#}
		elif selection == 2:#{
			while True:#{
				option = makeMenu(stdscr, optionsmenu)
				if option == 1:#{
					fieldsize=numberSelector(stdscr, sizeselector, gamedims)
					if fieldsize != -1:#{
						gamedims = fieldsize
						break
					#}              
				#}
				elif option == 2:#{
					characters = elemSelector(stdscr, charselector, elemArray, currPosList) 
				#}
				elif option == 3:#{
					rulesmenu=makeMenu(stdscr, rules)
					if rulesmenu == 1:#{
						print ('1')
					elif rulesmenu == 2:#{
						print ('2')
					#}
				#}
				elif option == 0:#{	
					break
				#}
			#}
		#}
		elif selection == 3:#{
			stdscr.clear()
			stdscr.refresh()
			curses.endwin()
			output = open('./constants/size.pkl', 'w')
			data = gamedims
			pickle.dump(data, output)
			output.close()
			sys.exit()
		#}
		elif selection == 0:#{
			stdscr.clear()
			stdscr.refresh()
			curses.endwin()
			output = open('./constants/size.pkl', 'w')
			data = gamedims
			pickle.dump(data, output)
			output.close()
			sys.exit()
		#}
		#elif selection == ord('h'):#{
		#	stdscr.addstr(dims[0]-2,dims[1]/2-len(gamemenu[1])/2, gamemenu[1])		
		#	stdscr.refresh()
		##}
#}
			
def makeMenu(stdscr, L):#{

	pos1 = 1
	header = L[0]
	optCount = len(L)-2
	dims = stdscr.getmaxyx()
	lenList = [0]

	for i in L[2:]:#{
		lenList.append(len(i))
	#}

	maxWidth = max(lenList)+1

	while True:#{
		for x in range (0,optCount):#{
			if pos1 == x+1:#{
				stdscr.addstr(dims[0]/2-optCount+2*x, dims[1]/2-maxWidth/2-2,'> '+L[x+2].center(maxWidth,' ')+' <', curses.color_pair(2)) 
			#}
			else:#{
				stdscr.addstr(dims[0]/2-optCount+2*x, dims[1]/2-maxWidth/2-2, L[x+2].center(maxWidth+4, ' '))
			#}
		#}
		for y in range (0,maxWidth+10):#{
			stdscr.addstr(dims[0]/2-optCount-2, dims[1]/2-maxWidth/2+y-3-2, '=',curses.color_pair(2))
		#}
		stdscr.addnstr(dims[0]/2+optCount, dims[1]/2-maxWidth/2-3-2, ornament1 ,maxWidth+10,curses.color_pair(2))
		for a in range (0,optCount*2+1):#{
			stdscr.addstr(dims[0]/2-optCount-1+a, dims[1]/2-maxWidth/2+maxWidth+6-2,'I',curses.color_pair(2))
			stdscr.addstr(dims[0]/2-optCount-1+a, dims[1]/2-maxWidth/2-3-2, 'I', curses.color_pair(2))
		#}
		stdscr.addstr(dims[0]/2+optCount+1, dims[1]/2-len('Press "h" for help')/2, 'Press "h" for help',curses.color_pair(2))
		for b in range(0,len(header)):#{		
			stdscr.addstr(dims[0]/2-optCount-2, dims[1]/2-len(L[0])/2+b, header[b],curses.color_pair(random.randint(3,8)))	
		#}
		stdscr.refresh()	
		selection1=stdscr.getch()
		if selection1 == curses.KEY_UP and pos1 > 1:#{
			pos1-=1
		#}
		elif selection1 == curses.KEY_DOWN and pos1 < optCount:#{
			pos1+=1
		#}
		elif selection1 == 10:#{
			stdscr.clear()
			stdscr.refresh()
			return pos1
			break
		#}
		elif selection1 == curses.KEY_BACKSPACE:#{
			stdscr.clear()
			stdscr.refresh()
			pos1 = 0
			return pos1
			break
		#}
		#if (selection1 > ord('0') and selection1 <=ord(str(optCount))) or selection1 == curses.KEY_BACKSPACE:#{
		#	stdscr.clear()
		#	stdscr.refresh()
		#	return selection1
		#	break
		##}
		elif selection1 == ord('h'):#{
			stdscr.addstr(dims[0]-2,dims[1]/2-len(L[1])/2, L[1])
		#}
	#}	
#}

def drawBoard(stdscr, boardArray):#{
	x = len(boardArray)
	dims = stdscr.getmaxyx()
	y= int(round(float(x)/2))-x/2

	corner1 = [dims[0]/2-x/2,dims[1]/2-x]
	corner2 = [dims[0]/2-x/2,dims[1]/2+x]
	corner3 = [dims[0]/2+x/2,dims[1]/2+x]
	corner4 = [dims[0]/2+x/2,dims[1]/2-x]

	stdscr.addstr(corner1[0]-1, corner1[1]-1, "+", curses.A_REVERSE)
	stdscr.addstr(corner2[0]-1, corner2[1]+1, "+", curses.A_REVERSE)
	stdscr.addstr(corner3[0]+y, corner3[1]+1, "+", curses.A_REVERSE)
	stdscr.addstr(corner4[0]+y, corner4[1]-1, "+", curses.A_REVERSE)
	for k in range(1,x*2+2):#{
		stdscr.addstr(corner1[0]-1,corner1[1]-1+k, "-", curses.A_REVERSE)
		stdscr.addstr(corner4[0]+y,corner4[1]-1+k, "-", curses.A_REVERSE)
		if k <= x:#{
			stdscr.addstr(corner1[0]-1+k,corner1[1]-1, "|", curses.A_REVERSE)
			stdscr.addstr(corner2[0]-1+k,corner2[1]+1, "|", curses.A_REVERSE)
		#}
	#}
	for i in range(0,len(boardArray)):#{
		for j in range(0,len(boardArray[i])):#{
			stdscr.addstr(corner1[0]+j, corner1[1]+2*i, ' '+str(boardArray[i][j]))
		#}
	#}
	stdscr.refresh()
	input = stdscr.getch()
	stdscr.clear()
	stdscr.refresh()
#}		
	
def makePrompt(stdscr, L):#{
	
	question = L[0]
	header = L[1]
	ansCount = len(L)-2
	queLength = len(L[0])
	dims = stdscr.getmaxyx()
	
	while True:#{
		stdscr.clear()
		stdscr.box()
		stdscr.addstr(dims[0]/2-5, dims[1]/2-queLength/2, L[0])
		if ansCount == 2:#{
			stdscr.addstr(dims[0]/2+2, dims[1]/2-len(L[2])-5, L[2]+'(1)')
			stdscr.addstr(dims[0]/2+2, dims[1]/2+5, L[3]+'(2)')
		#}
		elif ansCount == 3:#{
			stdscr.addstr(dims[0]/2+2, dims[1]/2-len(L[2])-4, L[2]+' (1)')