# Concave
# Copyright (C) 2012 Daniel Malmqvist
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import mapmaker
from inventory import Inventory
import constants
import helperfunctions
import pygame
from sounds import Sounds

currentLevel = 0
levelMaps = []
enemies = []
mapMaker = 0
player = 0
enemies = []
player = 0
items = []
levels = 0
magic = []
inventory = Inventory()
itemImages = dict()
magicImages = dict()
magicIcons = dict()
magicLevels = 0
fontBigger = pygame.font.Font(None, 36)
fontBig = pygame.font.Font(None, 24)
fontNormal = pygame.font.Font(None, 20)
npcImages = dict()
soundSystem = Sounds()
fire = []

def reset():
    global currentLevel
    global levelMaps
    global enemies
    global mapMaker
    global player
    global enemies
    global items
    global levels
    global magic
    global inventory
    global itemImages
    global magicImages
    global magicIcons
    global magicLevels
    global fontBigger
    global fontBig
    global fontNormal
    global npcImages
    global soundSystem
    global fire
    currentLevel = 0
    levelMaps = []
    enemies = []
    mapMaker = 0
    player = 0
    enemies = []
    player = 0
    items = []
    levels = 0
    magic = []
    inventory = Inventory()
    itemImages = dict()
    magicImages = dict()
    magicIcons = dict()
    magicLevels = 0
    fontBigger = pygame.font.Font(None, 36)
    fontBig = pygame.font.Font(None, 24)
    fontNormal = pygame.font.Font(None, 20)
    npcImages = dict()
    soundSystem = Sounds()
    fire = []
    
    

def getSoundSystem():
    global soundSystem
    return soundSystem

def setPlayer(p):
    global player
    player = p
    
def getPlayer():
    global player
    return player


def removeEnemy(e):
    global enemies
    global currentLevel
    try:
        enemies[currentLevel].remove(e)
    except ValueError:
        pass

def getEnemies():
    global currentLevel
    return getEnemiesA(currentLevel)

def getEnemiesA(level):
    global enemies
    try:
        return enemies[level]
    except IndexError:
        return []

def addEnemy(e):
    global enemies
    global currentLevel
    
    if len(enemies) == 0:
        for i in range(0,100):
            enemies.append([])
    
    if currentLevel % 5 == 0 and len(enemies[currentLevel]) == 1:
        pass
    else:
        enemies[currentLevel].append(e)

def setMapLevel(mapTable, level):
    global levelMaps
    
    if len(levelMaps) == 0:
        for i in range(0,100):
            levelMaps.insert(i, 0)

    levelMaps[level] = mapTable


def setMap(mapTable):
    global levelMaps
    global currentLevel
    
    
    if len(levelMaps) == 0:
        for i in range(0,100):
            levelMaps.insert(i, 0)

    levelMaps[currentLevel] = mapTable
    
def getMap():
    global currentLevel
    return getLevelMap(currentLevel)

def getLevelMap(level):
    if level % 5 == 0:
        return getBossLevel()
    
    global levelMaps
    if len(levelMaps) == 0:
        for i in range(0,100):
            levelMaps.insert(i, 0)


    if levelMaps[level] == 0:
        global mapMaker
        if mapMaker == 0:
            mapMaker = mapmaker.MapMaker()
        
        if getCurrentLevel() != 0:
            levelMaps[level] = mapMaker.generateMap(200+10*level, 100+10*level, 10+(level*3), 8, 8)
        #levelMaps[level] = mapMaker.generateMap(200, 100, 2, 5, 5)
    return levelMaps[level]

def getItems():
    global currentLevel
    return getItemsMap(currentLevel)

def getItemsMap(level):
    global items

    if len(items) == 0:
        for i in range(0,100):
            items.append([])

    return items[level]

def addItem(item):
    global currentLevel
    addItemLevel(currentLevel, item)

def addItemLevel(level, item):
    global items
    if len(items) == 0:
        for i in range(0,100):
            items.append([])
    
    items[level].append(item)
    
def removeItem(item):
    global items
    global currentLevel
    
    
    items[currentLevel].remove(item)
    
def getCurrentLevel():
    global currentLevel
    return currentLevel

def setCurrentLevel(level):
    global currentLevel
    currentLevel = level
    
def expLevels():
    global levels
    
    if levels == 0:
        levels = []
        expTot = 0
        prev = 10
        for x in range(1,61):
            prev = int(prev*1.1)
            expTot += x*x*prev
            levels.append(expTot)
        
    return levels


def getInventroy():
    global inventory
    return inventory

def setInventory():
    global inventory
    inventory = Inventory()

def addMagic(m):
    global magic
    magic.append(m)

def removeMagic(m):
    global magic
    magic.remove(m)
    
def getMagic():
    global magic
    return magic
    
