local assets=
{
	Asset("ANIM", "anim/warfarincutlass.zip"),
	Asset("ANIM", "anim/warfarincutlasspoof.zip"),
	Asset("ANIM", "anim/swap_warfarincutlass.zip"),
}
    
local prefabs =
{
}

local function Gasem(v)

        if v and v.components.health and not v.components.health:IsDead() 
        and v ~= GetPlayer() and v.components.combat and v.components.poisonable then
        	v.components.combat:GetAttacked(GetPlayer(), 2.4)
		v.components.poisonable:Poison()
	end
end

-- Asplode upon the Stabbins

local function Mark(inst, attacker, target)

    SpawnPrefab("warfarincutlasspoof").Transform:SetPosition(target:GetPosition():Get())
    local pt = attacker:GetPosition()
    local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, 3)
    local ents2 = TheSim:FindEntities(pt.x, pt.y, pt.z, 6)
    local ents3 = TheSim:FindEntities(pt.x, pt.y, pt.z, 8)
    local ents4 = TheSim:FindEntities(pt.x, pt.y, pt.z, 9)
    for k,v in pairs(ents) do
        	Gasem(v)
    end
attacker:DoTaskInTime(0.1, function()
    for k,v in pairs(ents2) do
        	        	Gasem(v)
    end
	end)
attacker:DoTaskInTime(0.2, function()
    for k,v in pairs(ents3) do
        	        	Gasem(v)
    end
	end)
attacker:DoTaskInTime(0.3, function()
    for k,v in pairs(ents4) do
        	        	Gasem(v)
    end
	end)
	inst.SoundEmitter:PlaySound("dontstarve_DLC002/creatures/Stinkray/attack")
    local cloud = SpawnPrefab("firering_fx").Transform:SetPosition(inst:GetPosition():Get())
    if cloud then
    cloud.AnimState:SetMultColour(0.4,0.8,0.9,1)
    end
end

local function OnEquip(inst, owner) 
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal")
    owner.AnimState:OverrideSymbol("swap_object", "swap_warfarincutlass", "swap_warfarincutlass")
        if not inst.miasma then 
            inst.miasma = SpawnPrefab( "poisonbubble_level1_loop" )
            local follower = inst.miasma.entity:AddFollower()
            follower:FollowSymbol( owner.GUID, "swap_object", 0, -40, 1 )
	inst.miasma.Transform:SetScale(0.6, 0.6, 0.6)
		inst.miasma.persists = false
        end 
        inst.task = inst:DoPeriodicTask(10, function() 
owner.components.health:DoPoisonDamage(1)
owner:PushEvent("poisondamage", {1})
owner.SoundEmitter:PlaySound("dontstarve_DLC002/common/HUD_Poisoned")
    local bubble = SpawnPrefab("poisonbubble_level1")
    if bubble then
	    local follower = bubble.entity:AddFollower()
	    follower:FollowSymbol(owner.GUID, owner.components.combat.hiteffectsymbol, 0, 0, 0 )
    end
		end)
end

local function OnUnequip(inst, owner) 
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal")
    if inst.miasma then 
        inst.miasma:Remove()
        inst.miasma = nil
    end 
   if inst.task then
        inst.task:Cancel()
        inst.task = nil
    end
end

local function fn(Sim)
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
    MakeInventoryPhysics(inst)
    
    inst:AddTag("warwep")

    MakeInventoryPhysics(inst)

		MakeInventoryFloatable(inst, "idle_water", "idle")

	--inst.Transform:SetScale(1.2, 1.2, 1.2)
    inst.AnimState:SetBank("warfarincutlass")
    inst.AnimState:SetBuild("warfarincutlass")
    inst.AnimState:PlayAnimation("idle")
  
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(OnEquip)
    inst.components.equippable:SetOnUnequip(OnUnequip)

    ---------------------       
        inst:AddTag("nopunch")
    inst:AddTag("sharp")

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(36)
    inst.components.weapon:SetOnAttack(Mark)

	inst:AddComponent("finiteuses")
	inst.components.finiteuses:SetMaxUses(TUNING.SPEAR_USES)
	inst.components.finiteuses:SetUses(TUNING.SPEAR_USES)

    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/warfarincutlass.xml"
    
    return inst
end

local function puff()
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	local sound = inst.entity:AddSoundEmitter()
	local light = inst.entity:AddLight()

    inst.AnimState:SetBank("poison_hole")
    inst.AnimState:SetBuild("warfarincutlasspoof")
    inst.AnimState:PlayAnimation("pop")
	inst.Transform:SetScale(1, 1, 1)

	inst.persists = false
		
	inst.persists = false

    inst:AddTag("FX")
    inst:AddTag("NOCLICK")

    anim:SetFinalOffset(1)

        inst:ListenForEvent("animover", function() inst:Remove() end)

	return inst
end
return Prefab( "common/inventory/warfarincutlass", fn, assets),
	Prefab( "common/inventory/warfarincutlasspoof", puff, assets)

