"""
levels.py - code used for loading enemies level-wise
"""

import common
import enemyA,enemyAI,enemyAII
import enemyB,enemyBI
import enemyC
import enemyD
import enemyE
import enemyF
import enemyG
import enemyH
import enemyI
import enemyJ
import enemyK
import enemyL
import goodieA
import meteorA,meteorB,meteorC,meteorD
import random
import pygame, pygame.font
import main

level = 0
level_surface = None
final_timeout = 100
enemydata = []
gameover_surface = None
levelfinished_surface = None
pause_surface = None

def init():
    global gameover_surface,levelfinished_surface,pause_surface
    gameover_font = pygame.font.Font('bluebold.ttf', 64)
    gameover_surface = gameover_font.render("That's all folks!",1,(0,255,0),(0,0,0)).convert()
    gameover_surface.set_colorkey((0,0,0))
    levelfinished_surface = gameover_font.render("LEVEL FINISHED",1,(0,255,0),(0,0,0)).convert()
    levelfinished_surface.set_colorkey((0,0,0))
    pause_surface = gameover_font.render("PAUSED",1,(0,255,0),(0,0,0)).convert()
    pause_surface.set_colorkey((0,0,0))

def showLevel():
    r = level_surface.get_rect()
    res = common.resolution
    x = common.screen.blit(level_surface,(res[0]/2-r[2]/2,res[1]/2-r[3]/2))
    pygame.display.update(x)

def hideLevel():
    r = level_surface.get_rect()
    res = common.resolution
    dest = (res[0]/2-r[2]/2,res[1]/2-r[3]/2,r[2],r[3])
    x = common.screen.blit(common.current_bg,dest,dest)
    pygame.display.update(x)

def showPause():
    res = common.resolution
    r = pause_surface.get_rect()
    x = common.screen.blit(pause_surface,(res[0]/2-r[2]/2,res[1]/2-r[3]/2))
    pygame.display.update(x)

def hidePause():
    res = common.resolution
    r = pause_surface.get_rect()
    dest = res[0]/2-r[2]/2,res[1]/2-r[3]/2,r[2],r[3]
    x = common.screen.blit(common.current_bg,dest,dest)
    pygame.display.update(x)
 
