import pygame

import constants
import platforms

class Level():
    """ This is a generic super-class used to define a level.
        Create a child class for each level with level-specific
        info. """

    # Lists of sprites used in all levels. Add or remove
    # lists as needed for your game. """
    platform_list = None
    enemy_list = None

    # Background image
    background = None

    # How far this world has been scrolled left/right
    world_shift = 0
    level_limit = -1000

    def __init__(self, player):
        """ Constructor. Pass in a handle to player. Needed for when moving platforms
            collide with the player. """
        self.platform_list = pygame.sprite.Group()
        self.enemy_list = pygame.sprite.Group()
        self.player = player

    # Update everythign on this level
    def update(self):
        """ Update everything in this level."""
        self.platform_list.update()
        self.enemy_list.update()

    def draw(self, screen):
        """ Draw everything on this level. """

        # Draw the background
        # We don't shift the background as much as the sprites are shifted
        # to give a feeling of depth.
        screen.fill(constants.WHITE)
        screen.blit(self.background,(self.world_shift // 3,0))

        # Draw all the sprite lists that we have
        self.platform_list.draw(screen)
        self.enemy_list.draw(screen)

    def shift_world(self, shift_x):
        """ When the user moves left/right and we need to scroll everything: """

        # Keep track of the shift amount
        self.world_shift += shift_x

        # Go through all the sprite lists and shift
        for platform in self.platform_list:
            platform.rect.x += shift_x

        for enemy in self.enemy_list:
            enemy.rect.x += shift_x

# Create platforms for the level
class Level_01(Level):
    """ Definition for level 1. """

    def __init__(self, player):
        """ Create level 1. """

        # Call the parent constructor
        Level.__init__(self, player)

        self.background = pygame.image.load("background_01.png").convert()
        self.background.set_colorkey(constants.WHITE)
        self.level_limit = -1500

        # Array with type of platform, and x, y location of the platform.
        level = [ [platforms.GRASS_LEFT, 400, 500],
                  [platforms.GRASS_MIDDLE, 470, 500],
                  [platforms.GRASS_MIDDLE, 540, 500],
                  [platforms.GRASS_MIDDLE, 610, 500],
                  [platforms.GRASS_RIGHT, 680, 500],
                  [platforms.GRASS_LEFT, 800, 400],
                  [platforms.GRASS_MIDDLE, 870, 400],
                  [platforms.GRASS_RIGHT, 940, 400],
                  [platforms.GRASS_LEFT, 1000, 500],
                  [platforms.GRASS_RIGHT, 1070, 500],
                  [platforms.STONE_PLATFORM_LEFT, 1120, 330],
                  [platforms.STONE_PLATFORM_MIDDLE, 1190, 330],
                  [platforms.STONE_PLATFORM_RIGHT, 1260, 330],
                  [platforms.STONE_PLATFORM_MIDDLE, 480, 390],
                  [platforms.STONE_PLATFORM_MIDDLE, 550, 350],
                  [platforms.STONE_PLATFORM_MIDDLE, 700, 260],
                  [platforms.STONE_PLATFORM_MIDDLE, 770, 220], 
                  [platforms.STONE_PLATFORM_MIDDLE, 840, 180], 
                  [platforms.STONE_PLATFORM_MIDDLE, 910, 140], 
                  [platforms.STONE_PLATFORM_MIDDLE, 980, 100],                  
                  [platforms.STONE_PLATFORM_MIDDLE, 1050, 60],
                  [platforms.STONE_PLATFORM_MIDDLE, 1120, 20],
                  [platforms.STONE_PLATFORM_MIDDLE, 1190, -20], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1260, -60], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1330, -100],  
                  [platforms.STONE_PLATFORM_MIDDLE, 1400, -140],
                  [platforms.STONE_PLATFORM_MIDDLE, 1470, -180], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1470, -140], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1470, -100], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1470, -60], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1470, -20], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1470, 20], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1540, 20], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1610, 20],
                  [platforms.STONE_PLATFORM_MIDDLE, 1610, 180],
                  [platforms.STONE_PLATFORM_MIDDLE, 1680, 20],
                  [platforms.STONE_PLATFORM_MIDDLE, 1750, 20], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1820, 20], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1820, -20],  
                  [platforms.STONE_PLATFORM_MIDDLE, 1820, -60], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1820, -100], 
                  [platforms.STONE_PLATFORM_MIDDLE, 1820, -140], 
                  [platforms.GRASS_LEFT, 1320, 500],
                  [platforms.GRASS_RIGHT, 1390, 500],
                  [platforms.GRASS_LEFT, 1360, 400],
                  [platforms.GRASS_RIGHT, 1430, 400],
                  [platforms.GRASS_LEFT, 1620, 330],
                  [platforms.GRASS_RIGHT, 1690, 330],
                  [platforms.GRASS_LEFT, 1700, 480],
                  [platforms.GRASS_RIGHT, 1770, 480],
                  
                  [platforms.SIGN_RIGHT, 2600, 550],
                  
                  ]

        # Go through the array above and add platforms
        for platform in level:
            block = platforms.Platform(platform[0])
            block.rect.x = platform[1]
            block.rect.y = platform[2]
            block.player = self.player
            self.platform_list.add(block)

        # Add a custom moving platform
        block = platforms.MovingPlatform(platforms.STONE_PLATFORM_MIDDLE)
        block.rect.x = 1500 
        block.rect.y = 220
        block.boundary_left = 1500
        block.boundary_right = 1650
        block.change_x = 1
        block.player = self.player
        block.level = self
        self.platform_list.add(block)

        block = platforms.MovingPlatform(platforms.STONE_PLATFORM_MIDDLE)
        block.rect.x = 1840 
        block.rect.y = 400
        block.boundary_left = 1840
        block.boundary_right = 2000
        block.change_x = 1
        block.player = self.player
        block.level = self
        self.platform_list.add(block)

        block = platforms.MovingPlatform(platforms.GRASS_MIDDLE)
        block.rect.x = 200 
        block.rect.y = 280
        block.boundary_left = 200
        block.boundary_right = 380
        block.change_x = 1
        block.player = self.player
        block.level = self
        self.platform_list.add(block)
        
