local light_zones = {}

local indoor_levels = {
	l03u_agr_underground = true,
	l03u_agr_underground_e3 = true,
	l03u_agr_underground_hw = true,
	l04u_labx18 = true,
	l08u_brainlab = true,
	l10u_bunker = true,
	l12u_sarcofag = true,
	l12u_control_monolith = true,
	chugai_test = true
}
---------------------------------------------------------------------------------------------------------------------
class "action_light"
function action_light:__init (obj, storage)
	self.object = obj
	self.st = storage
	self.active = false
	self.id = obj:id()
end
function action_light:reset_scheme()
    printf("Light zone insert")
    light_zones[self.id] = self
end
function action_light:update(delta)
	if xr_logic.try_switch_to_another_section(self.object, self.st, db.actor) then
	   self.active = false     
	   self.stalkers = {}
       printf("Light zone set nil")
	   light_zones[self.id] = nil
	   return
	end	
	self.active = true
end
function action_light:check_stalker(stalker)
    if self.active == false then 
       return false, false
    end

	if self.object:inside(stalker:position()) then
		return self.st.light, true
	end

	return false, false
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_light (npc, storage)
	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)
	st.light = utils.cfg_get_bool (ini, section, "light_on", npc, false, false)
end



---------------------------------------------------------------------------------------------------------------------
function check_light(stalker)
--'	printf("[check_light %s] start", stalker:name())
    if stalker == nil then 
       return 
    end

--'	printf("[check_light %s] start_passed", stalker:name())
    local torch = stalker:object("device_torch")
    if torch == nil then
       return
    end

--'	printf("[check_light %s] torch_passed", stalker:name())
	local light = false
	local forced = false
	
	if benchmark.light then
		light  = true
		forced = true
	end
	
	if stalker:alive() == false then
		light  = false
		forced = true
	end

--'	printf("[check_light %s] alive_passed: light=%s, forced=%s", stalker:name(), tostring(light), tostring(forced))
    --' Проверка по лайтзонам
    if not forced then
	    for k,v in pairs(light_zones) do
			light, forced = v:check_stalker(stalker)
			if forced == true then
				break
			end
	    end
	end

--'	printf("[check_light %s] lightzone_passed: light=%s, forced=%s", stalker:name(), tostring(light), tostring(forced))
	if not forced then
		--' Проверка по времени суток (ночью всегда включено)
		local htime = level.get_time_hours()
		if htime <= 4 or htime >= 22 then
			light = true
		end
	
		--' Проверка на индор-аутдор (внутри всегда включено)
		if light == false then
			if indoor_levels[level.name()] == true then
				light = true
			end
		end
	end			

--'	printf("[check_light %s] timezone_passed: light=%s, forced=%s", stalker:name(), tostring(light), tostring(forced))
	--' Проверка по активному действию
	if not forced and light == true then
		--' Фонарики должны быть выключены у костра
		local scheme = db.storage[stalker:id()].active_scheme
		if scheme == "kamp" or
		   scheme == "camper" 
		then
			light = false
			forced = true
		end
	end

--'	printf("[check_light %s] scheme_passed: light=%s, forced=%s", stalker:name(), tostring(light), tostring(forced))	
--	if not forced and light == true then
--		--' Фонарики должны быть выключены в бою. Но если в бою идет поиск врага - то включены.
--		if stalker:best_enemy() ~= nil then
--			light = false
--		end				
--	end
	
	if light ~= nil then
		torch:enable_attachable_item(light)
	end
end

function clean_up ()
--'   printf("Light zones cleanup")
   light_zones = {} 
end