--Name: Appstore
--Author: kevink98
--Date: 03.08.2016
--Version: 1.0

--Versionen: 
-- 				v 1.0: - Release
	
--Keine Änderung ohne meine Erlaubnis!
--No modification without my permission!

Appstore = {};
Appstore_directory = g_currentModDirectory;


print("Appstore-Extension v1.0 loaded! Support on Marhu.net");
--g_currentMission.environment.currentDay

function Appstore:loadAppstore ()
	--g_currentMission.FarmingTablet.Appstore = {};
	--Apps:
	g_currentMission.FarmingTablet.Appstore.allApps = {};
	g_currentMission.FarmingTablet.Appstore.allApps[1] = "Bank";
	g_currentMission.FarmingTablet.Appstore.allApps[2] = "FabrikscriptExtension";
	g_currentMission.FarmingTablet.Appstore.allApps[3] = "SoilExtension";
	g_currentMission.FarmingTablet.Appstore.allApps[4] = "Calendar";
	
	g_currentMission.FarmingTablet.Appstore.infos = {};
	g_currentMission.FarmingTablet.Appstore.infos["Bank"] = Bank:appStore();
	g_currentMission.FarmingTablet.Appstore.infos["Bank"].sektion = 1;
	
	g_currentMission.FarmingTablet.Appstore.infos["FabrikscriptExtension"] = FabrikscriptExtension:appStore ();
	g_currentMission.FarmingTablet.Appstore.infos["FabrikscriptExtension"].sektion = 2;
	
	g_currentMission.FarmingTablet.Appstore.infos["SoilExtension"] = SoilExtension:appStore ();
	g_currentMission.FarmingTablet.Appstore.infos["SoilExtension"].sektion = 3;
	
	g_currentMission.FarmingTablet.Appstore.infos["Calendar"] = Calendar:appStore ();
	g_currentMission.FarmingTablet.Appstore.infos["Calendar"].sektion = 1;
	
	self.lefton = false;
	self.righton = false;
	
	self.site1 = true;
	self.site2 = false;
	
end;

function Appstore:loadXml(xml)
	g_currentMission.FarmingTablet.Appstore.apps = {};
	g_currentMission.FarmingTablet.Appstore.apps[1] = getXMLBool(xml, "FarmingTablet.Appstore.Apps.bank");
	g_currentMission.FarmingTablet.Appstore.apps[2] = getXMLBool(xml, "FarmingTablet.Appstore.Apps.fabrikscriptaddon");
	g_currentMission.FarmingTablet.Appstore.apps[3] = getXMLBool(xml, "FarmingTablet.Appstore.Apps.soilapp");
	g_currentMission.FarmingTablet.Appstore.apps[4] = getXMLBool(xml, "FarmingTablet.Appstore.Apps.kalender");
	
end;

function Appstore:loadXmlFirst()
	g_currentMission.FarmingTablet.Appstore.apps = {};
	g_currentMission.FarmingTablet.Appstore.apps[1] = false;
	g_currentMission.FarmingTablet.Appstore.apps[2] = false;
	g_currentMission.FarmingTablet.Appstore.apps[3] = false;
	g_currentMission.FarmingTablet.Appstore.apps[4] = false;
	
	
end;
function Appstore:saveXml(xml)
	setXMLBool(xml, "FarmingTablet.Appstore.Apps.bank", g_currentMission.FarmingTablet.Appstore.apps[1]);
	setXMLBool(xml, "FarmingTablet.Appstore.Apps.fabrikscriptaddon", g_currentMission.FarmingTablet.Appstore.apps[2]);
	setXMLBool(xml, "FarmingTablet.Appstore.Apps.soilapp", g_currentMission.FarmingTablet.Appstore.apps[3]);
	setXMLBool(xml, "FarmingTablet.Appstore.Apps.kalender", g_currentMission.FarmingTablet.Appstore.apps[4]);
	
end;

