import pygame, random

items = {"expensive china" : 200, "cheap china" : 12, "Phonebook" : 5, "CRT Monitor" : 25, "Gaming Monitor" : 250, "Retro TV" : 40,
         "Plasma TV" : 65, "Flatscreen TV" : 100, "Curved Flatscreen TV" : 300}

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale2x(pygame.image.load("./images/player_right.png"))
        self.right = [pygame.transform.scale2x(pygame.image.load("./images/player_right.png")), pygame.transform.scale2x(pygame.image.load("./images/player_right2.png")), pygame.transform.scale2x(pygame.image.load("./images/player_right.png")), pygame.transform.scale2x(pygame.image.load("./images/player_right3.png"))]
        self.down = [pygame.transform.scale2x(pygame.image.load("./images/player_down.png")), pygame.transform.scale2x(pygame.image.load("./images/player_down2.png")), pygame.transform.scale2x(pygame.image.load("./images/player_down.png")), pygame.transform.scale2x(pygame.image.load("./images/player_down3.png"))]
        self.up = [pygame.transform.scale2x(pygame.image.load("./images/player_up.png")),
                     pygame.transform.scale2x(pygame.image.load("./images/player_up2.png")),
                     pygame.transform.scale2x(pygame.image.load("./images/player_up.png")),
                     pygame.transform.scale2x(pygame.image.load("./images/player_up3.png"))]
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = 580, 320
        self.x, self.y = 0, 0
        self.index = 0
        self.direction = [1, 0]
        self.moving = False

    def draw(self, screen):
        screen.blit(self.image, [self.rect.left, self.rect.top])

    def scroll(self, x, y):
        self.direction = [x, y]
        self.x += x
        self.y += y
        self.moving = True

    def animate(self):
        #self.image.fill([random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)])

        if self.moving:
            if self.direction[0] == 1:
                if self.index < 3:
                    self.index += 1
                else:
                    self.index = 0

                self.image = self.right[self.index]
            elif self.direction[0] == -1:
                if self.index < 3:
                    self.index += 1
                else:
                    self.index = 0
                self.image = pygame.transform.flip(self.right[self.index], True, False)
            elif self.direction[0] == 0:
                if self.direction[1] == 1:
                    if self.index < 3:
                        self.index += 1
                    else:
                        self.index = 0
                    self.image = pygame.transform.flip(self.down[self.index], True, False)
                if self.direction[1] == -1:
                    if self.index < 3:
                        self.index += 1
                    else:
                        self.index = 0
                    self.image = pygame.transform.flip(self.up[self.index], True, False)

class junk(pygame.sprite.Sprite):
    def __init__(self, pos):
        global items
        pygame.sprite.Sprite.__init__(self)
        self.weight = random.randint(1, 2)
        if self.weight == 1:
            if random.randint(0, 1) == 0:
                self.image = pygame.transform.scale2x(pygame.image.load("./images/junk.png"))
            else:
                self.image = pygame.transform.scale2x(pygame.image.load("./images/junk2.png"))
        elif self.weight == 2:
            self.image = pygame.transform.scale2x(pygame.image.load("./images/box.png"))
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = list(pos)
        self.sx, self.sy = pos
        self.x, self.y = 0, 0
        self.item = random.choice(items.keys())
        self.sound = pygame.mixer.Sound("./sfx/pickup.wav")
        self.sentimental = random.randint(1, 5)
    def update(self, action, screen, mouse, parameters):
        if action == "draw" and self.rect.left > -10 and self.rect.right < 1290 and self.rect.top > -10 and self.rect.bottom < 730:
            screen.blit(self.image, [self.rect.left, self.rect.top])
        elif action.startswith("scroll"):
            if action == "scrollup":
                self.x += 0
                self.y += 1
                self.rect.left = self.sx + (self.x * 20)
                self.rect.top = self.sy + (self.y * 20)
            elif action == "scrolldown":
                self.x += 0
                self.y -= 1
                self.rect.left = self.sx + (self.x * 20)
                self.rect.top = self.sy + (self.y * 20)
            elif action == "scrollright":
                self.x += 1
                self.y += 0
                self.rect.left = self.sx + (self.x * 20)
                self.rect.top = self.sy + (self.y * 20)
            elif action == "scrollleft":
                self.x -= 1
                self.y += 0
                self.rect.left = self.sx + (self.x * 20)
                self.rect.top = self.sy + (self.y * 20)
        elif action == "remove":
            global items
            if self.rect.collidepoint(mouse):

                if parameters.current == "cart":
                    if self.sentimental > 4:
                        parameters.parting = pygame.sprite.Group()
                        parameters.parting.add(self)
                    else:
                        parameters.space -= 1
                        parameters.trash += 1
                        if random.randint(1, 10) > 6:
                            parameters.money += items[self.item]
                        self.kill()
                        self.sound.play()
                        parameters.energy -= 1


                elif parameters.current == "trash bags" and self.weight == 1:
                    if self.sentimental > 4:
                        parameters.parting = pygame.sprite.Group()
                        parameters.parting.add(self)
                    else:
                        parameters.bags -= 1
                        parameters.trash += 1
                        if random.randint(1, 10) > 6:
                            parameters.money += items[self.item]
                        self.kill()
                        self.sound.play()
                        parameters.energy -= 1
        elif action == "removefromparting":
            parameters.parting.remove(self)
        elif action == "kill":
            if parameters.current == "trash bags":
                parameters.bags -= 1
                parameters.trash += 1
                if random.randint(1, 10) > 6:
                    parameters.money += items[self.item]
                self.kill()
                parameters.mood -= random.randint(15, 25)
                parameters.energy -= 1
            elif parameters.current == "cart":
                parameters.bags -= 1
                parameters.space += 1
                if random.randint(1, 10) > 6:
                    parameters.money += items[self.item]
                self.kill()
                parameters.mood -= random.randint(15, 25)
                parameters.energy -= 1
