----------------------------------------------------------------------------------------------------
-- Disable actor's weapon
----------------------------------------------------------------------------------------------------
--function printf()
--end

local state_nowhere = 0 -- схема только что включилась
local state_inside = 1 -- актер внутри
local state_outside = 2 -- актер снаружи

class "action_no_weapon"

function action_no_weapon:__init( obj, storage )
	self.object = obj
	self.st = storage
	self.inited_time = game.CTime()
end

function action_no_weapon:reset_scheme()
	--printf("_bp: sr_idle: action_no_weapon:reset_scheme: self.object:name()='%s'", self.object:name())

	self.state = state_nowhere -- еще неясно, в зоне он, или нет

	self:switch_state(db.actor)

	db.no_weap_zones[self.object:name()] = false
end

function action_no_weapon:update( delta )
	--printf("_br: sr_idle: action_no_weapon:update()")

	--if not xr_logic.is_active( self.object, self.st ) then
	--	return
	--end

	local actor = db.actor

	if xr_logic.try_switch_to_another_section( self.object, self.st, actor ) then
		if self.state == state_inside then
			self:zone_leave()
		end
		return
	end	

	self:switch_state( actor )

	local hud = get_hud()
	local custom_static = hud:GetCustomStatic("can_use_weapon_now")
	if custom_static ~= nil and game.get_game_time():diffSec(self.inited_time) > 30 then
		hud:RemoveCustomStatic("can_use_weapon_now")
	end
end

function action_no_weapon:switch_state( actor )
	local state = self.state
	--printf("on_update state[%s]", tostring(self.state))

	if (state == state_outside or state == state_nowhere) then
		if ars_cop.check_unhided(alife():object(actor:id()))==false then
			self:zone_enter()
			--printf("on_update1 state[%s]", tostring(self.state))
			return
		end
	end

	if (state == state_inside or state == state_nowhere) then
		if ars_cop.check_unhided(alife():object(actor:id()))==true then
			self:zone_leave()
			--printf("on_update2 state[%s]", tostring(self.state))
			return
		end
		local weap_obj = db.actor:active_item()
		if weap_obj ~= nil and isWeapon (weap_obj) ~= false then
			printf("no weapon zone [%s] actor is inside, but with weapon active", tostring(self.object:name()))
		end
	end
end

function action_no_weapon:zone_enter()
	self.state = state_inside
	bind_stalker.hide_weapon(self.object:id())
	local hud = get_hud()
	local custom_static = hud:GetCustomStatic("can_use_weapon_now")
	if custom_static ~= nil then
		hud:RemoveCustomStatic("can_use_weapon_now")
	end
	printf("entering no weapon zone [%s]", tostring(self.object:name()))
end

function action_no_weapon:zone_leave()
	self.state = state_outside
	bind_stalker.restore_weapon(self.object:id())
	local hud = get_hud()
	local custom_static = hud:GetCustomStatic("can_use_weapon_now")
	if db.no_weap_zones[self.object:name()] == true then
		db.no_weap_zones[self.object:name()] = false
--	elseif custom_static == nil then
--		hud:AddCustomStatic("can_use_weapon_now", true)
--		hud:GetCustomStatic("can_use_weapon_now"):wnd():TextControl():SetTextST("st_can_use_weapon_now")
	end
	printf("exiting no weapon zone [%s]", tostring(self.object:name()))
	self.inited_time = game.get_game_time()
end

---------------------------------------------------------------------------------------------------------------------
function add_to_binder(npc, ini, scheme, section, storage)
	printf("DEBUG: add_to_binder: scheme='%s', section='%s'", scheme, section)

	local new_action = action_no_weapon(npc, storage)

	-- Зарегистрировать все actions, в которых должен быть вызван метод reset_scheme при изменении настроек схемы:
	xr_logic.subscribe_action_for_events(npc, storage, new_action)
end

function set_scheme(npc, ini, scheme, section, gulag_name)
	local st = xr_logic.assign_storage_and_bind(npc, ini, scheme, section)
	st.logic = xr_logic.cfg_get_switch_conditions(ini, section, npc)
end