function Appstore:StartscreenApp (posX, posY, isDown, button)
	local id = g_currentMission:findUserIdByConnection(connection);
	
	--[[
	local money = g_currentMission.FarmingTablet.Appstore.Kredit1.kredithoehe*-1;
	if g_server ~= nil then
		g_currentMission:addMoney(money, id, "other");
	else
		g_currentMission.missionStats.money = g_currentMission.missionStats.money + money;
		MoneyEvent:sendEvent(g_currentMission.missionStats.money);
	end;
	]]--

	if posX > 0.084 and posX < 0.284 and posY > 0.157 and posY < 0.217 then
		self.lefton = true;
	else
		self.lefton = false;
	end;
	if posX > 0.721 and posX < 0.921 and posY > 0.157 and posY < 0.217 then
		self.righton = true;
	else
		self.righton = false;
	end;
	if posX > 0.084 and posX < 0.284 and posY > 0.157 and posY < 0.217 and isDown and button == 1 then
		if self.site2 then
			self.site1 = true;
			self.site2 = false;
		end;
	end;
	if posX > 0.721 and posX < 0.921 and posY > 0.157 and posY < 0.217 and isDown and button == 1 then
		if self.site1 then
			self.site2 = true;
			self.site1 = false;
		end;
	end;
	
	if posX > 0.8 and posX < 0.9 and posY > 0.586 and posY < 0.662 and isDown and button == 1 then
		if self.site1 then
			local money = g_currentMission.FarmingTablet.Appstore.infos[g_currentMission.FarmingTablet.Appstore.allApps[1]].price*-1;
			if g_currentMission.missionStats.money > money*-1 then
				g_currentMission.FarmingTablet.Appstore.apps[1] = true;
				AppstoreEvent:sendEvent();
				if g_server ~= nil then
					g_currentMission:addMoney(money, id, "other");
				else
					g_currentMission.missionStats.money = g_currentMission.missionStats.money + money;
					MoneyEvent:sendEvent(g_currentMission.missionStats.money);
				end;
			end;
		elseif self.site2 then
			local money = g_currentMission.FarmingTablet.Appstore.infos[g_currentMission.FarmingTablet.Appstore.allApps[4]].price*-1;
			if g_currentMission.missionStats.money > money*-1 then
				g_currentMission.FarmingTablet.Appstore.apps[4] = true;
				AppstoreEvent:sendEvent();
				if g_server ~= nil then
					g_currentMission:addMoney(money, id, "other");
				else
					g_currentMission.missionStats.money = g_currentMission.missionStats.money + money;
					MoneyEvent:sendEvent(g_currentMission.missionStats.money);
				end;
			end;
		end;
	elseif posX > 0.8 and posX < 0.9 and posY > 0.41 and posY < 0.483 and isDown and button == 1 then
		if self.site1 then
			local money = g_currentMission.FarmingTablet.Appstore.infos[g_currentMission.FarmingTablet.Appstore.allApps[2]].price*-1;
			if g_currentMission.missionStats.money > money*-1 then
				g_currentMission.FarmingTablet.Appstore.apps[2] = true;
				AppstoreEvent:sendEvent();
				if g_server ~= nil then
					g_currentMission:addMoney(money, id, "other");
				else
					g_currentMission.missionStats.money = g_currentMission.missionStats.money + money;
					MoneyEvent:sendEvent(g_currentMission.missionStats.money);
				end;
			end;
		end;
	elseif posX > 0.8 and posX < 0.9 and posY > 0.231 and posY < 0.305 and isDown and button == 1 then
		if self.site1 then
			local money = g_currentMission.FarmingTablet.Appstore.infos[g_currentMission.FarmingTablet.Appstore.allApps[3]].price*-1;
			if g_currentMission.missionStats.money > money*-1 then
				g_currentMission.FarmingTablet.Appstore.apps[3] = true;
				AppstoreEvent:sendEvent();
				if g_server ~= nil then
					g_currentMission:addMoney(money, id, "other");
				else
					g_currentMission.missionStats.money = g_currentMission.missionStats.money + money;
					MoneyEvent:sendEvent(g_currentMission.missionStats.money);
				end;
			end;
		end;
	end;
	
end;

function Appstore:updateAppstore (dt) 
	if g_currentMission.environment.currentHour < 10 then
		self.uhr1 = tostring("0"..g_currentMission.environment.currentHour);
	else
		self.uhr1 = tostring(g_currentMission.environment.currentHour);
	end;
	if g_currentMission.environment.currentMinute < 10 then
		self.uhr2 = tostring("0"..g_currentMission.environment.currentMinute);
	else
		self.uhr2 = tostring(g_currentMission.environment.currentMinute);
	end;
end;

