
local function onequip(inst, owner)
	--if owner.userid == TUNING.SUOFEIYA_OKID then
	if true then
		owner.AnimState:OverrideSymbol("swap_object", "swap_huanxiang", "swap_huanxiang")
		owner.AnimState:Show("ARM_carry")
		owner.AnimState:Hide("ARM_normal")
	else
		owner:DoTaskInTime(0, function()
			local inv = owner.components.inventory 
			if inv then
				inv:GiveItem(inst)
			end
			local talker = owner.components.talker 
			if talker then
				talker:Say("我不是指定的用户ID!")
			end
		end)
	end
end

local function onunequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
	--inst.Transform:SetScale(1.25,1.25,1.25)
	
    inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("huanxiang")
    inst.AnimState:SetBuild("huanxiang")
    inst.AnimState:PlayAnimation("idle")

    --inst:AddTag("sharp")
	--inst:AddTag("shadow")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
	
	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "images/huanxiang.xml"
	inst.components.inventoryitem.imagename = "huanxiang"

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(40)
	inst.components.weapon:SetOnAttack(function (weapon, attacker, target)
		if attacker then
			if attacker.components.health then
				attacker.components.health:DoDelta(2)
			end
			if attacker.components.sanity then
				attacker.components.sanity:DoDelta(2)
			end
		end
		if target then
			if target.components.freezable then
				if target.mengjiang_task == nil then
					target.mengjiang_task = target:DoPeriodicTask(1, function()
						if target.components.health and not target.components.health:IsDead() then
							target.components.freezable:AddColdness(6)
							target.components.freezable:SpawnShatterFX()
						end
					end)
				end
				target:DoTaskInTime(100, function()
					if target.mengjiang_task ~= nil then
						target.mengjiang_task:Cancel()
						target.mengjiang_task = nil
					end
				end)
			end
		end
	end)
	
	inst:AddComponent("tool")
	inst.components.tool:SetAction(ACTIONS.CHOP, 1)
	inst.components.tool:SetAction(ACTIONS.MINE, 1) 
	inst.components.tool:SetAction(ACTIONS.HAMMER)

    inst:AddComponent("inspectable")
	
	inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(100)
    inst.components.finiteuses:SetUses(100)
    inst.components.finiteuses:SetOnFinished(inst.Remove)
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
	
	--[[
	inst:DoPeriodicTask(.25, function()
		local owner = inst.components.inventoryitem.owner
		if owner and owner.userid == TUNING.SUOFEIYA_OKID then
			inst.components.finiteuses:SetMaxUses(999999)
			inst.components.finiteuses:SetUses(999999)
		end
	end)]]

    return inst
end

return Prefab("huanxiang", fn)