# 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/>.


from magic import Magic
import constants
import g
import pygame
from pygame.locals import *

class Spellbook():
    def __init__(self, level, mp):
        self.pageIndex = 0
        
        self.level = level
        self.mp = mp
        
        self.magicBar = [None, constants.HEALMAGIC(), constants.FIREMAGIC(), constants.TELEPORTTOWNMAGIC(), constants.TELEPORTDEEPMAGIC(), None, None, None, None, None] 
        self.magicBarImage = None 
        self.magicImageGrayed = None

        self.generateBar()

    def generateBar(self):
        rect = pygame.Rect((0,0, 320, 32))
        self.magicBarImage = pygame.Surface(rect.size, pygame.SRCALPHA)
        
        for i in range(1,10):
            if self.magicBar[i] != None:
                self.magicBarImage.blit(g.getMagicIcons()[self.magicBar[i]], (32*(i-1),0))
        
        if self.magicBar[0] != None:
                self.magicBarImage.blit(g.getMagicIcons()[self.magicBar[0]], (0,0))
            
    def getBarImage(self):
        return self.magicBarImage
    
    def getMagics(self):
        return ["apa", "bepa"]
    
    def setNewLevels(self, level, mp):
        self.level = level
        self.mp = mp
    
    def generateMagics(self):
        self.magics = []
        for (level, mp, magicType, name, text) in g.getMagic():
            if self.level >= level and self.mp >= mp:
                self.magics.append(magicType)
    
    
    def castMagic(self, num, caster, targets = None):
        if num > 9 or num < 0:
            return None
        if self.magicBar[num] != None:
            g.addMagic(Magic(self.magicBar[num], caster, targets))
        
    
    def setMagicInBar(self, magicType, place):
        self.magicBar[place] = magicType
        
        
    
    def getText(self, magicType):
        for (level, mp, m, name, text) in g.getMagics():
            if m == magicType:
                return (name, text)
            
        return ""
        