import pygame, math, random
from pygame.math import Vector2
from game.ui import Effect

def snaptogrid(c):
    coords = round(c[0]/48), round(c[1]/48)
    coords = (coords[0]*48)+24, (coords[1]*48)+24
    return coords

props = {"goo" : [2, 0.03], "cube" : [3, 0.025], "skater" : [4, 0.07], "jetpack" : [5, 0.085], "mini" : [1, 0.05], "giant" : [75, 0.02]}

images = {}
for i in props.keys():
    images[i+"f"] = pygame.image.load("./images/enemies/"+str(i)+"/front.png")
    images[i + "b"] = pygame.image.load("./images/enemies/" + str(i) + "/back.png")
    images[i + "l"] = pygame.image.load("./images/enemies/" + str(i) + "/left.png")
    images[i + "r"] = pygame.image.load("./images/enemies/" + str(i) + "/right.png")

class Enemy(pygame.sprite.Sprite):
    def __init__(self, type, pos, path):
        global props, images
        pygame.sprite.Sprite.__init__(self)
        self.image = images[str(type)+"f"]
        self.images = {"front":images[str(type)+"f"],"back":images[str(type)+"b"],"left":images[str(type)+"l"],"right":images[str(type)+"r"]}
        self.rect = self.image.get_rect()
        self.rect.center = list([pos[0]*48, pos[1]*48])
        self.path = list(path)
        self.type = str(type)
        self.pathindex = 0
        self.health = props[self.type][0]
        self.basehealth = self.health
        self.direction = "front"
        self.target = [self.path[self.pathindex][0]*48, self.path[self.pathindex][1]*48]
        self.targetrect = pygame.surface.Surface([2, 2])
        self.targetrect = self.targetrect.get_rect()
        self.targetrect.center = self.target
        self.pos = [self.rect.centerx, self.rect.centery]
        self.centerrect = pygame.surface.Surface([4, 4])
        self.centerrect = self.centerrect.get_rect()
        self.centerrect.center = self.rect.center
        self.velocity = 0
        self.speed = props[self.type][1]
    def redirect(self, dir):
        dir = str(dir)
        if not dir == self.direction:
            self.direction = dir
            self.oldrect = self.rect
            self.image = self.images[dir]
            self.rect = self.image.get_rect()
            self.rect.center = self.oldrect.center
    def update(self, pa, fps, sounds, sfx):
        if self.health <= 0:
            sfx.play(sounds["kill"])
            self.kill()
            pa.addcash += round((0.1 * random.randint(8, 16)) * (self.basehealth))
        mx, my = 0, 0
        fps = round(fps)
        if self.rect.center != self.target:
            self.velocity = Vector2(self.target[0] - self.rect.centerx,
                                    self.target[1] - self.rect.centery).normalize() * fps * self.speed
        if self.rect.centerx < self.target[0]:
            self.redirect("right")
        elif self.rect.centerx > self.target[0]:
            self.redirect("left")
        elif self.rect.centery < self.target[1]:
            self.redirect("front")
        elif self.rect.centery > self.target[1]:
            self.redirect("back")
        else:
            self.redirect("front")
        mx = self.velocity.x
        my = self.velocity.y
        self.pos[0] += mx
        self.pos[1] += my
        self.rect.center = [math.floor(self.pos[0]), math.floor(self.pos[1])]
        self.centerrect.center = self.rect.center
        if [mx, my] == [0, 0] or self.centerrect.colliderect(self.targetrect):
            self.pos = self.target
            self.pathindex += 1
            if self.pathindex > len(self.path) - 1:
                pa.health -= self.health
                self.kill()
                if pa.screen == "game":
                    sfx.play(sounds["end"])
            else:
                self.target = [self.path[self.pathindex][0]*48, self.path[self.pathindex][1]*48]
                self.targetrect = pygame.surface.Surface([2,2])
                self.targetrect = self.targetrect.get_rect()
                self.targetrect.center = self.target