child_scenes = child_scenes or {}

child_scenes.close_button_text = ""

function child_scenes.replase_minigame_buttons(_widget)
    local end_button_widget = get_child_widget(_widget, "end_game", sf.gui.CButtonWidget)
    local solve_button_widget = get_child_widget(_widget, "solve_game", sf.gui.CButtonWidget)
    if end_button_widget and solve_button_widget and __and(solve_button_widget.GetFlags(), sf.gui.CBaseWidget.FlagHidden) == 0 then
        local minigame_size = _widget.GetSize()
        local end_button_size = end_button_widget.GetSize()
        local solve_button_size = solve_button_widget.GetSize()
        local max_width = end_button_size.X
        if max_width < solve_button_size.X then
            max_width = solve_button_size.X
        end
        end_button_widget.SetSize(max_width, end_button_size.Y)
        solve_button_widget.SetSize(max_width, solve_button_size.Y)
        end_button_size = end_button_widget.GetSize()
        solve_button_size = solve_button_widget.GetSize()
        end_button_widget.SetOffset(minigame_size.X/2 + minigame_window.buttons_space, minigame_size.Y - end_button_size.Y - minigame_window.buttons_bottom_space)
        solve_button_widget.SetOffset(minigame_size.X/2 - minigame_window.buttons_space - solve_button_size.X, minigame_size.Y - solve_button_size.Y - minigame_window.buttons_bottom_space)

    elseif end_button_widget then
        local minigame_size = _widget.GetSize()
        local button_size = end_button_widget.GetSize()
        end_button_widget.SetOffset(minigame_size.X/2 - button_size.X/2, minigame_size.Y - button_size.Y - minigame_window.buttons_bottom_space)
    end
end

