local assets=
{
	Asset("ANIM", "anim/armormosquito.zip"),
	Asset("ANIM", "anim/armormosquito2.zip"),
	Asset("ANIM", "anim/torso_defitems.zip"),
}
---
local function armorhealing(inst, owner)
	local owner = inst.components.inventoryitem.owner
	if inst.components.inventoryitem == nil or owner.components.inventory == nil then
        return
    end
	if owner and owner.components.hunger ~= 0 and owner.components.health ~= 0 and owner.components.health:IsHurt() and inst.components.armor:GetPercent() ~= 0 and inst.heal_time == 1 then
		owner.components.hunger.current = owner.components.hunger.current-1
		owner.components.health.currenthealth = owner.components.health.currenthealth+2
		owner.components.health:ForceUpdateHUD()
		inst.components.armor:TakeDamage(2)
		---
		owner.player_classified.issleephealing:set(true)
	end
end

---
local function OnBlocked(owner)
    owner.SoundEmitter:PlaySound("dontstarve/wilson/hit_armour")
end

local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_body", "armormosquito", "swap_body")
	---
	inst.heal_time = 1
	if inst.heal_time == 1 then
	inst.task = inst:DoPeriodicTask(2, function() armorhealing(inst, 2) end)
	end
	---
	owner.player_classified.issleephealing:set(true)
	owner:AddTag("mosquitosuited")
	---
	inst:ListenForEvent("blocked", OnBlocked, owner)
end

local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")
	---
	inst.heal_time = 0
	if inst.task ~= nil then
        inst.task:Cancel()
        inst.task = nil
    end
	---
	owner.player_classified.issleephealing:set(false)
	owner:RemoveTag("mosquitosuited")
	---
	inst:RemoveEventCallback("blocked", OnBlocked, owner)
end

local function mosquitoarmor()
	local inst = CreateEntity()

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

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("torso_hawaiian")
    inst.AnimState:SetBuild("armormosquito2")
    inst.AnimState:PlayAnimation("anim")

	inst.foleysound = "dontstarve/movement/foley/logarmour"

	if TheSim:GetGameID()=="DST" then
		inst.entity:AddNetwork()

		if not TheWorld.ismastersim then
			return inst
		end

		inst.entity:SetPristine()

	    MakeHauntableLaunch(inst)
	end

	inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "images/inventoryimages/armormosquito.xml"
	inst.components.inventoryitem.imagename = "armormosquito"

    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.BODY
	inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

	inst:AddComponent("armor")
    inst.components.armor:InitCondition(1050, 0.7)

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

    return inst
end


return Prefab( "common/inventory/armormosquito", mosquitoarmor, assets)
