record_table = {}

record_table.ButtonIds = { easy_button = "Easy",
                           normal_button = "Normal",
						   hard_button = "Hard",
						   expert_button = "Expert"}

						   
record_table.GameXmlValue = { easy = 0,
                                   normal = 1,
  									hard = 2,
  									expert = 3
									}

						   
record_table.RecordTableTypesId = { Local = 0,
                                    Daily = 1,
  									Global = 2 }
									
record_table.ClearButtonAviable = { Local = true,
                                    Daily = false,
  									Global = false }
									
record_table.TabNames = {}
record_table.TabNames[0] = "Local"
record_table.TabNames[1] = "Daily"
record_table.TabNames[2] = "Global"

record_table.DifLevelsNames = {}
record_table.DifLevelsNames[0] = "Easy"
record_table.DifLevelsNames[1] = "Normal"
record_table.DifLevelsNames[2] = "Hard"
record_table.DifLevelsNames[3] = "Expert"
									
									
record_table.DiffLevelsId = { Easy = true,
                              Normal = true,
                              Hard = true,
  							  Expert = true }
									
record_table.DiffLevelsNums = { Easy = 0,
                              Normal = 1,
                              Hard = 2,
  							  Expert = 3 }

record_table.table_widgets = {}
record_table.table_widgets["Local"] = {}
record_table.table_widgets["Daily"] = {}
record_table.table_widgets["Global"] = {}

record_table.button_widgets = {}
record_table.button_widgets["Local"] = {}
record_table.button_widgets["Daily"] = {}
record_table.button_widgets["Global"] = {}

record_table.ClearButtonWidget = false
record_table.header_row = false
record_table.string_row = false
record_table.template = false
record_table.tabs_widget = false

function record_table.activate_dif_level(_owner, _table_type, _dif_level_id)
	--[[
	for dif_level_id in pairs(record_table.DiffLevelsId) do
		local current_button = record_table.button_widgets[_table_type][dif_level_id]
		if current_button then
			
			current_button.DisableButton(false)
		end
	end
	for dif_level_id in pairs(record_table.DiffLevelsId) do
		set_widget_flag_value(record_table.table_widgets[_table_type][dif_level_id], sf.gui.CBaseWidget.FlagHidden, true)
		set_widget_flag_value(record_table.table_widgets[_table_type][dif_level_id], sf.gui.CBaseWidget.FlagDisabled, true)
	end
	
	if record_table.table_widgets[_table_type][_dif_level_id] then
		set_widget_flag_value(record_table.table_widgets[_table_type][_dif_level_id], sf.gui.CBaseWidget.FlagHidden, false)
		set_widget_flag_value(record_table.table_widgets[_table_type][_dif_level_id], sf.gui.CBaseWidget.FlagDisabled, false)
	end

	if record_table.button_widgets[_table_type][_dif_level_id] then
		record_table.button_widgets[_table_type][_dif_level_id].DisableButton(true)
	end
	]]
end

         
function record_table.run_record_table_window(_owner)
	
	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_record_table_window"), true), 
		app.GetSettings().GetChild(_T("GUITemplates"), false))
	window.UpdateLayout()
	MoveWindowToCenter(_owner, window)
	record_table.form_record_table_window(window)


	local current_profile = s4.g_ProfileManager.Instance().GetCurrentProfile()
	if current_profile then
		local last_played_dif_level = current_profile.GetLastPlayedDifficultyLevelId() 
		if T_(last_played_dif_level).c_str() == "" then
				
			--difficulty_level = s4.misc.GetGameplaySettings().GetValue(_T("difficulty_level"), true)
			local default_difficulty = app.GetSettings().GetChild(_T("settings"), false).GetChild(_T("gameplay"), false).GetValue(_T("difficulty_level"))
			--local default_difficulty = s4.g_Difficulty.Instance().GetDifficultyLevel()
			record_table.tabs_widget.SelectPanel(record_table.GameXmlValue[T_(default_difficulty).c_str()])
		else
			if diff_levels_enum[T_(last_played_dif_level).c_str()] then
				record_table.tabs_widget.SelectPanel(difficulty_list[T_(last_played_dif_level).c_str()].Index)
			end
		end
		--log("dln = "..tostring (diff_levels_enum[T_(last_played_dif_level).c_str()]))
		
		--local default_difficulty = s4.g_Difficulty.Instance().GetDifficultyLevel()		
		--local last_played_dif_level = current_profile.GetLastPlayedDifficultyLevelId() --_T("Normal") --
		--log("last_played_dif_level = "..last_played_dif_level)
	
	end
		
	--record_table.tabs_widget.SelectPanel(difficulty_list[T_(last_played_dif_level).c_str()].Index)

	local window_ptr = sf.gui.CBaseWindowPtrT(window)
	_owner.DoModal(window_ptr) 