# Create platforms for the level
class Level_02(Level):
    """ Definition for level 2. """

    def __init__(self, player):
        """ Create level 1. """

        # Call the parent constructor
        Level.__init__(self, player)

        self.background = pygame.image.load("background_02.png").convert()
        self.background.set_colorkey(constants.WHITE)
        self.level_limit = -1200

        # Array with type of platform, and x, y location of the platform.
        level = [ [platforms.STONE_PLATFORM_LEFT, 500, 550],
                  [platforms.STONE_PLATFORM_MIDDLE, 570, 550],
                  [platforms.STONE_PLATFORM_RIGHT, 640, 550],
                  [platforms.GRASS_LEFT, 800, 400],
                  [platforms.GRASS_MIDDLE, 870, 400],
                  [platforms.GRASS_RIGHT, 940, 400],
                  [platforms.GRASS_LEFT, 1000, 500],
                  [platforms.GRASS_MIDDLE, 1070, 500],
                  [platforms.GRASS_RIGHT, 1140, 500],
                  [platforms.STONE_PLATFORM_LEFT, 1120, 280],
                  [platforms.STONE_PLATFORM_MIDDLE, 1190, 280],
                  [platforms.STONE_PLATFORM_RIGHT, 1260, 280],
                  [platforms.STONE_PLATFORM_MIDDLE, 1260, 200],
                  [platforms.GRASS_LEFT, 1300, 500],
                  [platforms.GRASS_RIGHT, 1370, 500],
                  [platforms.STONE_PLATFORM_LEFT, 1310, 420],
                  [platforms.STONE_PLATFORM_RIGHT, 1380, 420],
                  [platforms.STONE_PLATFORM_MIDDLE, 1550, 500],
                  [platforms.STONE_PLATFORM_MIDDLE, 1690, 500],
                  [platforms.STONE_PLATFORM_MIDDLE, 1620, 460],
                  [platforms.STONE_PLATFORM_MIDDLE, 1550, 420],
                  [platforms.STONE_PLATFORM_MIDDLE, 1550, 380],
                  [platforms.STONE_PLATFORM_MIDDLE, 1690, 170],
                  [platforms.STONE_PLATFORM_MIDDLE, 1690, 250],
                  [platforms.STONE_PLATFORM_MIDDLE, 1690, 330],
                  [platforms.STONE_PLATFORM_MIDDLE, 1760, 330],
                  [platforms.STONE_PLATFORM_MIDDLE, 1830, 330],
                  [platforms.STONE_PLATFORM_MIDDLE, 1900, 330],
                  [platforms.STONE_PLATFORM_MIDDLE, 1970, 330],
                  [platforms.STONE_PLATFORM_MIDDLE, 1970, 290],
                  [platforms.STONE_PLATFORM_MIDDLE, 1970, 210],
                  [platforms.STONE_PLATFORM_MIDDLE, 1970, 130],
                  [platforms.GRASS_LEFT, 1900, 380],
                  [platforms.GRASS_RIGHT, 1970, 380],
                  
                  [platforms.SIGN_EXIT, 2300, 550],
                  ]


        # Go through the array above and add platforms
        for platform in level:
            block = platforms.Platform(platform[0])
            block.rect.x = platform[1]
            block.rect.y = platform[2]
            block.player = self.player
            self.platform_list.add(block)

        # Add a custom moving platform
        block = platforms.MovingPlatform(platforms.STONE_PLATFORM_MIDDLE)
        block.rect.x = 1500
        block.rect.y = 300
        block.boundary_top = 100
        block.boundary_bottom = 350
        block.change_y = -1
        block.player = self.player
        block.level = self
        self.platform_list.add(block)
