# 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 pygame
import constants
import g
from magiceffect import MagicEffect

class Magic():
    def __init__(self, magicType, caster, targets):
        self.magicType = magicType
        self.caster = caster
        self.direction = caster.direction
        self.targets = targets
        self.magic = g.getMagics()[magicType]
        self.frames = self.framesMax = self.magic[6]
        
        if self.magicType in [constants.FIREMAGIC()]:
            x = caster.getHitRect().left
            y = caster.getHitRect().top

            if self.direction == constants.NORTH():
                self.x = x
                self.y = y - 64
            elif self.direction == constants.SOUTH():
                self.x = x
                self.y = y
            elif self.direction == constants.WEST():
                self.x = x - 48
                self.y = y - 32
            else:
                self.x = x + 48
                self.y = y - 32
        else:
            self.x = caster.x
            self.y = caster.y
        
    def update(self):
        if self.magicType in [constants.TELEPORTDEEPMAGIC(), constants.TELEPORTTOWNMAGIC(), constants.HEALMAGIC()]:
            x = self.caster.getHitRect().left
            y = self.caster.getHitRect().top
            self.x = x
            self.y = y - 32
            
        if self.frames == self.framesMax:
            if self.magicType == constants.HEALMAGIC():
                
                added = False
                for buff in g.getPlayer().buffs:
                    if buff.type == constants.HEALMAGIC():
                        buff.timeLeft = self.magic[7]
                        added = True 
                
                if not added:
                    amount = self.magic[8] + self.caster.mag * self.magic[9]
                    amount /= 12
                    
                    g.getPlayer().buffs.append(MagicEffect(self.magicType, self.caster, amount, 10))
        
        self.frames -= 1
        

        if self.magicType == constants.FIREMAGIC():
            for t in self.targets:
                if t.getHitRect().colliderect(self.getHitRect()):
                    added = False
                    for d in t.dots:
                        if d.type == constants.FIREMAGIC():
                            added = True
                    
                    if not added:
                        amount = self.magic[8] + self.caster.mag * self.magic[9]
                        amount /= 12
                        t.dots.append(MagicEffect(constants.FIREMAGIC(), t, amount, 10))
            
            pass
        
        if self.frames < 0:
            if self.magicType == constants.TELEPORTTOWNMAGIC():
                g.setCurrentLevel(0)
            elif self.magicType == constants.TELEPORTDEEPMAGIC():
                g.setCurrentLevel(self.caster.deepestLevel)
            
            g.removeMagic(self)    
                
    
    def getHitRect(self):
        return pygame.rect.Rect(self.x, self.y, 64, 64)
    
    def getImage(self):
        magicBla = g.getMagicImages()[(self.magicType, self.direction)]
        
        imageId = (self.framesMax-self.frames)/(self.framesMax/(len(magicBla)))
        imageId = min(imageId, (len(magicBla) - 1))
        
        return magicBla[imageId]
        
    """def getPos(self):
        if self.magicType in [constants.TELEPORTDEEPMAGIC(), constants.TELEPORTTOWNMAGIC(), constants.HEALMAGIC()]:
            return (320-32,240-64)
        
        return (240,240)
    """
