states = staes or {}
states.change_effect_timer = sf.core.CTimerWrapper(sf.core.g_Application.GetTimeManager().GetTimer(_T("qe_level")).AttachTimer(_T("")))
states.use_bottom_change_state_default = true
states.change_effect_time = 300
states.sleep_after_change_state = true

--states.global_scene_light_flag = true

function states.IsAntiState(_id, _light_flag)
    local find_res = string.find(_id, "#dark")
    if find_res ~= nil then
        return _light_flag
    else
        return not _light_flag
    end
end

function states.FormAntiState(_id, _light_flag)
    if _light_flag == true then
        return _id.."#dark"
    else
        local dark_place = string.find(_id, "#dark")
        if dark_place ~= nil then
            return string.sub(_id, 1, dark_place - 1 )
        else
            return _id
        end
    end
end

function states.turn_light(_scene_id, _value)
    quest.global_modify_level_flag = true 
    if _value == nil then
        _value = _scene_id
        _scene_id = nil
    end
    if not _value then
        _value = true
    else
        _value = false
    end
    local  targen_scene = quest.global_current_main_scene
    if scene_id ~= nil then
        targen_scene = quest.global_played_level.GetSceneById(_scene_id)
    end
    if targen_scene then
        current_object = targen_scene.GetObjects()
        while not current_object.IsEnd() do 
            local current_group = __cast(current_object.Get(), qe.CSceneGroup)
            if current_group ~= nil then
                local visible_states_list = {}
                local anti_states_list = {}
                
                local current_state = current_group.GetChilds()
                while not current_state.IsEnd() do
                    if states.IsAntiState(current_state.Get().GetId().c_str(), _value) then
                        anti_states_list[current_state.Get().GetId().c_str()] = current_state.Get()
                    end

                    if not current_state.Get().ReadObjectFlag(qe.CBaseSceneObject.ObjectFlagHidden) then
                        visible_states_list[current_state.Get().GetId().c_str()] = current_state.Get()
                    end
                    current_state.Next()
                end
                for i,o in pairs(visible_states_list) do
                    local formed_anti_state = states.FormAntiState(i, _value)
                    if anti_states_list[formed_anti_state] then
                        local wer = o.GetId().c_str()
                        o.RaiseObjectFlag(qe.CBaseSceneObject.ObjectFlagHidden)
                        anti_states_list[formed_anti_state].ClearObjectFlag(qe.CBaseSceneObject.ObjectFlagHidden)
                    end
                end
                
            end

            current_object.Next()
        end

    end
    --states.global_scene_light_flag = not states.global_scene_light_flag
end
--[[
function states.is_light_on()
    return  states.global_scene_light_flag
end
]]


function states.change_state_ex(_draw_type, _time, _sleep, _object, _state)
	if _draw_type == nil then
		_draw_type = states.use_bottom_change_state_default
	end
	if _time == nil then
		_time = states.change_effect_time
	end
	if _sleep == nil then
		_sleep = states.sleep_after_change_state
	end
	
    local current_object = nil
    if _state == nil then
       _state = _object 
       current_object = quest.global_current_script_object
    else
       current_object = quest.FindRootObjectById(_object)
    end
    if current_object then
        quest.global_modify_level_flag = true 
        group_object = __cast(current_object, qe.CSceneGroup)
        if group_object then
            if _state == nil then
                __assert(false, "Не указаны параметры для change_state")
            else
                local state_type = type(_state)
                if state_type == "string" then
                    local formed_state = _state
                    
                    local curent_state = group_object.GetFirstVisibleState().c_str()
                    if curent_state ~= "" then
                        if string.find(curent_state, "#dark") then
                            formed_state = formed_state.."#dark"
                        end
                    end
                    
                    
                    --[[
                    
                    if not states.global_scene_light_flag then
                        local test_state = states.FormAntiState(_state, true )
                        if group_object.FindState(test_state) == true then
                            formed_state = test_state
                        end
                    end
                    ]]
                    group_object.SetCurrent(formed_state, _draw_type, _time, states.change_effect_timer.Get())
                    if _time > 0 and _sleep == true then
                        sleep(_time)
                    end
                end
            end
        else
            __message("Объект "..current_object.GetId().c_str().." не является группой.", "Смена состояния невозможна")
        end
    end

end

function states.change_state(_object, _state)
	states.change_state_ex(states.use_bottom_change_state_default, states.change_effect_time, states.sleep_after_change_state, _object, _state)
end

function states.state(_object, _state)
    local current_object = nil
    if _state == nil then
       _state = _object 
       current_object = quest.global_current_script_object
    else
       current_object = quest.FindRootObjectById(_object)
    end
	if not current_object then return false end
	
	current_object = __cast(current_object, qe.CSceneGroup)
	if not current_object then return false end

    return current_object.IsCurrent(_state) or current_object.IsCurrent(_state.."#dark") 
end
