game.StartLivesNum = 3

local wrap_bonus_settings =  sf.core.g_Application.GetSettings().GetChild(_T("Constants"), false).GetChild(_T("wrap_level_bonus"), false)
game.PauseBeforeWarps = tonumber ((T_(wrap_bonus_settings.GetChild(_T("creation_delays"), false).GetValue(_T("first_delay")))).c_str())
game.PauseBetweenWarps = tonumber ((T_(wrap_bonus_settings.GetChild(_T("creation_delays"), false).GetValue(_T("next_delay")))).c_str())
game.WrapLevelCreationHeight = tonumber ((T_(wrap_bonus_settings.GetChild(_T("creation_settings"), false).GetValue(_T("creation_height")))).c_str())

difficulty_list = {}
difficulty_list["Easy"] = {}
difficulty_list["Easy"].Name = _T("Easy")
difficulty_list["Easy"].Index = 0
difficulty_list["Normal"] = {}
difficulty_list["Normal"].Name = _T("Normal")
difficulty_list["Normal"].Index = 1
difficulty_list["Hard"] = {}
difficulty_list["Hard"].Name = _T("Hard")
difficulty_list["Hard"].Index = 2
difficulty_list["Expert"] = {}
difficulty_list["Expert"].Name = _T("Expert")
difficulty_list["Expert"].Index = 3

diff_levels = 
{
	Easy = true,
	Normal = true,
	Hard = true,
	Expert = true
}

diff_levels_indexes = 
{
	Easy = 0,
	Normal = 1,
	Hard = 2,
	Expert = 3
}

diff_levels_enum = 
{
	Easy = s4.CDifficulty.DifficultyEasy,
	Normal = s4.CDifficulty.DifficultyNormal,
	Hard = s4.CDifficulty.DifficultyHard,
	Expert = s4.CDifficulty.DifficultyExpert
}

function MoveWindowToCenter(_owner, _child_window)
	local owner_size = _owner.GetSize()
	local owner_offset = _owner.GetOffset()
	local child_size = _child_window.GetSize()
	_child_window.SetOffset(owner_size.X/2 - child_size.X/2, owner_size.Y/2 - child_size.Y/2)
end

s4.g_MainLua.LoadScript(_T("scripts\\game\\download.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\update_game.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\main_menu.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\select_level.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\select_campaign.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\game_window.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\user_profiles.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\help_window.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\record_table.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\options_window.lua"))


function set_widget_flag_value(_widget, _flag, _value)
	if _value then
		_widget.SetFlags(sf.misc.BitwiseOr(_widget.GetFlags(), _flag))
	else
		if sf.misc.BitwiseAnd(_widget.GetFlags(), _flag) == 0 then
		else
			_widget.SetFlags(sf.misc.BitwiseXor(_widget.GetFlags(), _flag))
		end
	end
end

function game.update() -- called once per second
	local bricks = game.g_Level.EnumNeedToWinBricks()
	local current_stage = game.g_Level.GetCurrentStage()
	local min_brciks = game.g_Level.GetMinBricksForWarp()
	

	if bricks<=min_brciks and current_stage <= s4.CLevel.LevelStageGameplay then
		if game.PauseBeforeWarps>0 then
			game.PauseBeforeWarps = game.PauseBeforeWarps - 1
		else
			game.PauseBeforeWarps = 5 + math.random(game.PauseBetweenWarps) -- пауза между выпадениями бонусов
			local level_width = game.g_Level.GetRect().Width
			local bonus = s4.CBonus(_T("warp_level"), sf.misc.FloatVector(50 + math.random(level_width-100), game.WrapLevelCreationHeight), 100, true)
			local bonus_ptr = s4.CBonusPtrT(bonus)
			game.g_Level.AddBonus(bonus_ptr)
			bonus, bonus_ptr = nil, nil
			collectgarbage()
		end
	end	
end

function yes_no_action(_owner, _action, _child)
	if _action==_T("key_down_enter") then
		_owner.EndModal(1)
	elseif _action==_T("key_down_escape") then
		_owner.EndModal(0)
	elseif _child.GetId()==_T("yes") and _action==_T("pressed") then
		_owner.EndModal(1)
	elseif _child.GetId()==_T("no") and _action==_T("pressed") then
		_owner.EndModal(0)
	end
end

function ok_cancel_action(_owner, _action, _child)
	if _child.GetId()==_T("ok") and _action==_T("pressed") then
		_owner.EndModal(1)
	elseif _child.GetId()==_T("cancel") and _action==_T("pressed") then
		_owner.EndModal(0)
	end