def getItemImages():
    global itemImages
    
    if len(itemImages) == 0:
        itemImages = dict()
        itemSheet = pygame.image.load("graphic/items.png")
        
        itemImages[(constants.BELT(), 1)] = helperfunctions.loadImage(itemSheet, 0, 0, 32, 32)
        itemImages[(constants.BELT(), 2)] = helperfunctions.loadImage(itemSheet, 1, 0, 32, 32) 
        
        itemImages[(constants.FEET(), 1)] = helperfunctions.loadImage(itemSheet, 0, 1, 32, 32)
        itemImages[(constants.FEET(), 2)] = helperfunctions.loadImage(itemSheet, 1, 1, 32, 32) 
        
        itemImages[(constants.HANDS(), 1)] = helperfunctions.loadImage(itemSheet, 0, 2, 32, 32)
        
        itemImages[(constants.HEAD(), 1)] = helperfunctions.loadImage(itemSheet, 0, 3, 32, 32)
        itemImages[(constants.HEAD(), 2)] = helperfunctions.loadImage(itemSheet, 1, 3, 32, 32)
        itemImages[(constants.HEAD(), 3)] = helperfunctions.loadImage(itemSheet, 2, 3, 32, 32)
        itemImages[(constants.HEAD(), 4)] = helperfunctions.loadImage(itemSheet, 3, 3, 32, 32)
        itemImages[(constants.HEAD(), 5)] = helperfunctions.loadImage(itemSheet, 4, 3, 32, 32)
        
        itemImages[(constants.LEGS(), 1)] = helperfunctions.loadImage(itemSheet, 0, 4, 32, 32)
        itemImages[(constants.LEGS(), 2)] = helperfunctions.loadImage(itemSheet, 1, 4, 32, 32)
        itemImages[(constants.LEGS(), 3)] = helperfunctions.loadImage(itemSheet, 2, 4, 32, 32)
        
        itemImages[(constants.TORSO(), 1)] = helperfunctions.loadImage(itemSheet, 0, 5, 32, 32)
        itemImages[(constants.TORSO(), 2)] = helperfunctions.loadImage(itemSheet, 1, 5, 32, 32)
        itemImages[(constants.TORSO(), 3)] = helperfunctions.loadImage(itemSheet, 2, 5, 32, 32)
        itemImages[(constants.TORSO(), 4)] = helperfunctions.loadImage(itemSheet, 3, 5, 32, 32)
        itemImages[(constants.TORSO(), 5)] = helperfunctions.loadImage(itemSheet, 4, 5, 32, 32)
        itemImages[(constants.TORSO(), 6)] = helperfunctions.loadImage(itemSheet, 5, 5, 32, 32)
        itemImages[(constants.TORSO(), 7)] = helperfunctions.loadImage(itemSheet, 6, 5, 32, 32)
        itemImages[(constants.TORSO(), 8)] = helperfunctions.loadImage(itemSheet, 7, 5, 32, 32)
        
        
        itemImages[(constants.DAGGER(), 0)] = helperfunctions.loadImage(itemSheet, 0, 6, 32, 32)
        itemImages[(constants.LONGSWORD(), 0)] = helperfunctions.loadImage(itemSheet, 1, 6, 32, 32)
        itemImages[(constants.RAPIER(), 0)] = helperfunctions.loadImage(itemSheet, 2, 6, 32, 32)
        itemImages[(constants.SPEAR(), 0)] = helperfunctions.loadImage(itemSheet, 3, 6, 32, 32)
        itemImages[(constants.STAFF(), 0)] = helperfunctions.loadImage(itemSheet, 4, 6, 32, 32)
        itemImages[(constants.LONGSPEAR(), 0)] = helperfunctions.loadImage(itemSheet, 5, 6, 32, 32)
        
    return itemImages