end

function record_table.form_record_table_window(_owner)
	record_table.form_table_widgets(_owner)
	record_table.fill_table_widgets(_owner)
	record_table.activate_dif_level(_owner, "Local", "Easy")
--	record_table.activate_dif_level(_owner, "Daily", "Easy")
--	record_table.activate_dif_level(_owner, "Global", "Easy")
	record_table.tab_select_event(_owner, "Local")
end

function record_table.form_table_widgets(_owner)

	local app = sf.core.g_Application
	record_table.header_row = app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(
		_T("window"), _T("id"), _T("s4_record_table_window"), true).GetChildByAttribute(
			_T("table_widget"), _T("id"), _T("table_easy_local"), true).GetNamedChild(_T("rows"), false).GetChild(_T("row"), false)
	record_table.string_row = record_table.header_row.GetNextSibling()
	record_table.template = app.GetSettings().GetChild(_T("GUITemplates"), false)

	record_table.tabs_widget = __cast(_owner.GetWidget(_T("record_tables_tabs"),true).get(), "sf.gui.CTabWidget")
	
	record_table.ClearButtonWidget = __cast(_owner.GetWidget(_T("clear_button"),true).get(), "sf.gui.CButtonWidget")
	
	local base_table = __cast(_owner.GetWidget(_T("table_easy_local"),true).get(), "sf.gui.CTableWidget")

	set_widget_flag_value(base_table, sf.gui.CBaseWidget.FlagHidden, true)
	set_widget_flag_value(base_table, sf.gui.CBaseWidget.FlagDisabled, true)

	
	local base_table_offset = base_table.GetOffset()
	
	
	
	
	local app = sf.core.g_Application
	
	local base_table_group = app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute( 
		_T("window"), _T("id"), _T("s4_record_table_window"), true).GetChildByAttribute(
			_T("table_widget"), _T("id"), _T("table_easy_local"), true)
	local template = app.GetSettings().GetChild(_T("GUITemplates"), false)
	
	
	--local tab_index = 0
	local type_id = "Local" 
	--[[
	for type_id in pairs(record_table.RecordTableTypesId) do
		

		local widget = current_tab_content.GetWidgets()
		while not widget.IsEnd() do 
			--
			local button_widget = __cast(widget.Get().get(), "sf.gui.CButtonWidget")
			if button_widget then
				if record_table.ButtonIds[T_(button_widget.GetId()).c_str()] then
					record_table.button_widgets[type_id][ record_table.ButtonIds[T_(button_widget.GetId()).c_str()] ] = button_widget
				end
			end
			widget.Next()
		end	
	]]
		
		for dif_level_id in pairs(record_table.DiffLevelsId) do
			local current_tab_content = record_table.tabs_widget.GetPanelContent(record_table.DiffLevelsNums[dif_level_id])
			record_table.table_widgets[type_id][dif_level_id] = sf.gui.CTableWidget(_T("table_id"), 0, 0)
			record_table.table_widgets[type_id][dif_level_id].Load(base_table_group, template)
			record_table.table_widgets[type_id][dif_level_id].SetOffset(base_table_offset.X, base_table_offset.Y)
			--set_widget_flag_value(record_table.table_widgets[type_id][dif_level_id], sf.gui.CBaseWidget.FlagHidden, true)
			--set_widget_flag_value(record_table.table_widgets[type_id][dif_level_id], sf.gui.CBaseWidget.FlagDisabled, true)
			current_tab_content.AddWidget(sf.gui.CWidgetPtrT(record_table.table_widgets[type_id][dif_level_id]))
--			tab_index = tab_index + 1	
		end
		
		
	--end

	--[[
	for type_id in pairs(record_table.RecordTableTypesId) do
		for dif_level_id in pairs(record_table.DiffLevelsId) do
			local current_button = record_table.button_widgets[type_id][dif_level_id]
			if current_button then
				
				current_button.DisableButton(false)
			end
		end
	end
]]

end