end

function get_camapigns_info()
	local CamapignsList ={}
	local num_campaigns = s4.le.g_Campaigns.Instance().EnumCampaigns()
	for i = 0,num_campaigns-1 do
		local current_campaign = s4.le.g_Campaigns.Instance().GetCampaign(i)
		--local id = T_(current_campaign.GetId()) 
		local camp_guid = T_(current_campaign.GetGUIDStr()).c_str() 
		CamapignsList[camp_guid] = {}
		CamapignsList[camp_guid].Name = current_campaign.GetId()
		CamapignsList[camp_guid].Rating = current_campaign.GetAverageRating()
		CamapignsList[camp_guid].NumLevels = current_campaign.EnumLevels()
		CamapignsList[camp_guid].GUID = current_campaign.GetGUIDStr()
		
		if 1 == sf.misc.BitwiseAnd(current_campaign.GetFlags(), s4.le.CCampaign.FlagPlayerRatingAviable) then
			CamapignsList[camp_guid].IsRatingAviable = true
		else
			CamapignsList[camp_guid].IsRatingAviable = false
		end
	end
	return CamapignsList 
end

function get_profile_info(_current_profile)
	local ProfileInfo = {}							  

	local CamapignsList = get_camapigns_info()
	
	local profile_camapigns_num = _current_profile.EnumCampaigns()
	for camp_index = 0,profile_camapigns_num-1 do
		local current_campaign = _current_profile.GetCampaign(camp_index)
		--local camp_id =  T_(current_campaign.Id)
		local camp_guid =  T_(current_campaign.GUIDStr).c_str()
		ProfileInfo[camp_guid] = {}
		ProfileInfo[camp_guid].Id = current_campaign.Id
		ProfileInfo[camp_guid].GUIDStr = current_campaign.GUIDStr
		ProfileInfo[camp_guid].common_info = {}
		ProfileInfo[camp_guid].common_info.Rating = current_campaign.Rating
		ProfileInfo[camp_guid].common_info.MaxLevelFinished = 0
		ProfileInfo[camp_guid].common_info.NumLevels = 0
		if CamapignsList[T_(current_campaign.GUIDStr).c_str()] then
			ProfileInfo[camp_guid].common_info.NumLevels = CamapignsList[T_(current_campaign.GUIDStr).c_str()].NumLevels
		end
		
		local profile_dif_levels_num = current_campaign.EnumDifficultyLevels()
		ProfileInfo[camp_guid].dif_levels = {}

		for dif_level_index = 0, profile_dif_levels_num-1 do
			local current_dif_level = current_campaign.GetDifficultyLevel(dif_level_index)
			local dif_id = T_(current_dif_level.Id).c_str()
			ProfileInfo[camp_guid].dif_levels[dif_id] = {}
			ProfileInfo[camp_guid].dif_levels[dif_id].LevelsFinished = current_dif_level.TotalLevelsFinished
			ProfileInfo[camp_guid].dif_levels[dif_id].LastGameLevel = current_dif_level.CurrentLevel
			ProfileInfo[camp_guid].dif_levels[dif_id].LastGameLifes = current_dif_level.LifesCount
			ProfileInfo[camp_guid].dif_levels[dif_id].LastGameGems = current_dif_level.GemsColected
			ProfileInfo[camp_guid].dif_levels[dif_id].LastGamePoints = current_dif_level.PointNumber
			ProfileInfo[camp_guid].dif_levels[dif_id].LastGD = current_dif_level.LastGD
		end
	end

	for camp_guid in pairs(ProfileInfo) do
		for dif_id in pairs(ProfileInfo[camp_guid].dif_levels) do
			if ProfileInfo[camp_guid].common_info.MaxLevelFinished <  ProfileInfo[camp_guid].dif_levels[dif_id].LevelsFinished then
				ProfileInfo[camp_guid].common_info.MaxLevelFinished = ProfileInfo[camp_guid].dif_levels[dif_id].LevelsFinished
			end
		end
	end
	return ProfileInfo
end

function quit_dialog_script(_owner, _action, _child)
	local app = sf.core.g_Application
	if _child.GetId()==_T("yes") and _action==_T("pressed") then
		_owner.EndModal(1)
	elseif _child.GetId()==_T("no") and _action==_T("pressed") then
		_owner.EndModal(0)
	end
end

