"""

This is the general code for the game. No menu and extra frills added yet.
Do not edit this file. Use this file as basis for server and client versions of the game.

Notes:
	- list of assets and game states are stored in resources.py (comments added to note what game states are for)

"""

import pyglet
import time
import thread
import random
from pyglet.window import mouse
from pyglet.window import key
from game import resources,card
from game.resources import SharedVar

# Disable error checking for increased performance
pyglet.options['debug_gl'] = False

from pyglet.gl import *

window_width = 800
window_height = 600

# this is the main game loop
event_loop = pyglet.app.EventLoop()

# this is the main game window
game_window = pyglet.window.Window(window_width,window_height)

# generate cards
# this is the array for the cards in the boards
cards_list = []

# this is the list of index for the cards on the board
# board is 20x20, a pair of cards has matching index i
# so if we have 20 cards, we have twice of 0-9 indexes
index_list = []

# initialize game_state to START
game_state = SharedVar.state['START']

# scores for players
player1 = 0
player2 = 0
start = True

# CUT SOMETHING HERE

# selector for the card
card_select_border = pyglet.sprite.Sprite(img=resources.card_select_border,x=0,y=window_height)

# column position of the selector from 0-4
card_select_x = 0

# row position of the selector from 0-3
card_select_y = 0

# index of card to which the selector is currently located
card_select_pos = 0

# stores the matched indexes 
matched_index = []

# stores the flipped cards
flipped_cards = []

# stores the flipped indexes
flipped_index = []

# check if the two flipped cards are matching pairs
def check_move():
	global flipped_cards
	global flipped_index
	global game_state
	global player1
	global player2
	global matched_index

	if flipped_cards[0].card_name != flipped_cards[1].card_name:
		# delay the card showing for 1 second
		event_loop.sleep(1)
		for item in flipped_cards:
			item.current = item.back
		if game_state == SharedVar.state['PLAYER1']:
			game_state = SharedVar.state['TRANSITION_PLAYER1']
		elif game_state == SharedVar.state['PLAYER2']:
			game_state = SharedVar.state['TRANSITION_PLAYER2']
		
	else:
		for item in flipped_index:
			matched_index.append(item)
		if game_state == SharedVar.state['PLAYER1']:
			player1 += 1
		elif game_state == SharedVar.state['PLAYER2']:
			player2 += 1
		if len(matched_index) == 20:
			game_state = SharedVar.state['END']

	# empty the flipped cards
	flipped_cards = []

	# empty the flipped indexes
	flipped_index = []

def flip(card):
	card.current.draw()
	
# draw the cards on the board
def draw_cards(cards_list):
	for card in cards_list:
		flip(card)

	card_select_border.draw()

# on key press events
@game_window.event
def on_key_press(symbol, modifiers):
	global game_state
	global card_select_x 
	global card_select_y
	global card_select_pos
	global flipped_cards
	global matched_index
	global player1
	global player2

	# let user move selector to the right
	if symbol == key.RIGHT:
		if card_select_border.x + card_select_border.width >= window_width:
			card_select_border.x = 0
		else:
			card_select_border.x += card_select_border.width

		if card_select_x >= 4:
			card_select_x = 0
		else:
			card_select_x += 1

		card_select_pos = card_select_x + (card_select_y * 5)

	# let user move selector to the left
	if symbol == key.LEFT:
		if card_select_border.x - card_select_border.width < 0:
			card_select_border.x = window_width - card_select_border.width
		else:
			card_select_border.x -= card_select_border.width

		if card_select_x <= 0:
			card_select_x = 4
		else:
			card_select_x -= 1

	card_select_pos = card_select_x + (card_select_y * 5)

	# let user move selector upwards
	if symbol == key.UP:
		if card_select_border.y + card_select_border.height >= window_height + card_select_border.height:
			card_select_border.y= card_select_border.height
		else:
			card_select_border.y += card_select_border.height

		if card_select_y <= 0:
			card_select_y = 3
		else:
			card_select_y -= 1
		
		card_select_pos = card_select_x + (card_select_y * 5)

	# let user move selector downwards
	if symbol == key.DOWN:
		if card_select_border.y - card_select_border.height < card_select_border.height:
			card_select_border.y = window_height
		else:
			card_select_border.y -= card_select_border.height

		if card_select_y >= 3:
			card_select_y = 0
		else:
			card_select_y += 1

		card_select_pos = card_select_x + (card_select_y * 5)

	# let the users flip up a card
	if symbol == key.SPACE:
		if card_select_pos not in matched_index and card_select_pos not in flipped_index:
			cards_list[card_select_pos].current = cards_list[card_select_pos].front
			flipped_cards.append(cards_list[card_select_pos])
			flipped_index.append(card_select_pos)

# on key release events
@game_window.event
def on_key_release(symbol,modifiers):
	if symbol == key.SPACE:
		# if there are already 2 flipped cards on the board
		if len(flipped_cards) == 2:
			check_move()

@game_window.event
def on_close():
	event_loop.exit()
	game_window.close()
	
@game_window.event
def on_draw():
	game_window.clear()
	draw_cards(cards_list)

def update(dt):
	global game_state
	global player1
	global player2
	global start

	if game_state == SharedVar.state['START']:
		game_state = SharedVar.state['SETUP']

	elif game_state == SharedVar.state['SETUP']:
		# we shuffle the index list to shuffle their arrangement on the board
		index_list = [i for i in range(10)] + [i for i in range(10)]
		random.shuffle(index_list)

		# generate images for the card
		i = 0
		j = 0
		k = 0

		# 4 rows
		while i<4:
			j=0

			# 5 columns
			while j<5:
				x_pos = j * 160
				y_pos = window_height - (i *150)

				# index is the kth item in the shuffled index list
				index = index_list[k]
				card_name = "card" + str(index+1)
				card_back = pyglet.sprite.Sprite(img=resources.card_back,x=x_pos,y=y_pos)
				card_front = pyglet.sprite.Sprite(img=resources.card_front[index],x=x_pos,y=y_pos)
				new_card = card.Card(card_back,card_front,card_name)
				cards_list.append(new_card)

				j = j + 1
				k = k + 1

			i = i + 1

		game_state = SharedVar.state['PLAYER1']

	# elif game_state == SharedVar.state['PLAYER1']:
	# 	print "PLAYER1"

	elif game_state == SharedVar.state['TRANSITION_PLAYER1']:
		game_state = SharedVar.state['PLAYER2']

	elif game_state == SharedVar.state['TRANSITION_PLAYER2']:
		game_state = SharedVar.state['PLAYER1']

	elif game_state == SharedVar.state['END']:
		if start:
			start = False
			print "GAME OVER"
			print "PLAYER 1 SCORE:", player1
			print "PLAYER 2 SCORE:", player2
			if player1 > player2:
				print "PLAYER 1 WINS!!"
			elif player2 > player1:
				print "PLAYER 2 WINS!!"
			else:
				print "IT'S A DRAW!!"

def main():
	pyglet.clock.schedule_interval(update, 1/120.0)
	pyglet.clock.set_fps_limit(120)
	event_loop.run()

# main function
if __name__ == "__main__":
	main()