from gen import generate

class BlockStorage(object):
    def __init__(self):
        self._blocks = {}

    def __getitem__(self, x, y, z):
        return self._blocks.get((x, y, z), None)

    def __setitem__(self, *largs):
        x, y, z = largs[0]
        
        assert isinstance(x, int)
        assert isinstance(y, int)
        assert isinstance(z, int)

        if x < 0: return
        if y < 0: return
        if z < 0: return

        if x > 15: return
        if y > 15: return
        if z > 256: return

        if largs[1] is None:
            if (x, y, z) in self._blocks:
                del self._blocks[(x, y, z)]
        else:
            self._blocks[(x, y, z)] = largs[1]

class Chunk(object):
    def __init__(self, master, x, y, dimension=0):
        self.master = master
        self.position = (x, y)
        self.dimension = 0
        self.blocks = BlockStorage()
        self.loaded = False

    def get_real_pos(self):
        return [self.position[0] * 16,
                self.position[1] * 16]

    def __getitem__(self, *largs):
        return self.blocks[largs[0]]

    def __setitem__(self, *largs):
        x, y, z = largs[0]
        self.blocks[x, y, z] = largs[1]

    def load(self):
        if self.loaded:
            return False
        
        return False

    def generate(self):
        if self.loaded:
            return False

        return generate(self.master, self)
