local assets=
{
    Asset("ANIM", "anim/handfan.zip"),
    Asset("ANIM", "anim/swap_handfan.zip"),
    Asset("ATLAS", "images/inventoryimages/handfan.xml"),
}

local prefabs = {}

local function FanUPgrades(inst)
	local max_upgrades = 500
	local rank = math.min(inst.fan_level, max_upgrades)

	if inst.fan_level > 499 then
		--inst.components.talker:Say("Coming soon Beta WhaRang MOD!")
	end

end

local function OnKill(inst, data)

	if data.cause == inst.prefab
	and not data.inst:HasTag("prey")
	and not data.inst:HasTag("veggie")
	and not data.inst:HasTag("insect")
	and not data.inst:HasTag("structure")
	and not data.inst:HasTag("companion") then
	if math.random() < .1 and not data.inst.components.health.nofadeout then
	inst.fan_level = inst.fan_level + 1
	FanUPgrades(inst)	
	end
	end
	
end

local function OnEquip(inst, owner)
        owner.AnimState:OverrideSymbol("swap_object", "swap_handfan", "swap_handfan")
        owner.AnimState:Show("ARM_carry")
        owner.AnimState:Hide("ARM_normal")
		owner.SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
end
 
local function OnUnequip(inst, owner)
        owner.AnimState:Hide("ARM_carry")
        owner.AnimState:Show("ARM_normal")
end

local function onpreload(inst, data)
	if data then
		if data.fan_level then
			inst.fan_level = data.fan_level
			FanUPgrades(inst)
		end
	end

end

local function onsave(inst, data)
	data.fan_level = inst.fan_level
end

local function fn()
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    local sound = inst.entity:AddSoundEmitter()
	
    MakeInventoryPhysics(inst)
    
    local minimap = inst.entity:AddMiniMapEntity()
    minimap:SetIcon("handfan.tex")
	
	inst.fan_level = 0
	FanUPgrades(inst)
	
    anim:SetBank("handfan")
    anim:SetBuild("handfan")
    anim:PlayAnimation("idle")
	
    inst:AddComponent("weapon")
	inst.components.weapon:SetDamage(30)
	inst:AddTag("sharp")
	inst:AddTag("handfan")
 
    inst:AddComponent("inspectable")
	
	inst:AddTag("irreplaceable")
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/handfan.xml"
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquip )
    inst.components.equippable:SetOnUnequip( OnUnequip )
	
    inst:AddComponent("talker")
    --inst.components.talker.ontalk = ontalk

    inst.components.talker.fontsize = 26
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(.9, .4, .4, 1)
    inst.components.talker.offset = Vector3(0,0,0)
    inst.components.talker.symbol = "swap_object"
	
	FanUPgrades(inst)
	inst:ListenForEvent("entity_death", function(wrld, data) OnKill(inst, data) end, GetWorld())
	
	if GetPlayer().prefab == "wharang" then
	
    inst:AddComponent("sentientfan")
    
    inst:ListenForEvent("donetalking", function() inst.SoundEmitter:KillSound("talk") end)
    inst:ListenForEvent("ontalk", function() 
		if inst.components.sentientfan.sound_override then
			inst.SoundEmitter:KillSound("talk")
			inst.SoundEmitter:PlaySound(inst.components.sentientfan.sound_override, "special")
		else
			if not inst.SoundEmitter:PlayingSound("special") then
				inst.SoundEmitter:PlaySound("wharangfan/fan/fan_LP", "talk")
			end
		end
	end)
	end
	
	inst.OnSave = onsave
	inst.OnPreLoad = onpreload
 
    return inst
end

return  Prefab("common/inventory/handfan", fn, assets)