-- день благодарения - 26 ноября, идентификатор - 2
-- рождество 25 декабря, идентификатор - 3
-- хэллоуин 31 октября, идентификатор - 1

-- Возвращает праздник, который должен быть на фоне геймплея

function GetBackgroundHoliday()

	local daysToCr = GetCountDaysFromNowToDate(25, 12)
	local daysToHa = GetCountDaysFromNowToDate(31, 10)
	local daysToTh = GetCountDaysFromNowToDate(26, 11)
	
	local holiday = ""
	local res = ""
	local tankname = ""
	
	local currentTank = PermanentStorage.GetLocalString("Current Tank")
	local tankType    = PermanentStorage.GetLocalString(currentTank.." Tank Type")
	if tankType == "1" then
		tankname = "halloween"
	elseif tankType == "2" then
		tankname = "thanksgiving"
	elseif tankType == "3" then
		tankname = "christmas"
	end
	
	local Version = PermanentStorage.GetGlobalString("temp:Ver")
	if (Version == "Full") then
		if getDate("day") == 25 and getDate("month") == 12 then
			holiday =  "christmas"
		elseif getDate("day") == 31 and getDate("month") == 10 then
			holiday = "halloween"
		elseif getDate("day") == 26 and getDate("month") == 11 then
			holiday = "thanksgiving"
		end
		
		if not (holiday == "") then
			res = holiday			
		else
			res = tankname
			if daysToCr < 20 then
				if random(1, 5) == 2 then	
					res = "christmas" 
				end
			elseif daysToHa < 20 then
				if random(1, 5) == 2 then	
					res = "halloween" 
				end
			elseif (daysToTh < 20) then
				if random(1, 5) == 2 then	
					res = "thanksgiving" 
				end
			end
		end
	else
		res = tankname
	end
	
	PermanentStorage.SetLocalString("currentgamebackground", res)
end	

-- сообщение что в одном из аквариумов нас ждет открытка
function ReceiveACard(message)
	if message == "hide" then
		SendMessage("GreetingCardReceiver", "hide")
		SendMessage("Greeting Card dialog", "hide")
		if not PermanentStorage.GetLocalBoolOrDef("[want tutorial] shown", false) then
			TutorialFunc("Enter main screen")
		end
		return
	elseif message == "destroy" then
		SendMessage("Screen", "erase top child")
		return
	end
	local holiday = PermanentStorage.GetGlobalString("temp:Ver")
	if (holiday == "Full") then
		
		if string.sub(message, 1, 13) == "Enter to tank" then

			local newTank = string.sub(message, 15)		

			PermanentStorage.SetLocalString("Current Tank", newTank)
			SendMessage("Aquarium bottom", "Change Aquarium")
			SendMessage("CupRenderElement", "Reset")
			SendMessage("Screen", "clear children list")
			SendMessage("Screen", "template CardEnterEffectTemplate")
			SendMessage("Screen", "create top instance")
		elseif not PermanentStorage.GetLocalBoolOrDef("MessageCardReceive", false) then
			Instantiate("Screen", "GreetingCardDialogTemplate")
			SendMessage("Greeting Card dialog", "template ReceiveACardTemplate")
			local idUser = PermanentStorage.GetLocalInt("_Id")
			local tank_texture = Application.getDataFolder() .. "textures/choose_tank/tanks/"
			if message == "christmas" then
				SendMessage("Greeting Card dialog", "defineText [GreetingCardText] textchristmasCard")
				if isFileExist(tank_texture..idUser.."_tank_3.jpg") then
					tank_texture = tank_texture..idUser.."_tank_3.jpg"
				else
					tank_texture = "textures/choose_tank/tanks/default3.jpg"
				end
				SendMessage("Greeting Card dialog", "defineText [tank_texture] "..tank_texture)
				SendMessage("Greeting Card dialog", "defineText [number] 3")
			elseif message == "thanksgiving" then
				SendMessage("Greeting Card dialog", "defineText [GreetingCardText] textthanksgivingCard")
				if isFileExist(tank_texture..idUser.."_tank_2.jpg") then
					tank_texture = tank_texture..idUser.."_tank_2.jpg"
				else
					tank_texture = "textures/choose_tank/tanks/default2.jpg"
				end
				SendMessage("Greeting Card dialog", "defineText [tank_texture] "..tank_texture)
				SendMessage("Greeting Card dialog", "defineText [number] 2")
			elseif message == "helloween" then
				SendMessage("Greeting Card dialog", "defineText [GreetingCardText] texthelloweenCard")
				if isFileExist(tank_texture..idUser.."_tank_1.jpg") then
					tank_texture = tank_texture..idUser.."_tank_1.jpg"
				else
					tank_texture = "textures/choose_tank/tanks/default1.jpg"
				end
				SendMessage("Greeting Card dialog", "defineText [tank_texture] "..tank_texture)
				SendMessage("Greeting Card dialog", "defineText [number] 1")
			else 
				assert(false)
			end
			
			SendMessage("Greeting Card dialog", "create instance")
			SendMessage("ReceiveACardReceiver", "show")
			PermanentStorage.SetLocalBool("MessageCardReceive", true)
			return true
		end
	else
		return false
	end
