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

local MAXWORK = 3
local MAXWORK_1 = 1/MAXWORK

--��������� ������� ������� finiteuses � ��������� ������������� �� ���������� workable
local function UpdateWorkValue(inst)
	local perc = inst.components.finiteuses:GetPercent()
	inst.components.workable:SetWorkLeft(perc*MAXWORK)
end

--������ �������� ����. �� ������ workable ��������� finiteuses
local function UpdateFiniteValue(inst)
	local perc = inst.components.workable.workleft * MAXWORK_1
	inst.components.finiteuses:SetPercent(perc)
end

--����������� ������ 5 ���, ����� ���� ���. �������� �����.
local function TaskPerish(inst)
	if not inst:IsValid() then
		inst.task_perish:Cancel()
		inst.task_perish = nil
		return
	end
	if TheWorld.state.temperature > 0  --Not in winter!
	then
		inst.components.finiteuses:Use(0.5)
		UpdateWorkValue(inst)
	end
	--[[if math.random() < TUNING.MAD_ICEBLADE.chance_drop then

	end--]]
end

local function onequip(inst, owner)
	owner.AnimState:OverrideSymbol("swap_object", "swap_iceblade", "swap_nightmaresword")
	owner.AnimState:Show("ARM_carry")
	owner.AnimState:Hide("ARM_normal")

	if not inst.task_perish then
		inst.task_perish = inst:DoPeriodicTask(5,TaskPerish)
	end
end

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

	if inst.task_perish then
		inst.task_perish:Cancel()
		inst.task_perish = nil
	end
end

local function onattack(inst, attacker, target)
	if attacker and attacker.components.sanity then
		attacker.components.sanity:DoDelta(-4) --� ��� ��� ����� ����. ���� ������, ��� � DS �� ���� ������������.
	end
	if target.components.freezable then
		if target:HasTag("player") then
			target.components.freezable:AddColdness(TUNING.MAD_ICEBLADE.player_coldness)
			--if target.components.temperature then
			target.components.temperature:DoDelta(TUNING.MAD_ICEBLADE.temp_delta)
		else
			target.components.freezable:AddColdness(TUNING.MAD_ICEBLADE.npc_coldness) --3 ��� ������� �����.
		end
		--jj: ���� �� ������� ����� ��-������� ��������������.
	end
	if target.components.sleeper and target.components.sleeper:IsAsleep() then
		target.components.sleeper:WakeUp()
	end
	if target.components.burnable and target.components.burnable:IsBurning() then
		target.components.burnable:Extinguish() --���� �����! ���� �����!
	end
	if target.components.combat then
		target.components.combat:SuggestTarget(attacker)
		if target.sg and not target.sg:HasStateTag("frozen") and target.sg.sg.states.hit then
			target.sg:GoToState("hit")
		end
	end
	UpdateWorkValue(inst)
end

local function onhit(inst)
	inst.components.lootdropper:SpawnLootPrefab("ice")
	UpdateFiniteValue(inst)
end

local function onhammered(inst)
	inst.components.lootdropper:SpawnLootPrefab("bluegem")
	inst:Remove()
end

local function on_repaired(inst,doer,with_item)
	UpdateFiniteValue(inst)
end

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)

	inst.AnimState:SetBank("nightmaresword")
	inst.AnimState:SetBuild("iceblade")
	inst.AnimState:PlayAnimation("idle")
	--inst.AnimState:SetMultColour(1, 1, 1, 1)

	inst:AddTag("sharp")
	inst:AddTag("shadow")
	inst:AddTag("HASHEATER") --add to pristine

	inst.entity:SetPristine()
	if not TheWorld.ismastersim then
		return inst
	end

	inst:AddComponent("weapon")
	inst.components.weapon:SetDamage(TUNING.MAD_ICEBLADE.damage)
	inst.components.weapon:SetOnAttack(onattack)

	inst:AddComponent("heater")
	--inst.components.heater.iscooler = true
	inst.components.heater:SetThermics(false,true)
	inst.components.heater.equippedheat = TUNING.MAD_ICEBLADE.equippedheat

	inst:AddComponent("finiteuses")
	inst.components.finiteuses:SetMaxUses(TUNING.MAD_ICEBLADE.uses)
	inst.components.finiteuses:SetUses(TUNING.MAD_ICEBLADE.uses)
	inst.components.finiteuses:SetOnFinished( inst.Remove )

	inst:AddComponent("inspectable")

	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "inventoryimages/iceblade.xml"

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip( onequip )
	inst.components.equippable:SetOnUnequip( onunequip )
	inst.components.equippable.dapperness = TUNING.MAD_ICEBLADE.dapperness

	inst:AddComponent("tool")
	inst.components.tool:SetAction(ACTIONS.CHOP)


	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.MINE)
	inst.components.workable:SetWorkLeft(MAXWORK)
	inst.components.workable:SetMaxWork(MAXWORK)
	inst.components.workable:SetOnFinishCallback(onhammered)
	inst.components.workable:SetOnWorkCallback(onhit)

	inst:AddComponent("repairable")
	inst.components.repairable.repairmaterial = MATERIALS.ICE
	inst.components.repairable.announcecanfix = false --��������? �, �����. ����� �����. :)
	inst.components.repairable.onrepaired = on_repaired

	inst:AddComponent("lootdropper")

	MakeHauntableLaunch(inst)

	return inst
end

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