local assets=
{
	Asset("ANIM", "anim/inorihat.zip"),
	Asset("IMAGE", "images/inventoryimages/inorihat.tex"),
	Asset("ATLAS", "images/inventoryimages/inorihat.xml"),
}

local function onusehat(inst)
    local owner = inst.components.inventoryitem.owner
    if owner then
        owner.sg:GoToState("hide")
    end
end

local function stopusinghat(inst, data)
        local hat = inst.components.inventory and inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
        if hat and not (data.statename == "hide_idle" or data.statename == "hide") then
            hat.components.useableitem:StopUsingItem()
        end
end

local function ShouldAcceptItem(inst, item)
	if GetPlayer().components.inventory:Has("goldnugget", 5) then
		if item.prefab == "goldnugget" then
			return true
		end
	end
		return false
end

local function OnGetItemFromPlayer(inst, giver, item)
	if item.prefab == "goldnugget" then
    giver.components.inventory:ConsumeByName("goldnugget", 4)
	end
	local player = GetPlayer()
	player.components.talker:Say("Everthing in my hand ! ! ! !", 4, false) 
    SpawnPrefab("book_fx").Transform:SetPosition(player.Transform:GetWorldPosition())
	local pos = Vector3(inst.Transform:GetWorldPosition())
	local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 15)
		for k,v in pairs(ents) do
			if v.components.pickable and v.prefab ~= "flower" then
				v.components.pickable:Pick(GetPlayer())
			end
			if v.components.crop then
				v.components.crop:Harvest(GetPlayer())
			end
		end
end

local function onequiphat(inst, owner)
    owner.AnimState:OverrideSymbol("swap_hat", "inorihat", "swap_hat")
    owner.AnimState:Show("HAT")
    owner.AnimState:Show("HAT_HAIR")
    owner.AnimState:Hide("HAIR_NOHAT")
    owner.AnimState:Hide("HAIR")
        
    if owner:HasTag("player") then
        owner.AnimState:Hide("HEAD")
        owner.AnimState:Show("HEAD_HAIR")
    end
	
	inst.damagefn = function(attacked, data)
	SpawnPrefab("collapse_big").Transform:SetPosition(owner.Transform:GetWorldPosition())
	if data and data.attacker and data.attacker.components.health then
		data.attacker.components.health:DoDelta(-77)
	end 
end
	inst:ListenForEvent("attacked", inst.damagefn, owner)
    inst:ListenForEvent("newstate", stopusinghat, owner) 
end

local function onunequiphat(inst, owner)
    owner.AnimState:Hide("HAT")
    owner.AnimState:Hide("HAT_HAIR")
    owner.AnimState:Show("HAIR_NOHAT")
    owner.AnimState:Show("HAIR")
	
    if owner:HasTag("player") then
        owner.AnimState:Show("HEAD")
        owner.AnimState:Hide("HEAD_HAIR")
    end
	
	GetPlayer().components.health.absorb = 0
	inst:RemoveEventCallback("attacked", inst.damagefn, owner)
    inst:RemoveEventCallback("newstate", stopusinghat, owner)
end

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    local sound = inst.entity:AddSoundEmitter()
    MakeInventoryPhysics(inst)
    
    anim:SetBank("bushhat")
    anim:SetBuild("inorihat")
    anim:PlayAnimation("anim")
    
    inst:AddTag("sharp")
	inst:AddTag("hat")
	inst:AddTag("hide")
    
	inst:AddComponent("waterproofer")
    inst.components.waterproofer:SetEffectiveness(TUNING.WATERPROOFNESS_LARGE)
	
	inst:AddComponent("useableitem")
    inst.components.useableitem:SetOnUseFn(onusehat)
	
    inst:AddComponent("inspectable")
		
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "images/inventoryimages/inorihat.xml"
	inst.components.inventoryitem.foleysound = "dontstarve/movement/foley/bushhat"
    
    inst:AddComponent("equippable")
	inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
	inst.components.equippable:SetOnEquip( onequiphat )
    inst.components.equippable:SetOnUnequip( onunequiphat )
	inst.components.equippable.dapperness = TUNING.DAPPERNESS_MED
	
	inst:AddComponent("trader")
    inst.components.trader.onaccept = OnGetItemFromPlayer
    inst.components.trader:SetAcceptTest(ShouldAcceptItem)
	

	
    return inst
end

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