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


local function onequiphat(inst, owner)
    owner.AnimState:OverrideSymbol("swap_hat", "venus_flowerglass", "swap_glass")
		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
		if owner.components.farseer then
		owner.components.farseer:AddBonus("venus_flowerglass", 5)
		end
		
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

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

		if owner.components.farseer then
			owner.components.farseer:RemoveBonus("venus_flowerglass")
		end
end

local function opentop_onequip(inst, owner)
        owner.AnimState:OverrideSymbol("swap_hat", "venus_flowerglass", "swap_glass")
		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  
		if owner.components.farseer then
		owner.components.farseer:AddBonus("venus_flowerglass", 5)
		end
end

local function finished(inst)
        inst:Remove()		
end
  local function baozang(new_sec, old_sec, inst, isload)
		if isload then
			return
		end

		local equipper = inst and inst.components.equippable and inst.components.equippable.equipper

		if equipper and not equipper:HasTag("player") and math.random() > 0.66 then
			return
		end

		local pos = inst:GetPosition()
		local offset = FindGroundOffset(pos, math.random() * 2 * math.pi, math.random(25, 30), 18)

		if offset then
			local spawn_pos = pos + offset
		    local tile = GetVisualTileType(spawn_pos:Get())
    		local is_water = GetMap():IsWater(tile)
    		local treasure = SpawnPrefab("buriedtreasure")

    		treasure.Transform:SetPosition(spawn_pos:Get())
    		treasure:SetRandomTreasure()

    		if equipper then
    			inst.components.equippable.equipper:PushEvent("treasureuncover")
    		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)

    anim:SetBank("venus_flowerglass")
    anim:SetBuild("venus_flowerglass")
    anim:PlayAnimation("anim")
    
	inst:AddTag("hat")
	inst:AddTag("hide")
    inst:AddTag("show_spoilage")
	inst:AddComponent("inspectable")

    inst:AddComponent("tradable")
	
    inst:AddComponent("inspectable")
		
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "images/inventoryimages/venus_flowerglass.xml"
    
	inst:AddComponent("fueled")
    inst.components.fueled.fueltype = "USAGE"
    inst.components.fueled:InitializeFuelLevel(TUNING.WOODLEGSHAT_PERISHTIME)  --10days
    inst.components.fueled:SetDepletedFn(finished)
	inst.components.fueled:SetSections(TUNING.WOODLEGSHAT_TREASURES) --6
	inst.components.fueled:SetSectionCallback(baozang)

	inst:AddComponent("equippable")
	inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
	inst.components.equippable:SetOnEquip( onequiphat )
    inst.components.equippable:SetOnUnequip( onunequiphat )
    inst.components.equippable:SetOnEquip(opentop_onequip)
    
	inst:AddComponent("characterspecific")
    inst.components.characterspecific:SetOwner("venus")
    return inst
end 
    
return Prefab( "common/inventory/venus_flowerglass", fn, assets) 