function child_scenes.minigame(_id, _x, _y, _text, _close_pos_x, _close_pos_y)
    commit_score()
    if quest.global_skip_windows_flag == true then
        return false
    end
    quest.global_modify_level_flag = true 
    quest.global_minigame_window_result = nil

    objects_box_lua_widget.ShowObjectsWithAlpha(true)
    local minigame_res = nil
    minigame_res = quest.game_resources.GetChild("GUI", false).GetChildByAttribute("window", "id", _id, true)
    if minigame_res.GetId().c_str() == "" then
        minigame_res = quest.game_resources.GetChild("GUI", false).GetChildByAttribute("window", "id", "assemble_image_circle", true)
    end
    --quest.global_played_level.SetHintText(_T(""))
    quest.global_cursor.SetHintText(_T(""), false)
    local minigame_widget = sf.gui.CLuaWidget("minigame_widget", 15, __or(sf.gui.CWidget.BroadcastOnKeyDown, sf.gui.CWidget.BroadcastOnMouseMove))

    minigame_window.self = minigame_widget

    minigame_widget.Load(minigame_res, quest.game_resources.GetChild("GUITemplates", false))
    
    minigame_widget.UpdateLayout()
    
    local minigames_info = quest.game_resources.GetChild("Constants", false).GetChildRef("minigame", false)
    if minigames_info then
        local res = tonumber(minigames_info.GetValue("buttons_from_center_space").c_str())
        if res ~= nil then minigame_window.buttons_space = res end

        res = tonumber(minigames_info.GetValue("buttons_bottom_space").c_str())
        if res ~= nil then minigame_window.buttons_bottom_space = res end

    end

    local solve_button_widget = get_child_widget(minigame_widget, "solve_game", sf.gui.CButtonWidget)
    if solve_button_widget then
        minigame_window.InitSolveButton(solve_button_widget)
    end
    

    local window_template = minigame_res.GetValue("window_template").c_str()

    if window_template and window_template ~= "" then
        local templates = quest.game_resources.GetChild("GUITemplates", false).GetChildByAttribute("window_template", "id", window_template, false)
        if templates.GetId().c_str() ~= "" then
            local back_ground_id = templates.GetValue("background").c_str()
            minigame_window.background_image = sf.graphics.CImage(back_ground_id);

            minigame_window.background_image.SetFlags(__or(__or(minigame_window.background_image.GetFlags(), sf.graphics.CImage.FlagBoxImage), sf.graphics.CImage.FlagIntDest))
            local minigame_size = minigame_widget.GetSize()

            minigame_window.background_image.SetRect( sf.misc.FloatRect(0, 0, minigame_size.X, minigame_size.Y))
        end
    end
    local parent_size = quest.global_quest_window.GetSize()
    if _x == nil then
        _x = (parent_size.X - minigame_widget.GetSize().X) /2
    end

    if _y == nil then
        _y = (parent_size.Y - minigame_widget.GetSize().Y) /2
    end

    minigame_widget.SetOffset(_x, _y)

    --shadow_widget.Show(true)
    hints_lua_widget.HideHint()
    scene_click_widget.Enable(false)

    local click_pos = quest.g_ClickPos
    if not click_pos then
        click_pos = sf.misc.FloatVector(-1000, -1000)
    end
    child_scenes.init_minigame_pos = click_pos

    if child_scenes.init_minigame_pos then
        if _close_pos_x then
            child_scenes.init_minigame_pos.X = _close_pos_x
        end
        if _close_pos_y then
            child_scenes.init_minigame_pos.Y = _close_pos_y
        end
    end

    quest.OnOpenMinigame(minigame_widget)
    

    quest.global_quest_window.AddWidget(minigame_widget)
    difficulty.ApplyDifLevelForMinigames()
    child_scenes.replase_minigame_buttons(minigame_widget)

    quest.global_cursor.SetDisableHints(true)
    --set_widget_flag_value(objects_box_lua_widget.self, sf.gui.CBaseWidget.FlagDisabled, true)  

	quest.global_minigame_opened = minigame_widget
	
    if _text ~= nil then
        say(_text)
    end

    local hint_tutorial_value = hints_lua_widget.get_disable_hint_tutorial()
    hints_lua_widget.disable_hint_tutorial(true)

    hints_lua_widget.set_show_hint_tutorial(false)

    quest.do_not_change_curosor = true
	cursor_lua_widget.DropCursor()
    sound("mini_open_close")

    while true do
        if quest.global_minigame_window_result ~= nil then
            break
        end
        sleep(100)
    end
    hints_lua_widget.disable_hint_tutorial(hint_tutorial_value)
	
	quest.global_minigame_opened = false
	
	quest.OnCloseMinigame(minigame_window.self, quest.global_minigame_window_result == 1 or quest.global_minigame_window_result == 2)
	
    --set_widget_flag_value(objects_box_lua_widget.self, sf.gui.CBaseWidget.FlagDisabled, false)  
    quest.global_cursor.SetDisableHints(false)
    objects_box_lua_widget.ShowObjectsWithAlpha(false)
    scene_click_widget.Enable(true)
    --shadow_widget.Show(false)
    quest.do_not_change_curosor = false
    
    if quest.global_minigame_window_result == 1 then
        add_score("minigame_type", 1)
    end

    if quest.global_minigame_window_result == 1 or quest.global_minigame_window_result == 2 then
       return true
    end
    return false
end

function child_scenes.close_scene(_scene_id)
    if _scene_id ~= nil then
        quest.global_scene_widget.CloseChildSceneById(_scene_id)
    end
end

function child_scenes.close_minigame(_value)
    local current_widget = quest.global_quest_window.GetWidgets()
    while not current_widget.IsEnd() do
        local widget_id = current_widget.Get().GetId().c_str()
        local lua_widget = __cast(current_widget.Get(), sf.gui.CLuaWidget )
        if lua_widget then
            local overload_table = lua_widget.GetOverloadTable()
            if string.find(overload_table, "minigame") then
                say_widget.close_say()
                minigame_window.CloseMinigame(_value)
                return true
            end
        end
        current_widget.Next()
    end
    return false
end

function child_scenes.disable_scene_sounds(_scene)
        local current_child = _scene.GetObjects()
        while not current_child.IsEnd() do
            local object = current_child.Get()
            local type = object.GetType()
            if  type == qe.CBaseSceneObject.GroupType then
                local group = __cast(object.get(), qe.CSceneGroup)
                if group then
                    local current_group_child = group.GetChilds()
                    while not current_group_child.IsEnd() do
                        local clip_object = __cast(current_group_child.Get(), qe.CClipObject)
                        if clip_object then
                            clip_object.DisableSounds(true)
                        end
                        current_group_child.Next()
                    end 
                end
            else
                local clip_object = __cast(object, qe.CClipObject)
                if clip_object then
                    clip_object.DisableSounds(true)
                end
            end
            current_child.Next()
        end

