----------------------------------------------------------------------------
----Author: ViperGTS96------------------------------------------------------
----------------------------------------------------------------------------
--------------------"The simplest design is always the best design." -------
---@v.1.0.0.0---------------------------------------------------------------
----------------------------------------------------------------------------

readMe = {};
--------------------------------------------------------------------------------
readMe.savegameFile = "cowboyHat.xml"; --File to look for in savegame's folder.
local textGapFromTitle = 0.1; -- Space Between Title and Body Text.
--------------------------------------------------------------------------------
readMe.bg = createImageOverlay(g_currentModDirectory.."icons/background.dds");
readMe.icon = createImageOverlay(g_currentModDirectory.."icons/icon.dds");
readMe.OLW = 0.6;
readMe.OLH = readMe.OLW * (11/7);
readMe.OLX = 0.5 - (readMe.OLW / 2);
readMe.OLY = 0.5 - (readMe.OLH / 2);
readMe.iconW = 0.1;
readMe.iconH = readMe.iconW * g_screenAspectRatio;
readMe.iconX = (readMe.OLX + readMe.OLW) - (readMe.iconW + 0.01);
readMe.iconY = (readMe.OLY + readMe.OLH) - (readMe.iconH + 0.01);
readMe.titleSize = 0.04;
readMe.textSize = 0.015;
readMe.titleY = (readMe.OLY + readMe.OLH) - (readMe.titleSize + 0.01);
readMe.textX = readMe.OLX + 0.01;
readMe.textY = readMe.titleY - ((readMe.textSize + 0.01) + textGapFromTitle);
readMe.bottomY = readMe.OLY + ((readMe.titleSize/2) + 0.01);
textGapFromTitle = nil;
--------------------------------------------------------------------------------
readMe.tick = 0;
readMe.delay = 20;
readMe.visible = false;
--------------------------------------------------------------------------------

addModEventListener(readMe);

function readMe:loadMap(savegame)

	local sgFolder = g_currentMission.missionInfo.savegameDirectory;
	if sgFolder == nil then	
		sgFolder = ('%ssavegame%d'):format(getUserProfileAppPath(), g_currentMission.missionInfo.savegameIndex);
	end;
	sgFolder = sgFolder.."/";

	if fileExists(sgFolder..readMe.savegameFile) then --print("Savegame found; 'ReadMe' has been viewed)"); 
	readMe:killMod(); return; end;
	setOverlayColor(readMe.bg, 0.0, 0.0, 0.0, 0.96);
	readMe.title = asciiToUtf8(g_i18n:getText("readMe_title"));
	readMe.text = asciiToUtf8(g_i18n:getText("readMe_mainText"));
	readMe.closeText = asciiToUtf8(g_i18n:getText("readMe_closeText"));

end;


function readMe:killMod()
	removeModEventListener(readMe);
	readMe = nil;
end;

function readMe:update()
	if not readMe.visible then
		if readMe.tick < readMe.delay then
			readMe.tick = readMe.tick + 1;
		else
			readMe.tick = readMe.delay+1;
			readMe.visible = true;
		end;
	else
		if Input.isKeyPressed(Input.KEY_backspace) or Input.isKeyPressed(Input.KEY_return) then
			readMe.visible = false;
			readMe:killMod();
		end;
	end;
end;

function readMe:draw()
	if readMe.visible then
		renderOverlay(readMe.bg, readMe.OLX, readMe.OLY, readMe.OLW, readMe.OLH);	
		renderOverlay(readMe.icon, readMe.iconX, readMe.iconY, readMe.iconW, readMe.iconH);				
		setTextColor(1.0, 1.0, 1.0, 1.0);
		setTextBold(true);
		renderText(readMe.textX, readMe.titleY, readMe.titleSize, readMe.title);
		setTextBold(false);
		renderText(readMe.textX, readMe.textY, readMe.textSize, readMe.text);
		setTextBold(true);
		renderText(readMe.textX, readMe.bottomY, readMe.titleSize/2, readMe.closeText);
	end;
end;