end

-- возвращает не наступил ли праздник, если наступил, то пишет в сторедж 
-- необходимые данные и что то выводит либо поздравительную карточку либо
-- сообзение что в одном из аквариумов нас эта карточка ждет
-- день благодарения - 26 ноября, идентификатор - 2
-- рождество 25 декабря, идентификатор - 3
-- хэллоуин 31 октября, идентификатор - 1

function HolidayComing()

	local currentTank = PermanentStorage.GetLocalString("Current Tank")
	local tankType    = PermanentStorage.GetLocalString(currentTank.." Tank Type")
	local holiday = PermanentStorage.GetGlobalString("temp:Ver")	
	if getDate("day") == 26 and getDate("month") == 11 then
		if not PermanentStorage.GetLocalBoolOrDef("ThanksgivingCardReceive", false) then
			if not (tankType == "2") then
				return ReceiveACard("thanksgiving")
			else
				PermanentStorage.SetLocalBool("MessageCardReceive", true)
				Instantiate("Screen", "GreetingCardDialogTemplate")
				SendMessage("Greeting Card dialog", "instantiate GreetingCardThanksgivingTemplate")
				SendMessage("GreetingCardReceiver", "show")
				PermanentStorage.SetLocalBool("ThanksgivingCardReceive", true)
			end
			return true
		end
	elseif getDate("day") == 25 and getDate("month") == 12 then
		if not PermanentStorage.GetLocalBoolOrDef("CrismasCardReceive", false) then
			if not (tankType == "3") then
				return ReceiveACard("christmas")
			else
				PermanentStorage.SetLocalBool("MessageCardReceive", true)
				Instantiate("Screen", "GreetingCardDialogTemplate")
				SendMessage("Greeting Card dialog", "instantiate GreetingCardChristmasTemplate")
				SendMessage("GreetingCardReceiver", "show")
				PermanentStorage.SetLocalBool("CrismasCardReceive", true)
			end
			return true
		end
	elseif getDate("day") == 31 and getDate("month") == 10 then
		if not PermanentStorage.GetLocalBoolOrDef("HelloweenCardReceive", false) then
			if not (tankType == "1") then
				return ReceiveACard("helloween")
			else
				PermanentStorage.SetLocalBool("MessageCardReceive", true)
				Instantiate("Screen", "GreetingCardDialogTemplate")
				SendMessage("Greeting Card dialog", "instantiate GreetingCardHelloweenTemplate")
				SendMessage("GreetingCardReceiver", "show")
				PermanentStorage.SetLocalBool("HelloweenCardReceive", true)
			end
			return true
		end
	else
		PermanentStorage.SetLocalBool("HelloweenCardReceive", false)
		PermanentStorage.SetLocalBool("CrismasCardReceive", false)
		PermanentStorage.SetLocalBool("ThanksgivingCardReceive", false)
		PermanentStorage.SetLocalBool("MessageCardReceive", false)
	end
	return false
end