"""
->PART OF PROJECT CODEX<-
Contains data for Spell classes and their methods.
"""
from constants import *
import pygame
import math
from object_classes import *
from soundC import *


class Spell(object): #Parent class

	spells_casted = []
	
	def __init__(self):

		self.name = "Undefined_spell"

		self.x, self.y = None, None
		self.startx, self.starty = None, None
		self.targetx, self.targety = None, None
		self.level = 1
		self.damage = 1
		self.knockbakc = 1
		self.cooldown = 1
		self.speed = 1
		self.range = 1
		self.speedx, self.speedy = 0, 0
		self.hypo = 0
		self.cast_time = 1
		self.sound = None
		self.data_sent = False

		self.MasterSprite = pygame.image.load("images/spells/nosprite.png")
		self.sprite = self.MasterSprite

		self.animation_index = 0
		self.animation_speed = 0
		self.animation_tick = 0

		self.cdtick = 0
		self.deltaframes = 0

	def copy(self):
		if self.name == 'FIREBOLT':
			spell = Firebolt(self.level)
		elif self.name == 'LIGHTNING_STRIKE':
			spell = LightningStrike(self.level)

		return spell

	def resize_sprite(self):
		pass

	def animate(self):
		pass

	def prepare(self, player, Mpos):

		self.x = player.x
		self.y = player.y
		self.targetx = Mpos[0]
		self.targety = Mpos[1]

		deltaX = self.targetx - self.x
		deltaY = self.targety - self.y
		hypo = math.sqrt(deltaX ** 2 + deltaY ** 2)

		self.speedx = (deltaX/hypo * self.speed)
		self.speedy = (deltaY/hypo * self.speed)

		self.resize_sprite()

	@staticmethod
	def update_spells(screen):
		for spell in Spell.spells_casted:
			spell.update(screen)

"""-----------------------------SPELL TYPES (UPDATE FUNCTION IS CORE)----------------------------------"""

class Bolt(Spell):

	def __init__(self):

		super(Bolt, self).__init__()

	def update(self, screen):

		self.x += self.speedx
		self.y += self.speedy

		self.animate()

		screen.blit(self.sprite, (int(self.x),int(self.y)))

class Link(Spell):

	def __init__(self):
		super(Link, self).__init__()

	def resize_sprite(self):
		self.sprite = pygame.transform.scale(self.MasterSprite, (int(self.hypo/6), int(self.hypo)))
		#self.sprite = pygame.transform.rotate(self.MasterSprite, 90)

	def update(self, screen):

		if self.deltaframes > self.speed:
			Spell.spells_casted.remove(self)
		else:
			self.deltaframes += 1

		screen.blit(self.sprite, (int(self.targetx - self.sprite.get_width()/2),int(self.targety)))

class Sky(Spell):

	def __init__(self):
		super(Sky, self).__init__()

	def update(self, screen):

		if self.deltaframes > self.speed:
			Spell.spells_casted.remove(self)
		else:
			self.deltaframes += 1

		screen.blit(self.sprite, (int(self.targetx - self.sprite.get_width()/2),int(self.targety - self.sprite.get_height())))

"""---------------------------------------ACTUAL SPELLS-------------------------------------"""			

class Firebolt(Bolt):

	def __init__(self, level):

		super(Firebolt, self).__init__()

		self.parent = 'Bolt'

		self.name = "FIREBOLT"

		self.level = level
		self.damage = 10
		self.knockback = 10
		self.cooldown = 5
		self.speed = 7
		self.range = 500
		self.cast_time = 20

		self.MasterSprite = pygame.image.load("images/spells/firebolt.png")
		self.sprite = self.MasterSprite

		self.animation_speed = 3

		self.sound = Sound.Sounds["firebolt"]

	def animate(self):
		if self.animation_tick == self.animation_speed:
			self.sprite = pygame.transform.rotate(self.sprite, 90)
			self.animation_tick = 0
		else:
			self.animation_tick += 1
			print self.animation_tick

class LightningStrike(Sky):

	def __init__(self, level):

		super(LightningStrike, self).__init__()

		self.parent = 'Sky'

		self.name = "LIGHTNING_STRIKE"

		self.level = level
		self.damage = 15
		self.knockback = 10
		self.cooldown = 5
		self.speed = 10
		self.range = None
		self.rotate = True
		self.cast_time = 30

		self.MasterSprite = pygame.image.load("images/spells/lightningstrike.png")
		self.sprite = self.MasterSprite

		self.sound = Sound.Sounds["lightningstrike"]

	def update(self, screen):

		if self.deltaframes > self.speed:
			Spell.spells_casted.remove(self)
		else:
			self.deltaframes += 1

		if self.rotate == True:
			self.sprite = pygame.transform.rotate(self.MasterSprite, 5)
			self.rotate = False
		elif self.rotate == False:
			self.sprite = pygame.transform.rotate(self.MasterSprite, -5)
			self.rotate = True

		screen.blit(self.sprite, (int(self.targetx - self.sprite.get_width()/2),int(self.targety - self.sprite.get_height())))