function record_table.fill_one_table(_owner, _type, _dif_level)
	local current_table_widget = record_table.table_widgets[_type][_dif_level]

	if current_table_widget then
		current_table_widget.ClearTable()
		current_table_widget.LoadRow(record_table.header_row, record_table.template)
	   
		local record = s4.g_RecordTable.Instance().GetRecords(_T(_type), _T(_dif_level))
		local last_record_pos = s4.g_RecordTable.Instance().GetLastRecordPos(_T(_type), _T(_dif_level))
		local yellow_color = sf.graphics.Color(255, 255, 255, 0);
		local nomer = 1
		while not record.IsEnd() do 
			
			current_table_widget.LoadRow(record_table.string_row, record_table.template)
			__cast(current_table_widget.GetCell(nomer, 0).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetText(_T("  "..tostring(nomer)))
			__cast(current_table_widget.GetCell(nomer, 1).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetText(_T("  ") .. record.Get().Name)
			__cast(current_table_widget.GetCell(nomer, 2).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetText(_T("  "..tostring(record.Get().Score)))
			__cast(current_table_widget.GetCell(nomer, 3).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetText(_T("  "..tostring(record.Get().Gems)))
			__cast(current_table_widget.GetCell(nomer, 4).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetText(_T("  "..tostring(record.Get().CampName)))
		if nomer == last_record_pos then
			__cast(current_table_widget.GetCell(nomer, 0).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetColor(yellow_color)
			__cast(current_table_widget.GetCell(nomer, 1).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetColor(yellow_color)
			__cast(current_table_widget.GetCell(nomer, 2).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetColor(yellow_color)
			__cast(current_table_widget.GetCell(nomer, 3).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetColor(yellow_color)
			__cast(current_table_widget.GetCell(nomer, 4).GetWidgets().Get().get(), "sf.gui.CLabelWidget").SetColor(yellow_color)
		end
			nomer = nomer + 1
			record.Next()
		end	
		current_table_widget.FitCells() 
	end

end

function record_table.fill_table_widgets(_owner)
	local current_profile = s4.g_ProfileManager.Instance().GetCurrentProfile()

	--for type_id in pairs(record_table.RecordTableTypesId) do
	
	local type_id = "Local"
		for dif_level_id in pairs(record_table.DiffLevelsId) do
			record_table.fill_one_table(_owner, type_id, dif_level_id)			
		end
	
	--end
end

function record_table.tab_select_event(_owner, _tab_type_id)
	record_table.ClearButtonWidget.DisableButton(not record_table.ClearButtonAviable[_tab_type_id])
end

function record_table.record_table_window_script(_owner, _action, _child)
	if _child.GetId()==_T("record_tables_tabs") and _action==_T("panel_selected") then
		--local type_name = record_table.TabNames[record_table.tabs_widget.GetSelectedPanel()]
		--record_table.tab_select_event(_owner, type_name)
	elseif _child.GetId()==_T("ok_button") and _action==_T("pressed") or _action == _T("key_down_enter") or _action == _T("key_down_escape") then
		_owner.EndModal(1)
	elseif _child.GetId()==_T("clear_button") and _action==_T("pressed") then
		--local type_name = record_table.TabNames[record_table.tabs_widget.GetSelectedPanel()]
		--if type_name == "Local" then
			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_clear_dialog"), true), 
				app.GetSettings().GetChild(_T("GUITemplates"), false))
			window.UpdateLayout()   
			MoveWindowToCenter(_owner, window)
			
			local window_ptr = sf.gui.CBaseWindowPtrT(window)
			
			local modal_res = _owner.DoModal(window_ptr)
			window_ptr = nil
			collectgarbage()			
			if  modal_res == 1  then
				local dif_level_name = record_table.DifLevelsNames[record_table.tabs_widget.GetSelectedPanel()]
				for button_id in pairs(record_table.button_widgets["Local"]) do
					if	sf.misc.BitwiseAnd(record_table.button_widgets["Local"][button_id].GetButtonStateInt(), sf.gui.CButtonWidget.StateDisabled ) ~= 0 then
						dif_level_name = record_table.ButtonIds[T_(record_table.button_widgets["Local"][button_id].GetId()).c_str()]
						
					end
				end
				if dif_level_name ~= "" then
					s4.g_RecordTable.Instance().ClearTable(_T("Local"), _T(dif_level_name))
					record_table.fill_one_table(_owner, "Local", dif_level_name)
				end
			else
			end
		--end
	elseif _child.GetId()==_T("easy_button") and _action==_T("pressed") then
		local type_name = record_table.TabNames[record_table.tabs_widget.GetSelectedPanel()]
		record_table.activate_dif_level(_owner, type_name, "Easy")
	elseif _child.GetId()==_T("normal_button") and _action==_T("pressed") then
		local type_name = record_table.TabNames[record_table.tabs_widget.GetSelectedPanel()]
		record_table.activate_dif_level(_owner, type_name, "Normal")
	elseif _child.GetId()==_T("hard_button") and _action==_T("pressed") then
		local type_name = record_table.TabNames[record_table.tabs_widget.GetSelectedPanel()]
		record_table.activate_dif_level(_owner, type_name, "Hard")
	elseif _child.GetId()==_T("expert_button") and _action==_T("pressed") then
		local type_name = record_table.TabNames[record_table.tabs_widget.GetSelectedPanel()]
		record_table.activate_dif_level(_owner, type_name, "Expert")
	end
end
