import pygame, random

class wall(pygame.sprite.Sprite):
    def __init__(self, pos, image):
        pygame.sprite.Sprite.__init__(self)
        if image == None:
            self.image = pygame.surface.Surface([80, 80])
        else:
            if image == "wallsplitup" or image == "interiorsplitup3" or image == "wallsplitdown" or image == "wallsplitdown2" or image == "interiorsplitup" or image == "interiorsplitdown" or image == "interiorsplitup2" or image == "interiorsplitdown2" or image == "interiorsplitdown3":
                self.image = pygame.transform.scale2x(pygame.image.load("./images/" + image + ".png"))
            else:
                self.image = pygame.transform.scale2x(pygame.image.load("./images/" + image + ".jpg"))
        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
    def update(self, action, screen, mouse, parameters):
        if action == "draw" and self.rect.left > -80 and self.rect.right < 1360 and self.rect.top > -80 and self.rect.bottom < 800:
            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)

class floor(pygame.sprite.Sprite):
    def __init__(self, pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale2x(pygame.image.load("./images/floor.jpg"))
        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
    def update(self, action, screen, mouse, parameters):
        if action == "draw" and self.rect.left > -80 and self.rect.right < 1360 and self.rect.top > -80 and self.rect.bottom < 800:
            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)

class dumpster(pygame.sprite.Sprite):
    def __init__(self, pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.surface.Surface([160, 80])
        self.image.fill([46, 184, 46])
        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.spaceleft = 10
        self.sound = pygame.mixer.Sound("./sfx/dump.wav")
    def update(self, action, screen, mouse, parameters):
        font = pygame.font.Font("./font/Lintsec.ttf", 36)
        if action == "draw" and self.rect.left > -80 and self.rect.right < 1360 and self.rect.top > -80 and self.rect.bottom < 800:
            if self.spaceleft == 0:
                self.image.fill([255, 0, 0])
            else:
                self.image.fill([46, 184, 46])
            screen.blit(self.image, [self.rect.left, self.rect.top])
            text = font.render(str(10 - self.spaceleft) + " - 10", 1, [255, 255, 255])
            screen.blit(text, [self.rect.left + 5, self.rect.top + 5])
        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 == "dump" and self.rect.collidepoint(mouse) and self.spaceleft > 0: #and self.rect.left >= 440 and self.rect.right <= 840 and self.rect.top >= 160 and self.rect.bottom <= 560:
            if parameters.trash <= self.spaceleft: #if there is enough space left to fit trash
                self.spaceleft -= parameters.trash
                if parameters.space < parameters.maxSpace: #if cart is being used
                    spaceToFill = parameters.maxSpace - parameters.space #find amount of space being used
                    if parameters.trash > spaceToFill: #if trash dumped is greater than space being used (must've used trash bags)
                        parameters.space = parameters.maxSpace #cart is cleared
                        parameters.trash -= spaceToFill #trash left to dump is reduced by space being used
                    else: #if trash dumped is not greater than space being used
                        parameters.space += parameters.trash #clear as much as possible
                        parameters.trash = 0 #no trash left
                print self.spaceleft
                parameters.trash = 0 #all trash cleared
                self.sound.play()
            elif parameters.trash > self.spaceleft:
                parameters.trash = self.spaceleft
                self.spaceleft = 0
                if parameters.space < parameters.maxSpace:
                    spaceToFill = parameters.maxSpace - parameters.space
                    if parameters.trash > spaceToFill:
                        parameters.space = parameters.maxSpace
                        parameters.trash -= spaceToFill
                    else:
                        parameters.space += parameters.trash
                        parameters.trash = 0
                print self.spaceleft
                self.sound.play()
            parameters.energy -= 1
        elif action == "clear":
            self.spaceleft = 10
