import pygame
import os.path
import random
import math
from collections import deque
from pygame.locals import *
from constants import *

class TileSprites(pygame.sprite.DirtySprite):
    def __init__(self, image):
       super(TileSprites, self).__init__()
       self.image = image.copy()
       #self.image.fill((random.randint(0, 255), 0, 0))
       self.image.blit(image, (0,0))
       self.rect = self.image.get_rect()

class Tile(pygame.sprite.DirtySprite):
    """
    An abstract tile class
    """
    def __init__(self, map, tile_location):
        # Call the parent class (Sprite) constructor
        super(Tile, self).__init__()

        # Map this tile belongs to
        self.map = map

        # This tiles grid location and layer in the group
        self.tile_location = {'x': tile_location[0], 'y': tile_location[1]}
        self.layer = tile_location[1]

        # What type of tile is it?
        self.type = None

        # Does it need to be redrawn?
        self.dirty = 1

        # This tiles image surface
        self.image = I_TILES['grass_0']
        self.image = pygame.Surface((TILE_WIDTH, TILE_HEIGHT), 0, self.image)
        self.rect = self.image.get_rect()
        self.rect.x = self.tile_location['x'] * TILE_WIDTH
        self.rect.y = self.tile_location['y'] * TILE_FLOOR_HEIGHT
        self.tile_layers = pygame.sprite.OrderedUpdates()

        # TODO: Other Bits
        self.unit = None
        self.colour_overlay = None
        self.decoration = None
        self.passable = False

    def draw(self):
        """
        Method for drawing this tiles layers
        """
        self.map.dirty = 1
        self.tile_layers.draw(self.image)

    def clear_callback(self, surf, rect):
        """
        Method for clearing dirty tile areas
        """
        color = 0, 0, random.randint(0, 255)
        surf.fill(color, rect)


class TileGrass(Tile):
    """
    The default grass tile which has decoractions
    """
    def __init__(self, map, tile_location, type = "grass_0"):
        super(TileGrass, self).__init__(map, tile_location)

        self.type = type
        self.passable = True

        # Add background to the tile
        self.layer_background = TileSprites(I_TILES[type])
        self.unit_sprite = TileSprites(I_UNITS['unit_0'])
        self.tile_layers.add(self.layer_background, self.unit_sprite)
        self.draw()

class TileWall(Tile):
    """
    The default wall tile which cannot be entered
    """
    def __init__(self, map, tile_location, type = "wall_0"):
        super(TileWall, self).__init__(map, tile_location)
        self.type = type
        self.passable = False

        self.layer_background = TileSprites(I_TILES[type])
        self.tile_layers.add(self.layer_background)
        self.draw()

class TileWater(Tile):
    """
    The default water tile which can be entered, but perhaps at a higher cost?
    """
    def __init__(self, map, tile_location, type = "water_0"):

        super(TileWater, self).__init__(map, tile_location)
        self.type = type
        self.passable = True

        self.layer_background = TileSprites(I_TILES[type])
        self.tile_layers.add(self.layer_background)
        self.draw()