-- 
-- FarmingTablet - Appstore
-- 
-- @Interface: 1.4.4.0 1.4.4RC8
-- @Author: kevink98 
-- @Date: 10.06.2017
-- @Version: 1.0.0
-- 
-- @Support: http://ls-modcompany.com
-- @mCompanyInfo: http://mcompany-info.de/
-- 

FarmingTablet_Appstore = {};
FarmingTablet_Appstore.dir = g_currentModDirectory;
addModEventListener(FarmingTablet_Appstore);
g_farmingTablet.appStore = FarmingTablet_Appstore;

FarmingTablet_Appstore.name = "appStore";
FarmingTablet_Appstore.version = "1.0.0";
print("   [FarmingTablet - Appstore] load " .. FarmingTablet_Appstore.version);

function FarmingTablet_Appstore:loadMap()
	FarmingTablet.utils.loadLanguageText(FarmingTablet_Appstore.name, FarmingTablet_Appstore.dir.."appStore/languageXML.xml");	
	local appData = {}; 
	appData.name = FarmingTablet_Appstore.name;
	appData.nameOfApp = g_farmingTablet.utils.getTextByAppName(FarmingTablet_Appstore.name,"nameAppstore")
	appData.isBuyable = false;
	appData.appCategoryDefault = 1;	
	appData.startWindow = "AppstoreView";
	appData.author = "kevink98";
	appData.version = "1.0.0";
	appData.priceForAppstore = 0;
	appData.iconImage = createImageOverlay(FarmingTablet_Appstore.dir.."appStore/appstore.dds");	
	appData.modDir = FarmingTablet_Appstore.dir;
	appData.useSettings = false;
	g_farmingTablet.modApp.newApp(appData);
	
	g_farmingTablet.utils.addNew_Category_Window(g_farmingTablet.appStore, FarmingTablet_Appstore.name, "AppstoreView", "Appstore_PAGE_View", true,true,true)
	g_farmingTablet.utils.loadOverlay(FarmingTablet_Appstore.name, FarmingTablet_Appstore.dir , "appStore/overlays.xml")
	g_farmingTablet.utils.loadButtons(FarmingTablet_Appstore.name, FarmingTablet_Appstore.dir .. "appStore/buttons.xml", g_farmingTablet.appStore)
	
	g_farmingTablet.appStore.autoUpdateApps = false;
	
	g_farmingTablet.appStore.view = {};
	g_farmingTablet.appStore.view.apps = {};
	g_farmingTablet.appStore.activeIndex = 1;
	
	g_farmingTablet.appStore.view.pos = {};
	g_farmingTablet.appStore.view.pos[1] = {};
	g_farmingTablet.appStore.view.pos[1].background = {0.085,0.507,0.102,0.197};
	g_farmingTablet.appStore.view.pos[1].storePic = {0.105,0.58,0.06,0.106};
	g_farmingTablet.appStore.view.pos[1].price = {0.28, 0.555, 0.021};
	g_farmingTablet.appStore.view.pos[2] = {};
	g_farmingTablet.appStore.view.pos[2].background = {0.221,0.507,0.102,0.197};
	g_farmingTablet.appStore.view.pos[2].storePic = {0.241,0.58,0.06,0.106};
	g_farmingTablet.appStore.view.pos[2].price = {0.28, 0.555, 0.021};
	g_farmingTablet.appStore.view.pos[3] = {};
	g_farmingTablet.appStore.view.pos[3].background = {0.357,0.507,0.102,0.197};
	g_farmingTablet.appStore.view.pos[3].storePic = {0.377,0.58,0.06,0.106};
	g_farmingTablet.appStore.view.pos[3].price = {0.28, 0.555, 0.021};
	g_farmingTablet.appStore.view.pos[4] = {};
	g_farmingTablet.appStore.view.pos[4].background = {0.493,0.507,0.102,0.197};
	g_farmingTablet.appStore.view.pos[4].storePic = {0.513,0.58,0.06,0.106};
	g_farmingTablet.appStore.view.pos[4].price = {0.28, 0.555, 0.021};
	
	g_farmingTablet.appStore.view.pos[5] = {};
	g_farmingTablet.appStore.view.pos[5].background = {0.085,0.257,0.102,0.197};
	g_farmingTablet.appStore.view.pos[5].storePic = {0.105,0.33,0.06,0.106};
	g_farmingTablet.appStore.view.pos[5].price = {0.28, 0.308, 0.021};
	g_farmingTablet.appStore.view.pos[6] = {};
	g_farmingTablet.appStore.view.pos[6].background = {0.221,0.257,0.102,0.197};
	g_farmingTablet.appStore.view.pos[6].storePic = {0.241,0.33,0.06,0.106};
	g_farmingTablet.appStore.view.pos[6].price = {0.28, 0.308, 0.021};
	g_farmingTablet.appStore.view.pos[7] = {};
	g_farmingTablet.appStore.view.pos[7].background = {0.357,0.257,0.102,0.197};
	g_farmingTablet.appStore.view.pos[7].storePic = {0.377,0.33,0.06,0.106};
	g_farmingTablet.appStore.view.pos[7].price = {0.28, 0.308, 0.021};
	g_farmingTablet.appStore.view.pos[8] = {};
	g_farmingTablet.appStore.view.pos[8].background = {0.493,0.257,0.102,0.197};
	g_farmingTablet.appStore.view.pos[8].storePic = {0.513,0.33,0.06,0.106};
	g_farmingTablet.appStore.view.pos[8].price = {0.28, 0.308, 0.021};
	
	local saveApp = function(...) return g_farmingTablet.appStore:saveApp(...) end;
	local loadApp = function(...) return g_farmingTablet.appStore:loadApp(...) end;
	FarmingTablet:saveClientApp(saveApp);
	FarmingTablet:loadClientApp(loadApp);
