------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------

local assets =
{
	Asset("ANIM", "anim/swap_pirate_booty_bag.zip"),

	Asset("ATLAS", "inventoryimages/piratepack.xml"),
    Asset("IMAGE", "inventoryimages/piratepack.tex"),
}

------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------

local containers = require("containers")

local oldwidgetsetup = containers.widgetsetup
containers.widgetsetup = function(container, prefab)
    if not prefab and container.inst.prefab == "piratepack" then
        prefab = "krampus_sack"
    end
    oldwidgetsetup(container, prefab)
end

local function onequip(inst, owner)
	owner.AnimState:OverrideSymbol("swap_body", "swap_pirate_booty_bag", "swap_body")
    inst.components.container:Open(owner)
end

local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")
    inst.components.container:Close(owner)
end

local function onopen(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")
end

local function onclose(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")
end

local function onburnt(inst)
    if inst.components.container ~= nil then
        inst.components.container:DropEverything()
        inst.components.container:Close()
    end
    SpawnPrefab("ash").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst:Remove()
end

local function onignite(inst)
    if inst.components.container ~= nil then
        inst.components.container.canbeopened = false
    end
end

local function onextinguish(inst)
    if inst.components.container ~= nil then
        inst.components.container.canbeopened = true
    end
end
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------

local function fn(Sim)
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("pirate_booty_bag")
    inst.AnimState:SetBuild("swap_pirate_booty_bag")
    inst.AnimState:PlayAnimation("anim")

    inst:AddTag("backpack")
		inst:AddTag("heavy")
		inst:AddTag("structure")
		inst:AddTag("chest")

	local minimap = inst.entity:AddMiniMapEntity()
	minimap:SetIcon("piratepack.tex")

    if not TheWorld.ismastersim then
        return inst
    end

    inst.entity:SetPristine()

    inst:AddComponent("inspectable")

   	inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "piratepack"
    inst.components.inventoryitem.atlasname = "inventoryimages/piratepack.xml"
	inst.components.inventoryitem.cangoincontainer = false

    inst:AddComponent("equippable")
		if EQUIPSLOTS["BACK"] then --for additional equipslot mods
			inst.components.equippable.equipslot = EQUIPSLOTS.BACK
		else
			inst.components.equippable.equipslot = EQUIPSLOTS.BODY
		end

    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    inst:AddComponent("container")
    inst.components.container:WidgetSetup("krampus_sack")
		inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose

		MakeSmallBurnable(inst, TUNING.MED_BURNTIME)
	  MakeSmallPropagator(inst)
		inst.components.burnable:SetOnBurntFn(onburnt)
	  inst.components.burnable:SetOnIgniteFn(onignite)
	  inst.components.burnable:SetOnExtinguishFn(onextinguish)

	MakeHauntableLaunch(inst)

    return inst
end

------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------

STRINGS.NAMES.PIRATEPACK                             = "Pirate Pack"
STRINGS.RECIPE_DESC.PIRATEPACK                       = "A Pirate's getaway chest!"

STRINGS.CHARACTERS.GENERIC.DESCRIBE.PIRATEPACK       = "I now call myself a Pirate!"
--STRINGS.CHARACTERS.WILLOW.DESCRIBE.PIRATEPACK        = ""
--STRINGS.CHARACTERS.WOLFGANG.DESCRIBE.PIRATEPACK      = ""
--STRINGS.CHARACTERS.WENDY.DESCRIBE.PIRATEPACK         = ""
--STRINGS.CHARACTERS.WX78.DESCRIBE.PIRATEPACK          = ""
--STRINGS.CHARACTERS.WICKERBOTTOM.DESCRIBE.PIRATEPACK  = ""
--STRINGS.CHARACTERS.WOODIE.DESCRIBE.PIRATEPACK        = ""
--STRINGS.CHARACTERS.WAXWELL.DESCRIBE.PIRATEPACK       = ""
--STRINGS.CHARACTERS.WATHGRITHR.DESCRIBE.PIRATEPACK    = ""
--STRINGS.CHARACTERS.WEBBER.DESCRIBE.PIRATEPACK        = ""

------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------

return  Prefab( "common/inventory/piratepack", fn, assets)

------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
