select_level = {}
select_level.max_level_num = 10
select_level.levels_num = 0

select_level.camp_info = {}
select_level.new_game_dif_level_id = "Normal"

select_level.scr_border_width = 3
select_level.label_bottom_offset = 10

select_level.level_number_label_font = _T("giant_text")
select_level.level_number_label_scale = 0.7

function select_level.form_the_level_cell(_test_fixed_table, _row, _col, _level_number, _aviable)

	local lock_image = sf.graphics.CImage(_T("gui_game_gui_lock"))		

	local font_id = select_level.level_number_label_font

	local font = sf.core.g_Application.GetResourceManager().GetFont(font_id)

	local level_image = sf.graphics.CImage(select_level.current_campaign.GetLevelThumbnail(_level_number))
	
	local current_cell = _test_fixed_table.GetCell(_row, _col);
	
	if current_cell then
		local size_of_cell = _test_fixed_table.GetCellSize()

		local scr_offset_x = select_level.scr_border_width
		local scr_offset_y = select_level.scr_border_width
		local scr_offset_width = size_of_cell.X - select_level.scr_border_width*2
		local scr_offset_height = size_of_cell.Y - select_level.scr_border_width*2
		local image_widget = sf.gui.CWidgetPtrT(sf.gui.CImageWidget(level_image, _T("level_image"), 0, 0))
		
		image_widget.get().SetSize(scr_offset_width, scr_offset_height)
		image_widget.get().SetOffset(scr_offset_x, scr_offset_y)
		current_cell.AddWidget(image_widget)
	
		if _aviable	then
			local  image_widget = sf.gui.CWidgetPtrT(sf.gui.CImageWidget(lock_image, _T("lock_image"), 0, 0))
			local lock_image_size = image_widget.get().GetSize()
			image_widget.get().SetOffset(scr_offset_x + scr_offset_width/2 - lock_image_size.X/2, scr_offset_y + scr_offset_height/2 - lock_image_size.Y/2)
			current_cell.AddWidget(image_widget)
		end

		local  din_label_widget = sf.gui.CLabelWidget(font, _T(tostring(_level_number + 1)), _T("test_label_id"), 0, 0)
		din_label_widget.SetFontScale(select_level.level_number_label_scale)
		din_label_widget.ResizeToFitText(true)
		local text_size = din_label_widget.GetSize() * select_level.level_number_label_scale
		din_label_widget.SetOffset(size_of_cell.X/2 - text_size.X/2, size_of_cell.Y - text_size.Y - select_level.label_bottom_offset)
		current_cell.AddWidget(sf.gui.CWidgetPtrT(din_label_widget))
		_test_fixed_table.SetFlagToCell(_row, _col, s4.CFixedTableWidget.FlagCellEnabled, not _aviable)
	end
end

function select_level.form_select_level_window(_owner, _current_level)
	local levels_count = select_level.levels_num
	local aviable_levels_count = select_level.max_level_num + 1
	
	local test_fixed_table = __cast(_owner.GetWidget(_T("test_fixed_table_widget")).get(), "s4.CFixedTableWidget")
	local size_in_cells = test_fixed_table.GetSizeInCells()
	local selected_cell = {}
	
	local camp_name_label = __cast(_owner.GetWidget(_T("campaing_name_label")).get(), "sf.gui.CLabelWidget")
	camp_name_label.SetText(sf.misc.g_StringTable.Instance().FormatByStringsID(select_level.camp_info.Id))
		
	selected_cell.Y = _current_level /size_in_cells.X
	selected_cell.X = math.mod(_current_level, size_in_cells.X)

	local full_strings_count = levels_count/size_in_cells.X
	local last_row_cell_count = math.mod(levels_count, size_in_cells.X)
	local current_level = 0
	for i = 0,full_strings_count - 1 do
		if i >= size_in_cells.Y then
			test_fixed_table.AddRow()
		end
		for j = 0,size_in_cells.X - 1 do
		
			local aviable = false
			if current_level >= aviable_levels_count then
				aviable = true
			end
			select_level.form_the_level_cell(test_fixed_table, i, j, current_level, aviable)
			current_level = current_level + 1;
		end
	end
	if last_row_cell_count > 0 then
		if full_strings_count >size_in_cells.Y then 
			test_fixed_table.AddRow()
		end
		for j = 0,last_row_cell_count-1 do

			local aviable = false
			if current_level >= aviable_levels_count then
				aviable = true
			end
			select_level.form_the_level_cell(test_fixed_table, full_strings_count, j, current_level, aviable)
			current_level = current_level + 1;
		end
	end
	test_fixed_table.SetSelectedCell(selected_cell.Y, selected_cell.X, false)
end

function select_level.run_select_level_window(_owner, _camp_info, _dif_level_id, _current_level)

	select_level.levels_num = _camp_info.common_info.NumLevels
	select_level.max_level_num = _camp_info.common_info.MaxLevelFinished
	if _camp_info.common_info.MaxLevelFinished == _camp_info.common_info.NumLevels then
		select_level.max_level_num = select_level.max_level_num - 1
	end
	select_level.new_game_dif_level_id = _dif_level_id
	select_level.camp_info = _camp_info
	if _current_level >= _camp_info.common_info.NumLevels then
		_current_level = 0
	end
	select_level.current_campaign = s4.le.g_Campaigns.Instance().GetCampaignByGUID(select_level.camp_info.GUIDStr)
	if select_level.current_campaign then
		select_level.current_campaign.LoadLevelsThumbnails()
	end

	local app = sf.core.g_Application
	local window = s4.COnKeyDownWindow()
	window.Load(
		app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("s4_select_level"), true), 
		app.GetSettings().GetChild(_T("GUITemplates"), false))
	window.UpdateLayout()                            
	MoveWindowToCenter(_owner, window)
	select_level.form_select_level_window(window, _current_level)	
	window_ptr = sf.gui.CBaseWindowPtrT(window)
	set_widget_flag_value(_owner, sf.gui.CBaseWidget.FlagHidden, true)
	_owner.AddChildWindow(window_ptr)
	--window_ptr = nil
	
	--local window_ptr = sf.gui.CBaseWindowPtrT(window)

	--_owner.DoModal(window_ptr) 
	--set_widget_flag_value(_owner, sf.gui.CBaseWidget.FlagHidden, false)
	
end

function select_level.on_action_script(_owner, _action, _child)
	if (_child.GetId()==_T("play") and _action==_T("pressed") ) or (_child.GetId()==_T("test_fixed_table_widget") and _action==_T("double_click")) then
		local test_fixed_table = __cast(_owner.GetWidget(_T("test_fixed_table_widget")).get(), "s4.CFixedTableWidget")
											   
		local selected_cell = test_fixed_table.GetSelectedCell()
		if selected_cell.X ~= -1 and selected_cell.Y ~= -1 then
			local size_in_cells = test_fixed_table.GetSizeInCells()
			local selected_level = size_in_cells.X * selected_cell.Y + selected_cell.X
			
			s4.g_Difficulty.Instance().SetDifficultyLevel(diff_levels_enum[select_level.new_game_dif_level_id])
			main_menu.start_game(_owner, select_level.camp_info.GUIDStr, selected_level , 0, 0, game.StartLivesNum, false) 
		end
	elseif _child.GetId()==_T("cancel") and _action==_T("pressed") or _action == _T("key_down_escape") then
		--_owner.EndModal(1)
		set_widget_flag_value(_owner.GetParentWindow(), sf.gui.CBaseWidget.FlagHidden, false)
		_owner.AddFlags(sf.gui.CBaseWidget.FlagDead)
		
	end
end