def getMagicImages():
    global magicImages
    
    if len(magicImages) == 0:
        magicImages = dict()
        healSheet = pygame.image.load("graphic/magic_pack/heal.png")
        
        for magicType in [constants.HEALMAGIC(), constants.FIREMAGIC(), constants.TELEPORTDEEPMAGIC(), constants.TELEPORTTOWNMAGIC()]:
            for direction in [constants.NORTH(), constants.SOUTH(), constants.WEST(), constants.EAST()]:
                magicImages[(magicType, direction)] = []
        
        for i in range(0, 4):
            magicImages[(constants.HEALMAGIC(), constants.NORTH())].append(helperfunctions.loadImage(healSheet, i, 0, 64, 64))
            for d in [constants.SOUTH(), constants.EAST(), constants.WEST()]:
                magicImages[(constants.HEALMAGIC(), d)].append( magicImages[(constants.HEALMAGIC(), constants.NORTH())][i])
        
        firelion_down = pygame.image.load("graphic/magic_pack/firelion_down.png")
        firelion_left = pygame.image.load("graphic/magic_pack/firelion_left.png")
        firelion_right = pygame.image.load("graphic/magic_pack/firelion_right.png")
        firelion_up = pygame.image.load("graphic/magic_pack/firelion_up.png")
        
        for y in range(0, 4):
            for x in range(0, 4):
                magicImages[(constants.FIREMAGIC(), constants.NORTH())].append(helperfunctions.loadImage(firelion_up, x, y, 64, 64))
                magicImages[(constants.FIREMAGIC(), constants.SOUTH())].append(helperfunctions.loadImage(firelion_down, x, y, 64, 64))
                magicImages[(constants.FIREMAGIC(), constants.WEST())].append(helperfunctions.loadImage(firelion_left, x, y, 64, 64))
                magicImages[(constants.FIREMAGIC(), constants.EAST())].append(helperfunctions.loadImage(firelion_right, x, y, 64, 64))
        
        
        teleport = pygame.image.load("graphic/magic_pack/tornado.png")
        for y in range(0, 3):
            for x in range(0, 4):
                for d in [constants.SOUTH(), constants.EAST(), constants.WEST(), constants.NORTH()]:
                    magicImages[(constants.TELEPORTDEEPMAGIC(), d)].append(helperfunctions.loadImage(teleport, x, y, 64, 64))
                    magicImages[(constants.TELEPORTTOWNMAGIC(), d)].append(helperfunctions.loadImage(teleport, x, y, 64, 64))
        
        
    return magicImages

def getMagicIcons():
    global magicIcons
    
    if len(magicIcons) == 0:
        iconSheet = pygame.image.load("graphic/magic_pack/icons.png")
        magicIcons[constants.HEALMAGIC()] = helperfunctions.loadImage(iconSheet, 0, 0)
        magicIcons[constants.FIREMAGIC()] = helperfunctions.loadImage(iconSheet, 1, 0)
        magicIcons[constants.TELEPORTTOWNMAGIC()] = helperfunctions.loadImage(iconSheet, 2, 0)
        magicIcons[constants.TELEPORTDEEPMAGIC()] = helperfunctions.loadImage(iconSheet, 3, 0)
        
        
    return magicIcons
def getMagics():
    global magicLevels
    if magicLevels == 0:
        # magicid, Level recuirement, mp to cast magic, name, description, 4 type, 5 timeToCast, 6 animationTime, 7 timeActive, 8effect, 9effectBonusFromMag
        
        magicLevels = dict()
        magicLevels[constants.HEALMAGIC()] = (1, 10, "Heal", "Heals caster for 50 + 5*mag\nover 4 sec", constants.BUFF(), 30, 10, 240, 50, 5)
        magicLevels[constants.FIREMAGIC()] = (1, 20, "Fire", "Throws a fireLion that damages enemys over 3 sec\nDMG: 60 + 3*mag", constants.DOT(), 30, 120, 180, 60, 3)
        magicLevels[constants.TELEPORTTOWNMAGIC()] = (1, 50, "TeleportTown", "Teleports caster to town", constants.DIRECT(), 60, 60, 60, 0, 0)
        magicLevels[constants.TELEPORTDEEPMAGIC()] = (1, 50, "TeleportDown", "Teleports caster to their deepest explored place", constants.DIRECT(), 60, 60, 60, 0, 0)
        
        
    return magicLevels

def getNPCImage(npcId, direction = constants.SOUTH()):
    global npcImages
    if len(npcImages) == 0:
        for i in range(1,3):
            sheet = pygame.image.load("graphic/npc/" + str(i) + ".png")
            npcImages[(i, constants.NORTH())] = helperfunctions.loadImage(sheet, 0, 0, 64, 64)
            npcImages[(i, constants.WEST())] = helperfunctions.loadImage(sheet, 0, 1, 64, 64)
            npcImages[(i, constants.SOUTH())] = helperfunctions.loadImage(sheet, 0, 2, 64, 64)
            npcImages[(i, constants.EAST())] = helperfunctions.loadImage(sheet, 0, 3, 64, 64)
    
    return npcImages[(npcId, direction)]


def getFire(imageId):
    global fire
    if len(fire) == 0:
        fire.append(pygame.image.load("graphic/fire/fire1_ 01.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 02.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 03.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 04.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 05.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 06.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 07.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 08.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 09.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 10.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 11.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 12.png"))
        fire.append(pygame.image.load("graphic/fire/fire1_ 13.png"))
        
    return fire[imageId % (len(fire) - 1)]
        

def getBossLevel():
    if getCurrentLevel() != 0:
        
        return [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                [0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,2,2,2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,2,2,0],
                [0,2,2,2,2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,2,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,2,2,2,2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,0],
                [0,2,2,2,2,2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,4,1,1,1,1,1,1,1,1,2,0],
                [0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0],
                [0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0],
                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
        
    
