#Import Packages
print('loading Game...')
import pygame
import sys
import os

#Import Global Memory
print('Loading Game Memory...')
from Scripts import Global
Global.init()
print('Starting Game...')

#MainFile Game Class
class ProjectAlchemy(object):
    def main(this, screen):
        clock = pygame.time.Clock()

        #Set Game Icon
        gameIcon = pygame.image.load("Sprites/Game/icon.png")
        pygame.display.set_icon(gameIcon)

        #Main game loop
        while 1:
            #FPS control
            clock.tick(30)

            #Get Keyboard Data
            key = pygame.key.get_pressed()

            #Close the game
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    return

            #Input Management

            #LEFT
            if key[pygame.K_LEFT]:
                leftpress += 1
            else:
                leftpress = 0
            #RIGHT
            if key[pygame.K_RIGHT]:
                rightpress += 1
            else:
                rightpress = 0
            #UP
            if key[pygame.K_UP]:
                uppress += 1
            else:
                uppress = 0
            #DOWN
            if key[pygame.K_DOWN]:
                downpress += 1
            else:
                downpress = 0
            #Z
            if key[pygame.K_z]:
                zpress += 1
            else:
                zpress = 0
            #X
            if key[pygame.K_x]:
                xpress += 1
            else:
                xpress = 0

            #Debug (use this area for debugging)

            #Manage Audio
            Global.audio.Control()

            #Set black base background.
            Global.screen.fill((255, 255, 255))

            #Prioritize and Execute Game Instructions

            #Main Menu
            if Global.main.state > 0:
                Global.main.Control(leftpress, rightpress, uppress, downpress, zpress, xpress)
            #Minigame
            if Global.game.state > 0:
                Global.game.Control(leftpress, rightpress, uppress, downpress, zpress, xpress)
            #Shop
            if Global.shop.state > 0:
                Global.shop.Control(leftpress, rightpress, uppress, downpress, zpress, xpress)
            #Menu
            if Global.map.state > 0:
                Global.map.Control(leftpress, rightpress, uppress, downpress, zpress, xpress)
            #Transition
            if Global.transition.state > 0:
                Global.transition.Control()

            #Update the screen (Don't put any drawing assets below this line!)
            screen.blit(pygame.transform.scale(Global.screen, (800, 480)), (0, 0))
            pygame.display.flip()

#Makes the game run.
if __name__ == "__main__":
    pygame.init()
    pygame.display.set_caption("Project Alchemy")
    screen = pygame.display.set_mode((800,480))
    ProjectAlchemy().main(screen)
