local assets =
{
	Asset("ANIM", "anim/gears_staff.zip"),
	Asset("ANIM", "anim/swap_gears_staff.zip"),
}

local function generic_perish(inst)
	local fin = SpawnPrefab("ruins_battery")
	fin.Transform:SetPosition(inst.Transform:GetWorldPosition())	
    inst:Remove()
end

local function onequip(inst, owner) 
    inst.components.burnable:Ignite()
    owner.AnimState:OverrideSymbol("swap_object", "swap_gears_staff", "swap_nightmaresword")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
	inst.Light:Enable(true)

	inst:DoTaskInTime(0, function()
		if inst.components.fueled.currentfuel < inst.components.fueled.maxfuel then
		    inst.components.fueled:DoDelta(-inst.components.fueled.maxfuel*.01)
	    end
	end)
end

local function onunequip(inst, owner) 	
    inst.components.burnable:Extinguish()
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal") 
	inst.Light:Enable(false)
end

local function onattack(inst, owner, target)
    if owner.components.health and not target:HasTag("shadow") then
		owner.components.sanity:DoDelta(2.5,false,"gears_staff")
		inst.components.weapon:SetDamage(TUNING.SPEAR_DAMAGE)
	else
	    owner.components.sanity:DoDelta(5,false,"gears_staff")
		inst.components.weapon:SetDamage(TUNING.SPEAR_DAMAGE*2)
		SpawnPrefab("statue_transition").Transform:SetPosition(target:GetPosition():Get())
	end
end

local function ontakefuel(inst)
    inst.SoundEmitter:PlaySound("dontstarve/common/nightmareAddFuel")
end

local function fn()
	local inst = CreateEntity()
	
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)
	
    inst.AnimState:SetBank("nightmaresword")
	inst.AnimState:SetBuild("gears_staff")
    inst.AnimState:PlayAnimation("idle")

	inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
	
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(TUNING.SPEAR_DAMAGE)
	inst.components.weapon.onattack = onattack
	
    -------
	
    inst:AddComponent("tradable")
	inst:AddComponent("inspectable")
	
	inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/gears_staff.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.components.equippable.dapperness = TUNING.CRAZINESS_SMALL
	
	inst:AddComponent("burnable")
    inst.components.burnable.canlight = false
    inst.components.burnable.fxprefab = nil
	
    inst:AddComponent("fueled")
	inst.components.fueled:SetSectionCallback(
        function(section)
            if section == 0 then
                --when we burn out
                if inst.components.burnable then 
					inst.components.burnable:Extinguish() 
				end
				
                if inst.components.inventoryitem and inst.components.inventoryitem:IsHeld() then
                    local owner = inst.components.inventoryitem.owner
                    inst:Remove()
                end           
            end
        end)
    inst.components.fueled:InitializeFuelLevel(TUNING.TOTAL_DAY_TIME*.75)
	inst.components.fueled.accepting = true
    inst.components.fueled.fueltype = FUELTYPE.NIGHTMARE
--	inst.components.fueled.ontakefuelfn = ontakefuel
    inst.components.fueled:SetDepletedFn(generic_perish)
	
	inst.components.inventoryitem:SetOnDroppedFn(function(inst) inst.Light:Enable(false) end)
	
    inst.entity:AddLight()
    inst.Light:Enable(false)
	inst.Light:SetRadius(1.5)
	inst.Light:SetFalloff(0.4)
	inst.Light:SetIntensity(0.75)
    inst.Light:SetColour(255/255, 255/255, 155/255)
	
    MakeHauntableLaunch(inst)
	
    return inst
end

STRINGS.NAMES.GEARS_STAFF 								= "Vacuum Bulb"
STRINGS.RECIPE_DESC.GEARS_STAFF 						= "Absorbs nightmares."

STRINGS.CHARACTERS.GENERIC.DESCRIBE.GEARS_STAFF 		= "It runs on sanity!"
STRINGS.CHARACTERS.WILLOW.DESCRIBE.GEARS_STAFF 			= "It absorbs the light around it."
STRINGS.CHARACTERS.WOLFGANG.DESCRIBE.GEARS_STAFF 		= "Silly stick, light bulb is weak and fragile."
STRINGS.CHARACTERS.WENDY.DESCRIBE.GEARS_STAFF 			= "It feeds off my mind."
STRINGS.CHARACTERS.WX78.DESCRIBE.GEARS_STAFF 			= "PROGRAMMING IS COMPROMISED"
STRINGS.CHARACTERS.WICKERBOTTOM.DESCRIBE.GEARS_STAFF 	= "Wielding it drives its owner into insanity."
STRINGS.CHARACTERS.WOODIE.DESCRIBE.GEARS_STAFF			= "Now that's neat."
STRINGS.CHARACTERS.WAXWELL.DESCRIBE.GEARS_STAFF 		= "I never knew how to replicate its effects."
STRINGS.CHARACTERS.WATHGRITHR.DESCRIBE.GEARS_STAFF 		= "It can pierce through the darkness."
STRINGS.CHARACTERS.WEBBER.DESCRIBE.GEARS_STAFF 			= "It's like my night light!"

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