local assets =
{
    Asset("anim", "anim/clbl.zip"),
    Asset("anim", "anim/swap_clbl.zip"),
    Asset("ATLAS", "images/inventoryimages/clbl.xml"),
    Asset("IMAGE", "images/inventoryimages/clbl.tex"),
	
	Asset("SOUNDPACKAGE", "sound/clbl.fev"),
    Asset("SOUND", "sound/clbl.fsb"),  
}

local function onfinished(inst)
    inst:Remove()
end

local function shine(inst)
    inst.task = nil
    inst.AnimState:PlayAnimation("sparkle")
    inst.AnimState:PushAnimation("idle")
    inst.task = inst:DoTaskInTime(4 + math.random() * 5, shine)
end

local function onequip(inst, owner) 
    owner.AnimState:OverrideSymbol("swap_object", -- Symbol to override.
        "swap_clbl", -- Animation bank we will use to overwrite the symbol.
        "swap_clbl") -- Symbol to overwrite it with.
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
end

local function onunequip(inst, owner) 
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal") 
end

local function CanCast(doer, target, pos)
    return true
end

function  UseStaff(inst, Target, pos)
	if Target == nil then
		inst.SoundEmitter:PlaySound("clbl/clbl/ring")
		local cloud = SpawnPrefab("poopcloud")
		cloud.Transform:SetPosition(pos:Get())
		local hooh = SpawnPrefab("hooh")
		hooh.Transform:SetPosition(pos:Get())
		inst.components.finiteuses:Use(1)
		return true
	end
end

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

local function fn()
    local inst = CreateEntity()

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

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("bell01")
    inst.AnimState:SetBuild("clbl")
    inst.AnimState:PlayAnimation("idle")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(10)
    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "clbl"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/clbl.xml"
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
    inst:AddTag("nopunch")

    inst:AddComponent("spellcaster")
    inst.components.spellcaster:SetSpellFn(UseStaff)
    inst.components.spellcaster.canuseontargets = true
    --inst.components.spellcaster.canonlyuseonlocomotorspvp = true    
    inst.components.spellcaster.canusefrominventory = false
    inst.components.spellcaster.canuseonpoint = true
    inst.components.spellcaster.CanCast = CanCast
   
    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(1)
    inst.components.finiteuses:SetUses(1)
    inst.components.finiteuses:SetOnFinished( onfinished )

    shine(inst)
    return inst
end

return Prefab("clbl", fn, assets)