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

local prefabs =
{
     "butterfly",
}
local function onequip(inst, owner)
		owner.AnimState:OverrideSymbol("swap_hat", "venus_flowerhat", "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
		
		if inst.components.fueled then
			inst.components.fueled:StartConsuming()        
		end

end
local function onunequip(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

		if inst.components.fueled then
			inst.components.fueled:StopConsuming()        
		end
end

local function opentop_onequip(inst, owner)
		owner.AnimState:OverrideSymbol("swap_hat", "venus_flowerhat", "swap_hat")
		owner.AnimState:Show("HAT")
		owner.AnimState:Hide("HAT_HAIR")
		owner.AnimState:Show("HAIR_NOHAT")
		owner.AnimState:Show("HAIR")
		
		owner.AnimState:Show("HEAD")
		owner.AnimState:Hide("HEAD_HAIR")

		if inst.components.fueled then
			inst.components.fueled:StartConsuming()        
		end
end
local function finished(inst)
        inst:Remove()		
end

local function spawnbutterfly(inst,owner)
	local owner = inst.components.inventoryitem.owner 
	if owner ~= nil  and inst.components.equippable:IsEquipped() then
		if TheWorld.state.isday or TheWorld.state.iscaveday then  
        local x,y,z = owner.Transform:GetWorldPosition()
		local fly = SpawnPrefab("butterfly") 
			if fly then
            fly.Transform:SetPosition(x,y,z)
			end
		end
	end
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)
    --inst.entity:AddNetwork()
    anim:SetBank("venus_flowerhat")
    anim:SetBuild("venus_flowerhat")
    anim:PlayAnimation("anim")
    
	inst:AddTag("hat")
	inst:AddTag("hide")
    inst:AddTag("show_spoilage")
	inst:AddComponent("inspectable")

    inst:AddTag("frozen")
    inst:AddTag("icebox_valid")
    inst:AddTag("HASHEATER")
    inst:AddTag("waterproofer")
    inst:AddComponent("tradable")
	
    inst:AddComponent("inspectable")
		
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "images/inventoryimages/venus_flowerhat.xml"
	inst.components.inventoryitem.foleysound = "dontstarve/movement/foley/bushhat"
       
	inst:AddComponent("equippable")
	inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
	inst.components.equippable:SetOnEquip( onequip )
	inst.components.equippable:SetOnUnequip( onunequip )
    inst.components.equippable.dapperness =  5/60 --TUNING.DAPPERNESS_LARGE 
	inst.components.equippable:SetOnEquip( opentop_onequip )
    
	inst:AddComponent("insulator")
    inst.components.insulator:SetInsulation(60)
    inst.components.insulator:SetSummer()
	
	inst:AddComponent("perishable")
    inst.components.perishable:SetPerishTime(TUNING.PERISH_SUPERFAST)  
    inst.components.perishable:StartPerishing()                   
	inst.components.perishable:SetOnPerishFn(finished)      
    
	inst:AddComponent("repairable")
	inst.components.repairable.repairmaterial = "petals"
	--inst.components.repairable.announcecanfix = true


    if IsDLCEnabled(CAPY_DLC) then
	inst.components.equippable.walkspeedmult = 0.1
	else
	inst.components.equippable.walkspeedmult = 1.1
	end
	
    return inst
end 
    
return Prefab( "common/inventory/venus_flowerhat", fn, assets, prefabs) 