function MoveSpawnScreenPanels(where)
	SendMessage("Main panel mover", "move to "..where)
	SendMessage("Skip panel mover", "move to "..where)
end

function SpawnScreenWelcomeFunc()
	OkDialog("Spawn screen modal dialog", 220, "BonusLevel", "Spawn screen modal dialog", "hide")
end

function GetTankType()
	local tankId = PermanentStorage.GetLocalString("Current Tank")
	if tonumber(tankId) > 4 then
		tankId = PermanentStorage.GetLocalString(tankId.." Tank Type")
	end
	return tankId;
end

function SpawnScreenFunc(message)
	---------------------------------------------------------------------------
	-- spawn screen entered
	---------------------------------------------------------------------------
	if message == "Enter spawn screen" then
		SendMessage("Screen", "clear children list")
		SendMessage("Screen", "template SpawnScreenTemplate")
		SendMessage("Screen", "create top instance")
		PermanentStorage.SetLocalBool("temp:Launching spawn game", false)
		local tankId = GetTankType();
		local enterCount = PermanentStorage.GetLocalIntOrDef(tankId.." tank spawn game enter count", 0)
		PermanentStorage.SetLocalInt(tankId.." tank spawn game enter count", enterCount + 1)
		SendMessage("Aquarium bottom", "spawn roe begin")
		SendMessage("TimedSMS", "lua:MoveSpawnScreenPanels, in, 0.5")
		SendMessage("TimedSMS", "lua:SpawnScreenWelcomeFunc, null, 0.5")
		local retained = tonumber(SendMessageReply("Aquarium bottom", "get diamonds count"))
		PermanentStorage.SetLocalInt("temp:Roe retained", retained)
		spawnGameSuccessful = false
	
	---------------------------------------------------------------------------
	-- skip button pressed
	---------------------------------------------------------------------------
	elseif message == "Skip button pressed" then
		YesNoDialog("Spawn screen modal dialog", "SkipBonusLevel"
			, "lua:SpawnScreenFunc", "skip confirm yes pressed", "skip confirm no pressed")
	elseif message == "skip confirm yes pressed" then
		SendMessage("Spawn screen modal dialog", "hide")
		SendMessage("TimedSMS", "lua:SpawnScreenFunc, Skip button pressed continue, 0.5")
	elseif message == "skip confirm no pressed" then
		SendMessage("Spawn screen modal dialog", "hide")
	elseif message == "Skip button pressed continue" then
		local skipCount = PermanentStorage.GetLocalIntOrDef("Spawn skip count", 0)
		PermanentStorage.SetLocalInt("Spawn skip count", 1)
		if skipCount > 0 then
			YesNoDialog("Spawn screen modal dialog", "DisableBonusLevel"
				, "lua:SpawnScreenFunc", "turn off yes pressed", "Exit spawn screen")
		else
			SpawnScreenFunc("Exit spawn screen")
		end
	elseif message == "turn off yes pressed" then
		local currentTank = PermanentStorage.GetLocalString("Current Tank")
		PermanentStorage.SetLocalBool(currentTank.." Spawn game active", false)
		PermanentStorage.SetLocalInt("Spawn skip count", 2)
		SpawnScreenFunc("Exit spawn screen")
	
	---------------------------------------------------------------------------
	-- exit spawn screen
	---------------------------------------------------------------------------
	elseif message == "Exit spawn screen" then
		PermanentStorage.SetLocalBool("temp:Start spawn game", false)
		Instantiate("Screen", "ZGlassTemplate")
		SendMessage("Spawn screen modal dialog", "hide")
		MoveSpawnScreenPanels("out")
		SendMessage("Screen", "template DarkeningTemplate")
		SendMessage("Screen", "defineText [receiver] post:lua:SpawnScreenFunc")
		SendMessage("Screen", "defineText [message] Exit spawn screen continue")
		SendMessage("Screen", "create top instance")
	elseif message == "Exit spawn screen continue" then
		SendMessage("Aquarium bottom", "spawn roe end")
		if PermanentStorage.GetLocalStringOrDef("temp:Current mode", "menu") == "menu" then
			SendMessage("lua:MainScreenFunc", "Enter main screen")
		else
			SendMessage("lua:ShopFunc", "Enter shop")
			MoveMainScreenPanels("in")			
			SendMessage("Cup alpha changer", "change alpha to 1.0")
		end
		SendMessage("Screen", "template BrighteningTemplate")
		SendMessage("Screen", "create top instance")
		if spawnGameSuccessful == true then
			spawnGameSuccessful = false
			if BonusFishId ~= "none" then
				SendMessage("TimedSMS", "Aquarium bottom, present new fish add "..BonusFishId..", 1.0")
				SendMessage("TimedSMS", "lua:SpawnScreenFunc, play sound fish_buy, 1.0")
			end
		end
	elseif message == "play sound fish_buy" then
		Sound:PlaySample("fish_buy")
	
	---------------------------------------------------------------------------
	-- roe removed event
	---------------------------------------------------------------------------
	elseif message == "roe removed" then
		local retained = tonumber(SendMessageReply("Aquarium bottom", "get diamonds count"))
		PermanentStorage.SetLocalInt("temp:Roe retained", retained)
		if retained == 0 then
			spawnGameSuccessful = true
			Sound:PlaySample("big_cup_showing_bronze")
			SendMessage("TimedSMS", "post:lua:SpawnScreenFunc, roe removed level complite, 1.0")
		end
	elseif message == "roe removed level complite" then
		local variableName = GetTankType().." tank spawn game fish win"
		if PermanentStorage.GetLocalIntOrDef(variableName, 0) < 5 then
			BonusFishId = SendMessageReply("Aquarium bottom", "present new fish search")
			local animId = string.lower(BonusFishId).."_move"
			OkDialogAnimation("Spawn screen modal dialog", 150, animId, "post:lua:SpawnScreenFunc", "roe removed animation ok pressed")
			PermanentStorage.SetLocalInt(variableName, PermanentStorage.GetLocalInt(variableName) + 1)
		else
			BonusFishId = "none";
			Ok500Dialog("Spawn screen modal dialog", 150, "post:lua:SpawnScreenFunc", "roe removed animation ok pressed")
			PermanentStorage.SetLocalInt("Money", PermanentStorage.GetLocalInt("Money") + 500)
		end
	elseif message == "roe removed animation ok pressed" then
		SendMessage("Spawn screen modal dialog", "hide")
		SendMessage("TimedSMS", "lua:SpawnScreenFunc, Exit spawn screen, 0.5")
	end
end
