from functools import partial

from core.terraform.helper import plantOrganicAction, placeTiles
from core.terraform.stamps import createStampColor, loadStamps

from siege import game
from siege.util import RangeUint, Vector
from siege.worldgen import Biome, FillMode
from siege.world.realm import Layer


class ScorchedBiome(Biome):
    def __init__(self):
        Biome.__init__(self)
        self.name = "Scorched"
        self.layer = "surface"
        self.frequency = 10
        self.width = RangeUint(4, 10)
        self.height = RangeUint(30, 50)
        self.heightTolerance = 3
        self.widthIncrement = 32
        self.groundId = game.content.get('dirt').id
        self.wallId = self.groundId
        self.sky = 'mods/core/environment/scorched/sky.png'
        self.back1.images.append('mods/core/environment/scorched/back1.png')
        self.back1.offset = -100
        self.back1.scroll = Vector(0.5, 0.1)
        self.back2.images.append('mods/core/environment/scorched/back2.png')
        self.back2.offset = -150
        self.back2.scroll = Vector(0.3, 0.075)
        self.back3.images.append('mods/core/environment/scorched/back3.png')
        self.back3.offset = -200
        self.back3.scroll = Vector(0.1, 0.05)

    def reset(self, area):
        pass

    def isCompatible(self, biome):
        return True

    def getOccurrences(self, realmSize):
        return RangeUint(1, 9999)

    def getStamps(self, remainingSpace):
        return loadStamps('scorched')

    def getRules(self):
        tileRules = {}
        tileRules[createStampColor(100, 80, 60)] = (self.groundId, 0)
        tileRules[createStampColor(80, 60, 40)] = (self.groundId, self.wallId)
        tileRules[createStampColor(154, 126, 98)] = (0, self.wallId)
        tileRules[createStampColor(48, 141, 255)] = (self.groundId, self.wallId)  # This is to fill cave entrances with with solid tile
        tileRules[createStampColor(91, 84, 75)] = (game.content.get('soot').id, 0)
        return tileRules

    def getActions(self, biomeData):
        frequency = 1 / 10000.0
        actions = []
        actions.append(partial(placeTiles, Layer.Ground, '', 3 * frequency, (2, 5), (2, 5), (0.25, 1.0), FillMode.SOLID_ONLY, "holes"))
        actions.append(partial(placeTiles, Layer.Ground, '', 3 * frequency, (5, 10), (5, 10), (0.0, 1.0), FillMode.SOLID_ONLY, "holes"))
        actions.append(partial(placeTiles, Layer.WallAndGround, 'stone', 5 * frequency, (5, 10), (5, 10), (0.0, 1.0), FillMode.SOLID_ONLY, "tile_small"))
        actions.append(partial(placeTiles, Layer.WallAndGround, 'stone', 2 * frequency, (8, 15), (8, 15), (0.0, 1.0), FillMode.SOLID_ONLY, "tile_medium"))
        actions.append(partial(placeTiles, Layer.Ground, 'copper_ore', 4 * frequency, (2, 4), (2, 4), (0.4, 1.0), FillMode.SOLID_ONLY, "tile_small"))
        actions.append(partial(placeTiles, Layer.Ground, 'copper_ore', 2 * frequency, (4, 5), (4, 5), (0.6, 1.0), FillMode.SOLID_ONLY, "tile_medium"))
        actions.append(partial(placeTiles, Layer.Ground, 'iron_ore', 2 * frequency, (2, 5), (2, 5), (0.9, 1.0), FillMode.SOLID_ONLY, "tile_small"))
        return actions

    def getPostActions(self, biomeData):
        actions = []
        actions.append(partial(plantOrganicAction, 'dead_tree', Layer.Back, (7, 11), True, False))
        actions.append(partial(plantOrganicAction, 'ashberry_plant', Layer.Item, (4, 8), True, False))
        actions.append(partial(plantOrganicAction, 'ghost_plant', Layer.Item, (4, 8), True, False))
        return actions