function Appstore:drawAppstore ()
	renderOverlay(FarmingTablet.overlay.background3, 0.01, 0.01, 1, 1.5);
	--Allgemein
	setTextColor(1, 1, 1, 1);
	renderText(0.728, 0.833, 0.03, tostring(self.uhr1..":"..self.uhr2));
	if g_currentMission.FarmingTablet.Settings.akkuFuel < 162000 then
		renderOverlay(FarmingTablet.overlay.akkuRed, 0.8, 0.815, 0.05, 0.06);
		renderOverlay(FarmingTablet.overlay.akku1, 0.8, 0.815, 0.05, 0.06);
	elseif g_currentMission.FarmingTablet.Settings.akkuFuel >= 162000 and g_currentMission.FarmingTablet.Settings.akkuFuel < 324000 then
		renderOverlay(FarmingTablet.overlay.akku, 0.8, 0.815, 0.05, 0.06);
		renderOverlay(FarmingTablet.overlay.akku1, 0.8, 0.815, 0.05, 0.06);
		renderOverlay(FarmingTablet.overlay.akku2, 0.8, 0.815, 0.05, 0.06);	
	elseif g_currentMission.FarmingTablet.Settings.akkuFuel >= 324000 and g_currentMission.FarmingTablet.Settings.akkuFuel < 486000 then	
		renderOverlay(FarmingTablet.overlay.akku, 0.8, 0.815, 0.05, 0.06);
		renderOverlay(FarmingTablet.overlay.akku1, 0.8, 0.815, 0.05, 0.06);
		renderOverlay(FarmingTablet.overlay.akku2, 0.8, 0.815, 0.05, 0.06);	
		renderOverlay(FarmingTablet.overlay.akku3, 0.8, 0.815, 0.05, 0.06);	
	elseif g_currentMission.FarmingTablet.Settings.akkuFuel >= 324000 and g_currentMission.FarmingTablet.Settings.akkuFuel < 648000 then	
		renderOverlay(FarmingTablet.overlay.akku, 0.8, 0.815, 0.05, 0.06);
		renderOverlay(FarmingTablet.overlay.akku1, 0.8, 0.815, 0.05, 0.06);
		renderOverlay(FarmingTablet.overlay.akku2, 0.8, 0.815, 0.05, 0.06);	
		renderOverlay(FarmingTablet.overlay.akku3, 0.8, 0.815, 0.05, 0.06);	
		renderOverlay(FarmingTablet.overlay.akku4, 0.8, 0.815, 0.05, 0.06);
	end;
	renderText(0.86, 0.833, 0.03, tostring(string.format("%.f", g_currentMission.FarmingTablet.Settings.akkuFuel/6480))..tostring("%"));
	setTextColor(0, 0, 0, 1);
	renderText(0.1, 0.775, 0.033, tostring("Appstore"));
	renderText(0.1, 0.775, 0.033, tostring("Appstore"));
	--
	if self.lefton then
		renderOverlay(FarmingTablet.overlay.leftOn, 0.084, 0.158, 0.2, 0.06);
	else	
		renderOverlay(FarmingTablet.overlay.left, 0.084, 0.158, 0.2, 0.06);
	end;
	if self.righton then
		renderOverlay(FarmingTablet.overlay.rightOn, 0.721, 0.158, 0.2, 0.06);
	else	
		renderOverlay(FarmingTablet.overlay.right, 0.721, 0.158, 0.2, 0.06);
	end;
	
	local numApps = table.getn(g_currentMission.FarmingTablet.Appstore.allApps);
	
	if self.site1 then
		renderOverlay(FarmingTablet.overlay.pointOn, 0.49, 0.168, 0.02, 0.03);
		renderOverlay(FarmingTablet.overlay.point, 0.52, 0.168, 0.02, 0.03);
		for i = 1, 3 do
			Appstore:drawApp(g_currentMission.FarmingTablet.Appstore.allApps[i], i)
		end;
	elseif self.site2 then
		renderOverlay(FarmingTablet.overlay.point, 0.49, 0.168, 0.02, 0.03);
		renderOverlay(FarmingTablet.overlay.pointOn, 0.52, 0.168, 0.02, 0.03);
		for i = 4, 4 do
			Appstore:drawApp(g_currentMission.FarmingTablet.Appstore.allApps[i], i)
		end;
	end;
	
end;