end;

function FarmingTablet_Appstore:update() end;

function FarmingTablet_Appstore:saveApp(xml, key)
	key = key .. ".Appstore";
	local i = 0;
	for name, app in pairs(g_farmingTablet.apps) do
		local key_App = key .. string.format(".app(%d)", i);
		setXMLString(xml, key_App .. "#name", name);
		setXMLBool(xml, key_App .. "#isBought", app.isBought);
		setXMLInt(xml, key_App .. "#category", app.appCategoryDefault);
		if FarmingTablet.apps_useSettings[app.id] then		
			setXMLInt(xml, key_App .. "#activeBackground", app.backgroundActiveIndex);
			key_App = key_App .. ".settings";
			setXMLBool(xml, key_App .. "#useOwnTextColor", app.settings.useOwnTextColor);
			setXMLFloat(xml, key_App .. "#r", app.settings.r);
			setXMLFloat(xml, key_App .. "#g", app.settings.g);
			setXMLFloat(xml, key_App .. "#b", app.settings.b);
			setXMLFloat(xml, key_App .. "#a", app.settings.a);
		end;
		i = i + 1;
	end;	
end;

function FarmingTablet_Appstore:loadApp(xml, key)
	key = key .. ".Appstore";
	local i = 0;
	while true do
		local key_App = key .. string.format(".app(%d)", i);
		if not hasXMLProperty(xml, key_App) then
			break;
		end;
		local name = getXMLString(xml, key_App .. "#name");
		local isBought = getXMLBool(xml, key_App .. "#isBought");
		local appCategoryDefault = getXMLInt(xml, key_App .. "#category");
		
		if g_farmingTablet.apps[name] ~= nil then
			if FarmingTablet.apps_useSettings[g_farmingTablet.apps[name].id] then	
				local activeBackground = getXMLInt(xml, key_App .. "#activeBackground");	
				key_App = key_App .. ".settings";
				local settings_useOwnTextColor = Utils.getNoNil(getXMLBool(xml, key_App .. "#useOwnTextColor"),false);
				local settings_r = Utils.getNoNil(getXMLFloat(xml, key_App .. "#r"),0);
				local settings_g = Utils.getNoNil(getXMLFloat(xml, key_App .. "#g"),0);
				local settings_b = Utils.getNoNil(getXMLFloat(xml, key_App .. "#b"),0);
				local settings_a = Utils.getNoNil(getXMLFloat(xml, key_App .. "#a"),1);
				g_farmingTablet.apps[name].backgroundActiveIndex = Utils.getNoNil(activeBackground, g_farmingTablet.apps[name].backgroundActiveIndex);
				g_farmingTablet.apps[name].settings.useOwnTextColor = settings_useOwnTextColor;
				g_farmingTablet.apps[name].settings.r = settings_r;
				g_farmingTablet.apps[name].settings.g = settings_g;
				g_farmingTablet.apps[name].settings.b = settings_b;
				g_farmingTablet.apps[name].settings.a = settings_a;
			end;
			g_farmingTablet.apps[name].isBought = isBought;
			g_farmingTablet.apps[name].appCategoryDefault = Utils.getNoNil(appCategoryDefault, Utils.getNoNil(g_farmingTablet.apps[name].appCategoryDefault, 3));
			if isBought and name ~= "appStore" then
				g_farmingTablet.addAppToHome(g_farmingTablet.apps[name].appCategoryDefault,name)
				g_farmingTablet.appStore.removeApp(name)
			end;
		end;
		i = i + 1;
	end;