def nextLevel():
    global level, enemydata, level_surface
    level += 1
    fnt = pygame.font.Font('bluebold.ttf', 64)
    level_surface = fnt.render("LEVEL %d"%level,1,(0,255,0),(0,0,0))
    level_surface.set_colorkey((0,0,0))
    
    if level == 1:
        enemydata = [
            { 'Delay': 50,
              'Enemies':
              map(lambda x: [0,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,50)]],range(10))
              },
            { 'Delay': 400,
              'Enemies':
              map(lambda x: [2,[20+random.randrange(10),-40*x],1], range(1,8))+\
              map(lambda x: [2,[common.resolution[0]-20-random.randrange(10),-40*x-400],-1], range(1,8))+\
              map(lambda x: [3,[x*common.resolution[0]/4,-200]],range(1,4))
              },
            { 'Delay': 1500,
              'Enemies':
              map(lambda x: [2,[common.resolution[0]/2,-40*x],random.randrange(-1,2,2)],range(10))+\
              map(lambda x: [3,[x*common.resolution[0]/4,-100]],range(1,4))+\
              map(lambda x: [4,[x*common.resolution[0]/4,-200]],range(1,4))
              },
            { 'Delay': 3500,
              'Enemies':
              map(lambda x: [3,[x*common.resolution[0]/8,-random.randrange(50,150)]],range(1,8))+\
              map(lambda x: [4,[x*common.resolution[0]/6,-random.randrange(50,300)]],range(1,6))
              },
            { 'Delay': 20000,
              'Enemies': [ [5, [common.resolution[0]/2-20,-50]] ]
              }
            ]
    elif level == 2:
        enemydata = [
            { 'Delay':50,
              'Enemies':
              map(lambda x: [20,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,100)]],range(5))
              },
            { 'Delay':2000,
              'Enemies':
              map(lambda x: [23,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,50)]],range(10))
              },
            { 'Delay':4000,
              'Enemies':
              map(lambda x: [26,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,100)]], range(10))
              },
            { 'Delay':6500,
              'Enemies':
              map(lambda x: [26,[random.randrange(50,common.resolution[0]-50),-random.randrange(50,200)]], range(5))+
              map(lambda x: [23,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,100)]], range(10))+
              map(lambda x: [20,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,100)]], range(15))
              },
            { 'Delay':20000,
              'Enemies': [ [29, [common.resolution[0]/2-35, -100]] ]
              }
            ]
    elif level == 3:
        enemydata = [
            { 'Delay': 50,
              'Enemies':
              map(lambda x: [30,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]],range(10))+\
              map(lambda x: [random.randrange(10,14),[random.randrange(common.resolution[0]-20),-random.randrange(5,200)]],range(20))
              },
            { 'Delay': 750,
              'Enemies':
              map(lambda x: [33,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]],range(15))+\
              map(lambda x: [30,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]],range(5))
              },
            { 'Delay': 2500,
              'Enemies':
              map(lambda x: [36,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(5))+\
              map(lambda x: [30,[random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(10))
              },
            { 'Delay': 5000,
              'Enemies':
              map(lambda x: [36, [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(3))+\
              map(lambda x: [33, [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(7))+\
              map(lambda x: [30, [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(10))
              },
            { 'Delay': 20000,
              'Enemies': [ [39, [common.resolution[0]/2-40,-100]] ]
              }
            ]
    elif level == 4:
        enemydata = [
            { 'Delay': 50,
              'Enemies':
              map(lambda x: [random.randrange(10,14), [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]],range(10))+\
              map(lambda x: [4, [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(4))+\
              map(lambda x: [23, [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(4))
              },
            { 'Delay': 750,
              'Enemies':
              [ [5, [common.resolution[0]/3,-100]] ]+\
              map(lambda x: [33, [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(7))
              },
            { 'Delay': 2500,
              'Enemies':
              [ [29, [common.resolution[0]/2-40,-100]] ]+\
              map(lambda x: [30, [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(10))+\
              map(lambda x: [36, [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(3))
              },
            { 'Delay': 5000,
              'Enemies':
              [ [39, [common.resolution[0]/2-40, -100]],
                [5, [common.resolution[0]/3, -100]] ]+\
              map(lambda x: [30, [random.randrange(50,common.resolution[0]-50),-random.randrange(5,200)]], range(5))
              },
            { 'Delay': 20000,
              'Enemies': [ [40, [common.resolution[0]/2, -100]] ]
              }
            ]
    for x in enemydata:
        x['Delay'] += (2-common.diff)*200 # easy = slower waves
    # although a halfway good player shouldn't get into problems
    # anyways because the waves are chosen moderately distanced

def nextFrame():
    global enemydata,final_timeout
    if len(common.enemies)==0 and len(enemydata) > 0:
        if enemydata[0]['Delay'] > 50:
            enemydata[0]['Delay'] = 50
    for x in enemydata:
        x['Delay'] -= 1        
        if x['Delay'] <= 0:
            # Let the horde go off!
            # bla bla
            for data in x['Enemies']:
                if data[0] == 0:
                    enemyA.EnemyA(data[1])
                elif data[0] == 1:
                    enemyAI.EnemyAI(data[1],data[2])
                elif data[0] == 2:
                    enemyAII.EnemyAII(data[1],data[2])
                elif data[0] == 3:
                    enemyB.EnemyB(data[1])
                elif data[0] == 4:
                    enemyBI.EnemyBI(data[1])
                elif data[0] == 5:
                    enemyC.EnemyC(data[1])
                    
                elif data[0] == 10:
                    meteorA.MeteorA(data[1])
                elif data[0] == 11:
                    meteorB.MeteorB(data[1])
                elif data[0] == 12:
                    meteorC.MeteorC(data[1])
                elif data[0] == 13:
                    meteorD.MeteorD(data[1])

                elif data[0] == 20:
                    enemyD.EnemyD(data[1])
                elif data[0] == 23:
                    enemyE.EnemyE(data[1])
                elif data[0] == 26:
                    enemyF.EnemyF(data[1])
                elif data[0] == 29:
                    enemyH.EnemyH(data[1])

                elif data[0] == 30:
                    enemyG.EnemyG(data[1])
                elif data[0] == 33:
                    enemyI.EnemyI(data[1])
                elif data[0] == 36:
                    enemyJ.EnemyJ(data[1])
                elif data[0] == 39:
                    enemyK.EnemyK(data[1])

                elif data[0] == 40:
                    enemyL.EnemyL(data[1])

                elif data[0] == 50:
                    goodieA.GoodieA(data[1])
                elif data[0] == 51:
                    goodieB.GoodieB(data[1])
            enemydata.remove(x)
    if len(common.enemies) == 0 and len(enemydata)==0:
        final_timeout -= 1
        if level < 4:
            showLevelFinished()
        else: showGameOver()
        if final_timeout <= 0:
            final_timeout = 100
            common.player.lives += 1
            if level < 4:
                main.start()
            else:
                common.game_finished = 1
            
def showGameOver():
    global gameover_surface
    r = gameover_surface.get_rect()
    res = common.resolution
    dest = (res[0]/2-r[2]/2,res[1]/2-r[3]/2,r[2],r[3])
    common.screen.blit(common.current_bg,dest,dest)
    gameover_surface.set_alpha(50+final_timeout)
    common.screen.blit(gameover_surface,dest)
    pygame.display.update(dest)

def showLevelFinished():
    global levelfinished_surface
    r = levelfinished_surface.get_rect()
    res = common.resolution
    dest = (res[0]/2-r[2]/2,res[1]/2-r[3]/2,r[2],r[3])
    common.screen.blit(common.current_bg,dest,dest)
    levelfinished_surface.set_alpha(10+final_timeout)
    common.screen.blit(levelfinished_surface,dest)
    pygame.display.update(dest)