function Appstore:drawApp(app, n)
	local infos = g_currentMission.FarmingTablet.Appstore.infos[app];
	
	if infos.sektion == 1 then
		renderOverlay(FarmingTablet.overlay.backgroundApp, 0.1, 0.58, 0.81, 0.17);
		renderOverlay(FarmingTablet.overlay.buy, 0.8, 0.587, 0.1, 0.075);
		setTextColor(0, 0, 0, 1);
		renderOverlay(infos.icon, 0.108, 0.587, 0.1, 0.15);
		renderText(0.22, 0.715, 0.029, tostring(infos.titel));
		renderText(0.22, 0.715, 0.029, tostring(infos.titel));
		if infos.version == 1 then
			local version = "1.0";
			renderText(0.4, 0.359, 0.029, tostring("Version: ")..version..tostring("  Script by: "..infos.author));
		else
			local version = tostring(infos.version);
			renderText(0.4, 0.359, 0.029, tostring("Version: ")..version..tostring("  Script by: "..infos.author));
		end;
		renderText(0.22, 0.68, 0.026, tostring("Beschreibung:"));
		renderText(0.22, 0.65, 0.023, tostring(infos.description.z1));
		renderText(0.22, 0.62, 0.023, tostring(infos.description.z2));
		renderText(0.22, 0.59, 0.023, tostring(infos.description.z3));
		
		if g_currentMission.FarmingTablet.Appstore.apps[n] == false then
			local price = tostring(infos.price);
			local length = string.len(price);
			if length > 3 then
			price = string.sub(price, 1, length-3).."."..string.sub(price, -3, length);
			end;
			renderText(0.809, 0.601, 0.039, tostring(price.."€"));
		elseif g_currentMission.FarmingTablet.Appstore.apps[n] then
			renderText(0.803, 0.601, 0.037, tostring("Installiert!"));
		end;		
	end;
	if infos.sektion == 2 then
		renderOverlay(FarmingTablet.overlay.backgroundApp, 0.1, 0.402, 0.81, 0.17);
		renderOverlay(FarmingTablet.overlay.buy, 0.8, 0.409, 0.1, 0.075);
		setTextColor(0, 0, 0, 1);
		renderOverlay(infos.icon, 0.108, 0.409, 0.1, 0.15);
		renderText(0.22, 0.537, 0.029, tostring(infos.titel));
		renderText(0.22, 0.537, 0.029, tostring(infos.titel));
		if infos.version == 1 then
			local version = "1.0";
			renderText(0.4, 0.359, 0.029, tostring("Version: ")..version..tostring("  Script by: "..infos.author));
		else
			local version = tostring(infos.version);
			renderText(0.4, 0.359, 0.029, tostring("Version: ")..version..tostring("  Script by: "..infos.author));
		end;
		renderText(0.22, 0.502, 0.026, tostring("Beschreibung:"));
		renderText(0.22, 0.472, 0.023, tostring(infos.description.z1));
		renderText(0.22, 0.442, 0.023, tostring(infos.description.z2));
		renderText(0.22, 0.412, 0.023, tostring(infos.description.z3));
		
		if g_currentMission.FarmingTablet.Appstore.apps[n] == false then
			local price = tostring(infos.price);
			local length = string.len(price);
			if length > 3 then
			price = string.sub(price, 1, length-3).."."..string.sub(price, -3, length);
			end;
			renderText(0.809, 0.423, 0.039, tostring(price.."€"));
		elseif g_currentMission.FarmingTablet.Appstore.apps[n] then
			renderText(0.803, 0.423, 0.037, tostring("Installiert!"));	
		end;
	
	end;
	if infos.sektion == 3 then
		renderOverlay(FarmingTablet.overlay.backgroundApp, 0.1, 0.227, 0.81, 0.17);
		renderOverlay(FarmingTablet.overlay.buy, 0.8, 0.231, 0.1, 0.075);
		setTextColor(0, 0, 0, 1);
		renderOverlay(infos.icon, 0.108, 0.231, 0.1, 0.15);
		renderText(0.22, 0.359, 0.029, tostring(infos.titel));
		renderText(0.22, 0.359, 0.029, tostring(infos.titel));
		if infos.version == 1 then
			local version = "1.0";
			renderText(0.4, 0.359, 0.029, tostring("Version: ")..version..tostring("  Script by: "..infos.author));
		else
			local version = tostring(infos.version);
			renderText(0.4, 0.359, 0.029, tostring("Version: ")..version..tostring("  Script by: "..infos.author));
		end;
		renderText(0.22, 0.324, 0.026, tostring("Beschreibung:"));
		renderText(0.22, 0.294, 0.023, tostring(infos.description.z1));
		renderText(0.22, 0.264, 0.023, tostring(infos.description.z2));
		renderText(0.22, 0.234, 0.023, tostring(infos.description.z3));
		
		if g_currentMission.FarmingTablet.Appstore.apps[n] == false then
			local price = tostring(infos.price);
			local length = string.len(price);
			if length > 3 then
			price = string.sub(price, 1, length-3).."."..string.sub(price, -3, length);
			end;
			renderText(0.809, 0.254, 0.039, tostring(price.."€"));
		elseif g_currentMission.FarmingTablet.Appstore.apps[n] then
			renderText(0.803, 0.254, 0.037, tostring("Installiert!"));	
		end;
	end;
	
end;


function Appstore:mouseEventAppstore(posX, posY, isDown, isUp, button) end;
function Appstore:deleteAppstore() end;
 
 
 
 
 
 
 