end;

function FarmingTablet_Appstore:draw_AppstoreView() 
	renderOverlay(FarmingTablet.button.getOverlayByModus_name("image", "background").id, 0.008,0.0163,1,0.964);
	for i=1, math.min(table.getn(g_farmingTablet.appStore.view.apps), 8) do
		placeApp = i;
		g_farmingTablet.appStore:drawPlacecView(i, placeApp);	
		g_farmingTablet.button.getButtonByApp("appStore", "view" .. tostring(i)).visible = true;
		g_farmingTablet.button.getButtonByApp("appStore", "view" .. tostring(i) .. "_2").visible = true;
		g_farmingTablet.appStore:drawButtonText(g_farmingTablet.button.getButtonByApp("appStore", "view" .. tostring(i)), g_farmingTablet.appStore.view.apps[placeApp].nameAppstoreButton);
	end;
	
	g_farmingTablet.button.drawButtonByCategory("Appstore_PAGE_View");	
	
	if table.getn(g_farmingTablet.appStore.view.apps) > 0 then
		if not g_farmingTablet.button.getButtonByApp("appStore", "viewBuyApp").visible then
			g_farmingTablet.button.getButtonByApp("appStore", "viewBuyApp").visible = true;
		end;
		g_farmingTablet.appStore:drawButtonText(g_farmingTablet.button.getButtonByApp("appStore", "viewBuyApp"), g_farmingTablet.utils.getTextByAppName(FarmingTablet_Appstore.name,"Appstore_buy"), 0.02);
		
		renderOverlay(FarmingTablet.button.getOverlayByModus_name("image", "appRahmen").id,g_farmingTablet.appStore.view.pos[g_farmingTablet.appStore.activeIndex].background[1],g_farmingTablet.appStore.view.pos[g_farmingTablet.appStore.activeIndex].background[2],g_farmingTablet.appStore.view.pos[g_farmingTablet.appStore.activeIndex].background[3],g_farmingTablet.appStore.view.pos[g_farmingTablet.appStore.activeIndex].background[4]);
	
		local app = g_farmingTablet.appStore.view.apps[g_farmingTablet.appStore.activeIndex]
		setTextBold(true);
		setTextColor(1,1,1,1);
		renderText(0.661, 0.666, 0.028, tostring(app.nameOfApp));
		renderText(0.745, 0.62, 0.022, g_farmingTablet.utils.getTextByAppName(FarmingTablet_Appstore.name,"Appstore_Category"));
		renderText(0.745, 0.56, 0.022, g_farmingTablet.utils.getTextByAppName(FarmingTablet_Appstore.name,"Appstore_Author"));
		renderText(0.745, 0.5, 0.022, g_farmingTablet.utils.getTextByAppName(FarmingTablet_Appstore.name,"Appstore_Version"));
		setTextBold(false);
		renderOverlay(app.iconImage,0.661, 0.5155, 0.074, 0.1315);
		renderText(0.748, 0.595, 0.022, tostring(g_farmingTablet.categorys[app.appCategoryDefault]));
		renderText(0.748, 0.535, 0.022, app.author);
		renderText(0.748, 0.475, 0.022, app.version);
		
		renderText(0.661, 0.4, 0.018, app.descLine1);
		renderText(0.661, 0.37, 0.018, app.descLine2);
		renderText(0.661, 0.34, 0.018, app.descLine3);
	else
		g_farmingTablet.button.getButtonByApp("appStore", "viewBuyApp").visible = false;
	end;				
end;