end

function child_scenes.show_scene(_scene_id, _x, _y, _init_offset_x, _init_ffset_y)
    if _scene_id == nil then return end
	
    local scene_widget = quest.get_scene_widget(_scene_id)
    if scene_widget ~= nil then return end
	target_scene = quest.global_played_level.GetSceneById(_scene_id)    
	if target_scene == nil then
		__message("Сцена "..tostring(_scene_id).." не найдена", "Ошибка")
		return 
	end
    quest.global_modify_level_flag = true 
	local in_a_window = target_scene.ReadFlag(qe.CScene.FlagSceneInNewWindow)
	local new_widget = qe.CSceneWidget(_T("standart"), true, quest.game_resources.GetChild("GUITemplates", true)) --in_a_window

    new_widget.SetCloseButtonText(child_scenes.close_button_text)
    
	if in_a_window then
        sound("mini_open_close")

		-- минисцена
		new_widget.SetScene(target_scene)
		if _x ~= nil and _y ~= nil then
			new_widget.SetOffset(_x, _y)    
		end
        hints_lua_widget.HideHint(new_widget.GetOffset(), new_widget.GetSize())
        quest.OnOpenScene(new_widget, _init_offset_x, _init_ffset_y)
		quest.global_scene_widget.AddWidget(new_widget)
		quest.global_cursor.SetHintText(_T(""), false)
	else
        hints_lua_widget.HideHint()
        child_scenes.disable_scene_sounds(quest.global_current_main_scene)
		-- главная сцена
		quest.global_scene_widget.CloseChildScenes()
		quest.global_scene_widget.SetScene(target_scene)
		quest.global_current_main_scene = target_scene
		quest.global_cursor.SetWorkAreaSize(quest.global_scene_widget.GetSize())
		quest.global_cursor.SetHintText(_T(""), false)
		quest.OnLevelChanged()
	end
end

function child_scenes.LoadConstants(_constants)
	local miniscene_info = _constants.GetChild("miniscene", false)
    if miniscene_info then
        child_scenes.close_button_text = miniscene_info.GetValue("close_button_text").c_str()
    end

--[[
	local self = cursor_lua_widget
	
	local cursors = _constants.GetChild("cursors", false)
    self.object_offset.X = tonumber(cursors.GetValue("object_offset_x").c_str()) or self.object_offset.X
	self.object_offset.Y = tonumber(cursors.GetValue("object_offset_y").c_str()) or self.object_offset.Y
    quest.global_cursor.SetObjectOffset(self.object_offset)
	
	self.object_scale.X = tonumber(cursors.GetValue("object_scale_x").c_str()) or self.object_scale.X
	self.object_scale.X = tonumber(cursors.GetValue("object_scale_y").c_str()) or self.object_scale.Y
    quest.global_cursor.SetCursorObjectScale(self.object_scale)
	
	quest.go_to_hourglass_delay = tonumber(cursors.GetValue("go_to_hourglass_delay").c_str())
	
	
	local hints = _constants.GetChild("hints", false)
    self.hint_offset.X = tonumber(hints.GetValue("hint_offset_x").c_str()) or self.hint_offset.X
	self.hint_offset.Y = tonumber(hints.GetValue("hint_offset_y").c_str()) or self.hint_offset.Y
    quest.global_cursor.SetHintOffset(self.hint_offset)

    local res = hints.GetValue("hint_font").c_str()
    if res ~= "" then
        cursor_lua_widget.hint_font = res
    end

    quest.global_cursor.SetHintFont(cursor_lua_widget.hint_font)

    local ring_offset_X = tonumber(cursors.GetValue("ring_offset_x").c_str())
	local ring_offset_Y = tonumber(cursors.GetValue("ring_offset_y").c_str())
	cursor_lua_widget.ring_object_width = tonumber(cursors.GetValue("ring_object_width").c_str()) or cursor_lua_widget.ring_object_width

    quest.global_cursor.SetPreObjectOffset(sf.misc.FloatVector(ring_offset_X, ring_offset_Y))


	local ring_id = cursors.GetValue("ring_id").c_str()
	if ring_id ~= "" then
        quest.global_cursor.SetPreObject(ring_id)
    end
    quest.global_cursor.SetObjectWidth(cursor_lua_widget.ring_object_width)
    
]]
end
