local ____exports = {}
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local EffectVariant = ____isaac_2Dtypescript_2Ddefinitions.EffectVariant
local RoomShape = ____isaac_2Dtypescript_2Ddefinitions.RoomShape
local ____cachedClasses = require("core.cachedClasses")
local game = ____cachedClasses.game
local ____LadderSubTypeCustom = require("enums.LadderSubTypeCustom")
local LadderSubTypeCustom = ____LadderSubTypeCustom.LadderSubTypeCustom
local ____array = require("functions.array")
local getRandomArrayElement = ____array.getRandomArrayElement
local ____entitiesSpecific = require("functions.entitiesSpecific")
local spawnEffectWithSeed = ____entitiesSpecific.spawnEffectWithSeed
local ____string = require("functions.string")
local removeCharactersBefore = ____string.removeCharactersBefore
local ____constants = require("classes.features.other.customStages.constants")
local ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH = ____constants.ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH
--- Normally, we would make a custom entity to represent a shadow effect, but we don't want to
-- interfere with the "entities2.xml" file in end-user mods. Thus, we must select a vanilla effect
-- to masquerade as a backdrop effect.
-- 
-- We arbitrarily choose a ladder for this purpose because it will not automatically despawn after
-- time passes, like most other effects.
local SHADOW_EFFECT_VARIANT = EffectVariant.LADDER
local SHADOW_EFFECT_SUB_TYPE = LadderSubTypeCustom.CUSTOM_SHADOW
--- The animation comes from StageAPI.
local ROOM_SHAPE_TO_SHADOW_ANIMATION = {
    [RoomShape.SHAPE_1x1] = "1x1",
    [RoomShape.IH] = "1x1",
    [RoomShape.IV] = "1x1",
    [RoomShape.SHAPE_1x2] = "1x2",
    [RoomShape.IIV] = "1x2",
    [RoomShape.SHAPE_2x1] = "2x1",
    [RoomShape.IIH] = "2x1",
    [RoomShape.SHAPE_2x2] = "2x2",
    [RoomShape.LTL] = "2x2",
    [RoomShape.LTR] = "2x2",
    [RoomShape.LBL] = "2x2",
    [RoomShape.LBR] = "2x2"
}
local FADED_BLACK = Color(0, 0, 0, 0.25)
function ____exports.setShadows(self, customStage)
    if customStage.shadows == nil then
        return
    end
    local room = game:GetRoom()
    local roomShape = room:GetRoomShape()
    local centerPos = room:GetCenterPos()
    local animation = ROOM_SHAPE_TO_SHADOW_ANIMATION[roomShape]
    local shadows = customStage.shadows[animation]
    if shadows == nil then
        return
    end
    local seed = 1
    local shadowEffect = spawnEffectWithSeed(
        nil,
        SHADOW_EFFECT_VARIANT,
        SHADOW_EFFECT_SUB_TYPE,
        centerPos,
        seed
    )
    local sprite = shadowEffect:GetSprite()
    sprite:Load(ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH .. "/stage-shadow.anm2", false)
    local decorationSeed = room:GetDecorationSeed()
    local shadow = getRandomArrayElement(nil, shadows, decorationSeed)
    local pngPath = removeCharactersBefore(nil, shadow.pngPath, "gfx/")
    sprite:ReplaceSpritesheet(0, pngPath)
    sprite:LoadGraphics()
    sprite:SetFrame(animation, 0)
    sprite.Color = shadow.color == nil and FADED_BLACK or Color(shadow.color.r, shadow.color.g, shadow.color.b, shadow.color.a)
end
return ____exports
