import pygame
from pygame.locals import *

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--windowed", help="Play in a window")
args = parser.parse_args()

pygame.init()

if(args.windowed):
    width = 1280 #Do not change (1280)
    height = 720 #Do not change (720)
    fullscreen = False
else:
    width = 1280
    height = 720
    fullscreen = True

gameTitle = "Waves of Empires"
version = "1.0"
credit = "Global Game Jam 2017, Thomas Cyrix (Dev), Juniper (Gfx) & Lucasfleur (audio)"
credit2 = "Special thanks to Hyptosis for a few tiles (CC)"

humans = 20 #Starting human houses - Lower is harder (20)
monthsPerYear = 50 #Speed - Higher is slower (100)

pStade = 5 #1 chance sur X d'évoluer de stade (5)
pStatus = 15 #1 chance sur X de partir de côté (15)
pDemography = 32 #1 chance sur X de créer une maison (32)

#Do not change :
stadeMax = 5 #Maximum evolution age (5)
statusMax = 4 # Maximum status (4)

print(gameTitle+" - "+version)
print(credit)

from Video import Video
from Map import Map

video = Video(width,height,gameTitle,version,credit,credit2,fullscreen)
clock = pygame.time.Clock()

#=====================================================
def waitMenu(video,menu="title"):
    if(menu=="title"):
        video.drawTitle()
        video.playEffect("gameover")
    if(menu=="gameover"):
        video.drawGameOver()
        video.playEffect("dino")
    if(menu=="victory"):
        video.drawVictory()
        video.playEffect("victory")
    if(menu=="tuto"): video.drawTuto()
    video.refresh()
    while True:
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYUP and event.key==K_ESCAPE): return False
            if event.type == KEYUP and event.key==K_SPACE: return True
        clock.tick(60)

#======================================================
exitNow = False

while exitNow==False:

    lmap = Map(width/32,height/32-1,video,humans,stadeMax,statusMax,pStade,pStatus,pDemography)

    
    video.refresh()
    
    result = 0
    if(waitMenu(video,"title")):
        continuer = 1
        if(waitMenu(video,"tuto")==False):
            continuer = 0
        else:
            pygame.mixer.music.load('main.mp3')
            pygame.mixer.music.play(-1)
            video.drawMap()
            video.refresh()
        
        pressed = False
        lmap.force = 0
        month = 0
        while continuer:
            for event in pygame.event.get():
                if event.type == QUIT:
                    exitNow = True
                    continuer = 0
                if event.type == KEYUP and event.key==K_ESCAPE: continuer = 0
                
                if(lmap.wave==None):
                    if event.type == KEYDOWN and event.key==K_SPACE:
                        pressed = True
                        lmap.force = 0
                    if event.type == KEYUP and event.key==K_SPACE:
                        pressed = False
                        
            if(pressed):
                    lmap.force += 1
                    if(lmap.force>=100): pressed = False
            if(lmap.force>0 and pressed==False):
                lmap.earthquake()
                video.playEffect("earthquake")
                lmap.force = 0

            if(month>monthsPerYear):
                month = 0
                result = lmap.nextYear()
                #video.drawMap()
                #video.refresh(lmap.year)
            month += 1
            if(lmap.nextMonth() or True):
                video.drawMap()
                video.refresh(lmap.year)
            
            clock.tick(60)
            if(result>0): continuer = 0
    else:
        exitNow = True
    pygame.mixer.music.stop()
    pygame.mixer.music.rewind()
    if(result==1): waitMenu(video,"victory")
    if(result==2): waitMenu(video,"gameover")
    if(result>0): continuer = 1
pygame.quit()
print("Done.")
