#!/usr/bin/python3
# -*- coding: Utf-8 -*

"""
Program "Help MacGyver to escape".
You have to move MacGyver to different items and to give them to the guardian.
Python script.
Files : mglabyrinthe.py, classes.py, constantes.py, map/n1.txt + images/...
"""
import pygame

from pygame.locals import (
	QUIT, K_ESCAPE, K_SPACE, K_DOWN, K_UP, K_LEFT, K_RIGHT, KEYDOWN
)

from constantes import (WINDOW, WINDOW_TITLE, ICON, BACKGROUND)
from constantes import (
	ITEMS_WINDOW_TITLE, SURFACE_ETHER, SURFACE_TUBE, SURFACE_SYRINGUE
)
from constantes import (MG)
from constantes import (
	WINDOW_HOME, HOME_WINDOW_TITLE1, HOME_WINDOW_TITLE2, HOME_WINDOW_TITLE3,
	HOME_WINDOW_TITLE_RECT1, HOME_WINDOW_TITLE_RECT2, HOME_WINDOW_TITLE_RECT3
)
from constantes import (ETHER, TUBE, SYRINGUE)
from constantes import (WIN, GAMEOVER)
from constantes import (SOUNDTRACK)

from classes import (Labyrinth, Heroe, Items)

# Customization WINDOW of video game.
pygame.display.set_caption(WINDOW_TITLE)
pygame.display.set_icon(ICON)


class GameLoop:
	"""
	Class which define:
	How the game react (Home > Play > Victory/Defeat).
	How the different elements (maze, MacGyver, Items) interact for the gameplay.
	"""

	def __init__(self):

		self.maze = Labyrinth()
		self.mac_gyver = Heroe(self.maze.mg_position)
		

		self.ether = Items(ETHER, 'ether')
		self.ether.locate_items(self.maze.grid)

		self.syringue = Items(SYRINGUE, 'syringue')
		self.syringue.locate_items(self.maze.grid)

		self.tube = Items(TUBE, 'tube')
		self.tube.locate_items(self.maze.grid)

	def play_loop(self):
		"""
		Method which define gameplay.
		"""
		home = 1
		play = 1

		pygame.key.set_repeat(400, 30)
		pygame.time.Clock().tick(30)

		while home:
			for event in pygame.event.get():
				# Quit the game.
				if event.type == QUIT \
						or event.type == KEYDOWN \
							and event.key == K_ESCAPE:
					home = 0
					play = 0
				# Go in game.
				if event.type == KEYDOWN:
					if event.key == K_SPACE:
						home = 0
						# Load the sound (game).
						SOUNDTRACK.play(loops=-1)

			# Load the background (home).	
			WINDOW.blit(WINDOW_HOME, (0, 0))
			WINDOW.blit(HOME_WINDOW_TITLE1, HOME_WINDOW_TITLE_RECT1)
			WINDOW.blit(HOME_WINDOW_TITLE2, HOME_WINDOW_TITLE_RECT2)
			WINDOW.blit(HOME_WINDOW_TITLE3, HOME_WINDOW_TITLE_RECT3)

			# Refresh.
			pygame.display.flip()

		while play:

			# Load the background (game).
			pygame.draw.rect(WINDOW, (0, 0, 0), (0, 0, 540, 450))  # Cover WINDOW_HOME.
			WINDOW.blit(BACKGROUND, (0, 0))

			# Load the background (items).
			WINDOW.blit(ITEMS_WINDOW_TITLE, (455, 30))
			WINDOW.blit(SURFACE_ETHER, (480, 60))
			WINDOW.blit(SURFACE_TUBE, (480, 110))
			WINDOW.blit(SURFACE_SYRINGUE, (480, 160))

			for event in pygame.event.get():
				play = self.check_input(event)

			# Display the labyrinth.
			WINDOW.blit(BACKGROUND, (0, 0))
			self.maze.display()

			# Display items.
			self.display_items()

			# Add MacGyver in the labyrinth with his position.
			WINDOW.blit(MG, (self.mac_gyver.x_value, self.mac_gyver.y_value))

			# Victory/Defeat condition.
			if (self.mac_gyver.x_value, self.mac_gyver.y_value) == (390, 420):

				SOUNDTRACK.stop()

				if self.ether.collected \
						and self.tube.collected \
							and self.syringue.collected:
					# Display you win.
					WINDOW.blit(WIN, (0, 0))
					pygame.display.flip()
					print('- You win -')

				else:
					# Display you lose.
					WINDOW.blit(GAMEOVER, (0, 0))
					pygame.display.flip()
					print("- Game over -")

			pygame.display.flip()

	def check_input(self, event):
		"""
		Method which define how the game or the heroe react,
		when the user press specific keys on the keyboard.
		"""
		# Quit the game.
		if event.type == QUIT \
				or event.type == KEYDOWN \
					and event.key == K_ESCAPE:
			return 0

		# Move our Heroe.
		if event.type == KEYDOWN:
			if event.key == K_DOWN:
				self.mac_gyver.move('bottom', self.maze.grid)
		if event.type == KEYDOWN:
			if event.key == K_UP:
				self.mac_gyver.move('top', self.maze.grid)
		if event.type == KEYDOWN:
			if event.key == K_RIGHT:
				self.mac_gyver.move('right', self.maze.grid)
		if event.type == KEYDOWN:
			if event.key == K_LEFT:
				self.mac_gyver.move('left', self.maze.grid)
		return 1

	def display_items(self):
		"""
		Method which define how react the display of items,
		when they are collected or not.
		"""

		self.ether.farming(self.mac_gyver)
		self.syringue.farming(self.mac_gyver)
		self.tube.farming(self.mac_gyver)

		# Add items in the labyrinth.
		if not self.ether.collected:
			WINDOW.blit(ETHER, (self.ether.x_value, self.ether.y_value))
		else:
			WINDOW.blit(ETHER, (480, 60))

		if not self.tube.collected:
			WINDOW.blit(TUBE, (self.tube.x_value, self.tube.y_value))
		else:
			WINDOW.blit(TUBE, (480, 110))

		if not self.syringue.collected:
			WINDOW.blit(SYRINGUE, (self.syringue.x_value, self.syringue.y_value))
		else:
			WINDOW.blit(SYRINGUE, (480, 160))


GAME_LOOP = GameLoop()
GAME_LOOP.play_loop()
