local assets=
{
    Asset("ANIM", "anim/eel_torch.zip"),
    Asset("ANIM", "anim/swap_eel_torch.zip"),
	Asset("SOUND", "sound/common.fsb"),

    Asset("ATLAS", "inventoryimages/eel_torch.xml"),
    Asset("IMAGE", "inventoryimages/eel_torch.tex"),
}
local function onfinished(inst)
    inst:Remove()
end
local prefabs =
{
	"nightstickfire",
}

local function onequip(inst, owner)
    inst.components.burnable:Ignite()
    owner.AnimState:OverrideSymbol("swap_object", "swap_eel_torch", "eel_torch") --3?
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")

    inst.SoundEmitter:PlaySound("dontstarve_DLC001/common/morningstar", "torch")
--    inst.SoundEmitter:SetParameter( "torch", "intensity", 1 )

    inst.fire = SpawnPrefab( "nightstickfire" )
    local follower = inst.fire.entity:AddFollower()
    follower:FollowSymbol( owner.GUID, "swap_object", 0, -110, 1 )

end
local function onunequip(inst,owner)
	inst.fire:Remove()
    inst.fire = nil

    inst.components.burnable:Extinguish()
    --owner.components.combat.damage = owner.components.combat.defaultdamage
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    inst.SoundEmitter:KillSound("torch")
end

local function item_oneaten(inst, eater)

    if eater.wormlight then
        eater.wormlight.components.spell.lifetime = 0
        eater.wormlight.components.spell:ResumeSpell()
    else
        local light = SpawnPrefab("wormlight_light")
        light.components.spell:SetTarget(eater)
        if not light.components.spell.target then
            light:Remove()
        end
        light.components.spell:StartSpell()
    end
end
local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    local sound = inst.entity:AddSoundEmitter()
    anim:SetBank("eel_torch")
    anim:SetBuild("eel_torch")
    anim:PlayAnimation("idle")
    MakeInventoryPhysics(inst)
	inst:AddTag("irreplaceable")
	inst.entity:AddNetwork() --+++

	if not TheWorld.ismastersim then
		return inst
	end

	-----
	--local light = inst.entity:AddLight()
	inst.entity:AddLight()
    inst.Light:Enable(true)
    inst.Light:SetIntensity(.75)
    inst.Light:SetColour(252/255,251/255,237/255)
    inst.Light:SetFalloff( 0.6 )
    inst.Light:SetRadius( 3 )
    -----
    inst:AddComponent("weapon")
	inst.components.weapon:SetDamage(46)
	--[[
	inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(TUNING.NIGHTSTICK_DAMAGE*1.6)
    inst.components.weapon:SetOnAttack(function(inst)
        if inst and inst:IsValid() and inst.fire and inst.fire:IsValid() then
            inst.fire:OnAttack()
        end
    end)
    --inst.components.weapon:SetElectric()
	]]--

	inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(100)
    inst.components.finiteuses:SetUses(100)
	inst.components.finiteuses:SetOnFinished( inst.Remove )
    --inst:AddComponent("fueled")
	----------------------


	inst:AddComponent("edible")
	inst.components.edible.healthvalue = TUNING.HEALING_TINY
	inst.components.edible.hungervalue = TUNING.CALORIES_SMALL
	inst.components.edible.sanityvalue = 0
	inst.components.edible.foodtype = "MEAT"
	inst.components.edible:SetOnEatenFn(item_oneaten)

	inst:AddTag("show_spoilage")
	--inst:AddTag("frozen")

    inst:AddComponent("perishable")
    inst.components.perishable:SetPerishTime(TUNING.PERISH_FAST) -- Days: SUPERFAST=3, FAST=6, MED=10, SLOW=15, PRESERVED=20, SUPERSLOW=40
    inst.components.perishable:StartPerishing()
    inst.components.perishable.onperishreplacement = "spoiled_food"
    -- -----------------------------------
    inst:AddComponent("lighter")
    -- -----------------------------------

    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "inventoryimages/eel_torch.xml"
    -----------------------------------

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnPocket( function(owner) inst.components.burnable:Extinguish()  end)
    inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip )
    -----------------------------------

    inst:AddComponent("inspectable")

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

    inst:AddComponent("burnable")
    inst.components.burnable.canlight = false
    inst.components.burnable.fxprefab = nil

    return inst
end

return Prefab( "common/inventory/eel_torch", fn, assets, prefabs)