function FarmingTablet_Appstore:drawPlacecView(pos, placeApp)
	local storePic = g_farmingTablet.appStore.view.apps[placeApp].iconImage;
	local header = g_farmingTablet.appStore.view.apps[placeApp].nameOfApp;
	local price = g_farmingTablet.appStore.view.apps[placeApp].priceForAppstore .." €";
	
	renderOverlay(FarmingTablet.button.getOverlayByModus_name("image", "appHintergrund").id,g_farmingTablet.appStore.view.pos[pos].background[1],g_farmingTablet.appStore.view.pos[pos].background[2],g_farmingTablet.appStore.view.pos[pos].background[3],g_farmingTablet.appStore.view.pos[pos].background[4]);
	renderOverlay(storePic,g_farmingTablet.appStore.view.pos[pos].storePic[1],g_farmingTablet.appStore.view.pos[pos].storePic[2],g_farmingTablet.appStore.view.pos[pos].storePic[3],g_farmingTablet.appStore.view.pos[pos].storePic[4]);
	
	local x = (g_farmingTablet.appStore.view.pos[pos].background[1] + (g_farmingTablet.appStore.view.pos[pos].background[3] / 2)) - (getTextWidth(g_farmingTablet.appStore.view.pos[pos].price[3], tostring(price)) / 2);
	setTextColor(0.3843,0.69,0.1176,1);
	renderText(x,g_farmingTablet.appStore.view.pos[pos].price[2],g_farmingTablet.appStore.view.pos[pos].price[3], tostring(price)); 		
	setTextColor(0,0,0,1);
end;

function FarmingTablet_Appstore:drawButtonText(button, text, height, factor)
	if button.visible then
		if button.hover then
			setTextColor(0.408,0.412,0.408,1);
		else
			setTextColor(0.3843,0.69,0.1176,1);
		end;
		if height == nil then
			height = 0.02;
		end;
		if factor == nil then
			factor = 1.5;
		end;
		local x = button.posXL + ((button.posXR-button.posXL)/2)-(getTextWidth(height ,text) /2);
		local y = button.posYU +(getTextHeight(height ,text) / factor);
		renderText(x,y, height, text);
		setTextColor(0,0,0,1);
	end;
end;

function FarmingTablet_Appstore.addAppToStore(name)
	table.insert(g_farmingTablet.appStore.view.apps, g_farmingTablet.apps[name])	
end;

function FarmingTablet_Appstore:onActiveClick_view(button, index)
	g_farmingTablet.appStore.activeIndex = index;
end;

function FarmingTablet_Appstore:onActiveClick_viewBuyApp()
	local app = g_farmingTablet.appStore.view.apps[g_farmingTablet.appStore.activeIndex]
	--if g_currentMission.missionStats.money >= app.priceForAppstore then
		--g_currentMission.missionStats.money = g_currentMission.missionStats.money - app.priceForAppstore
		--g_currentMission.missionStats:changeBalance(-app.priceForAppstore);	
		if g_server ~= nil then
			g_currentMission:addSharedMoney(-app.priceForAppstore, "other")
		else
			g_client:getServerConnection():sendEvent(MoneyEvent:new(app.priceForAppstore));
		end;
		app.isBought = true;
		FarmingTablet.addAppToHome(app.appCategoryDefault,app.name)
		table.remove(g_farmingTablet.appStore.view.apps, g_farmingTablet.appStore.activeIndex);
		g_farmingTablet.appStore.activeIndex = g_farmingTablet.appStore.activeIndex - 1;
		if g_farmingTablet.appStore.activeIndex < 1 then
			g_farmingTablet.appStore.activeIndex = 1;
		end;
		for i=1, 8 do
			g_farmingTablet.button.getButtonByApp("appStore", "view" .. tostring(i)).visible = false;
			g_farmingTablet.button.getButtonByApp("appStore", "view" .. tostring(i).. "_2").visible = false;		 
		end;
	--end;
end;

function FarmingTablet_Appstore.removeApp(name)
	for k, app in pairs(g_farmingTablet.appStore.view.apps) do
		if app.name == name then
			table.remove(g_farmingTablet.appStore.view.apps, k);
			break;
		end;
	end;
end;

function FarmingTablet_Appstore:mouseEvent(posX, posY, isDown, isUp, button) end;
function FarmingTablet_Appstore:keyEvent(unicode, sym, modifier, isDown) end;
function FarmingTablet_Appstore:draw() end;
function FarmingTablet_Appstore:delete() end;
function FarmingTablet_Appstore:deleteMap() end;

MoneyEvent = {};
MoneyEvent_mt = Class(MoneyEvent, Event);

InitEventClass(MoneyEvent, "MoneyEvent");

function MoneyEvent:emptyNew()
    local self = Event:new(MoneyEvent_mt);
    return self;
end;

function MoneyEvent:new(price)
    local self = MoneyEvent:emptyNew()
    self.price = price;
    return self;
end;

function MoneyEvent:readStream(streamId, connection)
    self.price = streamReadFloat32(streamId);
    self:run(connection);
end;

function MoneyEvent:writeStream(streamId, connection)
    streamWriteFloat32(streamId, self.price);
end;

function MoneyEvent:run(connection)
    g_currentMission:addSharedMoney(-self.price, "other")
end; 