--
-- Mod: FS19_RealLifeNumbers by Kaj-Aage Henneberg "Ki", ArmChairFarming
-- Script: RealNumbersFieldInfo by Kaj-Aage Henneberg "Ki", ArmChairFarming
-- e-mail: kaj-aage.henneberg@outlook.dk
-- Download: www.modhoster.com
-- All rights reserved.
-- Version 1.1.4.0
-- 30.11.2018 - 29.06.2019

-- Names for FillTypes

-- WHEAT BARLEY OAT CANOLA SUNFLOWER SOYBEAN MAIZE POTATO SUGARBEET OILSEEDRADISH
-- MANURE SEEDS FORAGE FORAGE_MIXING CHAFF WOODCHIPS SILAGE
-- STRAW GRASS_WINDROW DRYGRASS_WINDROW SUGARCANE FERTILIZER PIGFOOD LIME
-- WOOL TREESAPLINGS EGG
-- MILK FUEL WATER DIESEL DEF
-- LIQUIDFERTILIZER HERBICIDE
-- CHAFF GRASS_WINDROW
-- LIQUIDMANURE DIGESTATE
-- COW_TYPE_BRAHMAN_GREY COW_TYPE_BRAHMAN_LIGHT_BROWN COW_TYPE_BRAHMAN_WHITE COW_TYPE_BRAHMAN_BROWN
-- COW_TYPE_BROWN COW_TYPE_BROWN_WHITE COW_TYPE_BLACK COW_TYPE_BLACK_WHITE
-- SHEEP_TYPE_WHITE SHEEP_TYPE_BROWN SHEEP_TYPE_BLACK_WHITE SHEEP_TYPE_BLACK
-- PIG_TYPE_RED PIG_TYPE_WHITE PIG_TYPE_BLACK_WHITE PIG_TYPE_BLACK
-- CHICKEN_TYPE_BLACK CHICKEN_TYPE_WHITE CHICKEN_TYPE_BROWN CHICKEN_TYPE_ROOSTER

--[[
	Version 1.1
	
	Now handling maps with multifruits. I hope.

Field info

	This script provides information on size, price, ownership and crop on each field on the map.
	This information is needed to plan what fields to buy to meet animal feed needs.

Invocation

	This script needs to be executed when the map is finished loading. For this reason, the mod user
	needs to trigger it by pressing LeftAlt-rf.

Harvest work

	The script also adds up the total area owned to be harvested with various harvesters or headers.
	This information will help decide on the size of harvesters and headers to buy.
	
	The script prints a usability ratio, indicating how much of the farmland has been converted
	to arable land (field area). If you do not plan on plowing a lot of unused farmland, then you
	should buy farmlands with a high usability ratio in order to reduce money wasted on unused
	land.

	All information is printed to the log file.
]]--

RealNumbersFieldInfo = {};

function myPrintFieldInfo(Id, landArea, fieldArea, landPrice, currency, owned, fruitName)
	print(string.format("Field %2.0f: Landarea = %4.1f ha, Fieldarea = %4.1f ha, Price = %7.0f %s, Is owned: %s, Fruittype: %s", Id*1.0, landArea, fieldArea, landPrice*1.0, tostring(currency), tostring(owned), tostring(fruitName)));
end;
function myPrintFieldInfo2(Id, landArea, fieldArea, landPrice, currency, owned, fruitName)
	print(string.format("Field %2.0f: Landarea = %4.1f ha, Fieldarea = %4.1f ha, Price = %7.0f %s, %s, Fruittype: %s", Id*1.0, landArea, fieldArea, landPrice*1.0, currency, tostring(owned), tostring(fruitName)));
end;
function myPrintFieldInfo3(text, number, textWidth, unit)
	print(string.format("%s =  %8.1f %s", text..string.rep(" ",textWidth-string.len(text)),number, unit));
end;
function myPrintFieldInfo4(number, number2)
	print(string.format("Field %2.0f: Usability ratio = %8.4f", number, number2));
end;

function RealNumbersFieldInfo:loadMap()
	print("\nFS19_RealLifeNumbers: RealNumbersFieldInfo active,  press lAlt-rf");
	print("-- Lists fields in field number order\n");
end;

function RealNumbersFieldInfo:keyEvent(unicode, sym, modifier, isDown)
	local currency = " ";
	local isDebug = RN.isDebug;
	local isDebug2 = false;
	local isDebug2 = false;

	
	
    local textWidth = 40;
	local ownedCropHa = {};
	local usabilityRatio = {};
	local Fruits = RN.Fruits;
	local grainCrops = "WHEAT BARLEY OAT CANOLA SOYBEAN";
	local gameMoneyUnit = g_i18n.moneyUnit;
	if gameMoneyUnit == 1 then
        currency = "€";
	elseif gameMoneyUnit == 2 then
        currency = "$";
	elseif gameMoneyUnit == 3 then
        currency = "£";
	end

	ownedCropHa.grain = 0;									--grain crops harvested with grain header
	ownedCropHa.rowgrain = 0;								--grain crop harvested with corn header
	ownedCropHa.potato = 0;									--area harvested with with potato harvester
	ownedCropHa.sugarbeet = 0;								--area harvested with with sugarbeet harvester
	ownedCropHa.grass = 0;									--area mowed with grass mower
	ownedCropHa.cotton = 0;
	ownedCropHa.sugarcane = 0;
	ownedCropHa.farmlandtotal = 0;
	ownedCropHa.fieldstotal = 0;



	if Input.isKeyPressed(Input.KEY_lalt) and Input.isKeyPressed(Input.KEY_r) and Input.isKeyPressed(Input.KEY_f) then

--		if g_currentMission.missionInfo.savegameDirectory ~= nil then


			print("\nLeft Alt-rf pressed\n");
			print("Map                   : "..g_currentMission.missionInfo.map.title.." "..g_currentMission.missionInfo.displayName);
			local money = g_currentMission.missionInfo.money;
			if money ~= nil then print("Money                 : "..money); end;
			local gameLevel = g_currentMission.missionInfo.economicDifficulty;
			if gameLevel ~= nil then
				print("Economic difficulty   : "..gameLevel);
				print("Sell price multiplier : "..g_currentMission.economyManager.PRICE_MULTIPLIER[gameLevel]);
				print("Buy price multiplier  : "..g_currentMission.economyManager.COST_MULTIPLIER[gameLevel]);
			end;
			local currentDay = g_currentMission.environment.currentDay;
			if currentDay ~= nil then print("Current game day      : "..currentDay); end;
			local hour = g_currentMission.environment.currentHour;
			local minute = g_currentMission.environment.currentMinute;
			if hour ~= nil or minute ~= nil then print("Time of day           : "..hour..":"..minute); end;
			print(" ");


			if g_fieldManager.fields ~= nil then
				print("Mean land purchase price = "..g_farmlandManager.pricePerHa.." "..currency.."/ha");
				print("Mean land rent price = "..RN.rentPricePerHa.." "..currency.."/ha");
				for k,field in pairs(g_fieldManager.fields) do
					local farmland = field.farmland;
					local farmlandId = farmland.id;
					local fieldId = field.fieldId;
					local fieldArea = field.fieldArea;
					local farmlandArea = farmland.areaInHa;
					local farmlandPrice = farmland.price;
					local farmlandIsOwned = farmland.isOwned;					
					usabilityRatio[k] = fieldArea/farmlandArea;
					

					local fruitName = "Undefined";
					local fruitTypeIndex = field.fruitType;
					if fruitTypeIndex == nil then
						fruitName = "Undefined";
						if farmlandIsOwned then
							ownedCropHa.farmlandtotal = ownedCropHa.farmlandtotal + farmlandArea;
							ownedCropHa.fieldstotal = ownedCropHa.fieldstotal + fieldArea;
							if RN.fieldStatus[farmlandId] == " For sale" then RN.fieldStatus[farmlandId] = " Owned   "; end;
							if RN.fieldStatus[farmlandId] == " For rent" then RN.fieldStatus[farmlandId] = " Rented  "; end;
						end;
					else
						fruitType = g_fruitTypeManager:getFruitTypeByIndex(fruitTypeIndex);
						fruitName = fruitType.name;
						if farmlandIsOwned then
							ownedCropHa.farmlandtotal = ownedCropHa.farmlandtotal + farmlandArea;
							ownedCropHa.fieldstotal = ownedCropHa.fieldstotal + fieldArea;
							if RN.fieldStatus[farmlandId] == " For sale" then RN.fieldStatus[farmlandId] = " Owned   "; end;
							if RN.fieldStatus[farmlandId] == " For rent" then RN.fieldStatus[farmlandId] = " Rented  "; end;
							local i = 0;
							i,j = string.find(grainCrops,fruitName);
							if i ~= nil then
								ownedCropHa.grain = ownedCropHa.grain + fieldArea;
							elseif (fruitName == "MAIZE") or (fruitName == "Sunflower") then
								ownedCropHa.rowgrain = ownedCropHa.rowgrain + fieldArea;
							elseif fruitName == "POTATO" then
								ownedCropHa.potato = ownedCropHa.potato + fieldArea;
							elseif fruitName == "SUGARBEET" then
								ownedCropHa.sugarbeet = ownedCropHa.sugarbeet + fieldArea;
							elseif fruitName == "GRASS" then
								ownedCropHa.grass = ownedCropHa.grass + fieldArea;
							elseif fruitName == "COTTON" then
								ownedCropHa.cotton = ownedCropHa.cotton + fieldArea;
							elseif fruitName == "SUGARCANE" then
								ownedCropHa.sugarcane = ownedCropHa.sugrcane + fieldArea;
							end;
						end;
					end;
					myPrintFieldInfo2(fieldId, farmlandArea, fieldArea, farmlandPrice, currency, RN.fieldStatus[farmlandId], fruitName);
				end;
			end;
			print("\n---Owned land and crop area---\n")
			myPrintFieldInfo3("Total farmland area owned/rented",ownedCropHa.farmlandtotal,textWidth," Ha");
			myPrintFieldInfo3("Total field area owned/rented",ownedCropHa.fieldstotal,textWidth," Ha");
			myPrintFieldInfo3("Grain crop area",ownedCropHa.grain,textWidth," Ha");
			myPrintFieldInfo3("Row grain crop area",ownedCropHa.rowgrain,textWidth," Ha");
			myPrintFieldInfo3("Potato area",ownedCropHa.potato,textWidth," Ha");
			myPrintFieldInfo3("Sugarbeet area",ownedCropHa.sugarbeet,textWidth," Ha");
			myPrintFieldInfo3("Grass area",ownedCropHa.grass,textWidth," Ha");
			myPrintFieldInfo3("Cotton area",ownedCropHa.cotton,textWidth," Ha");
			myPrintFieldInfo3("Sugarcane area",ownedCropHa.sugarcane,textWidth," Ha\n");

			if g_fieldManager.fields ~= nil then
				for k,field in pairs(g_fieldManager.fields) do
					myPrintFieldInfo4(k, usabilityRatio[k]);
				end;
			end;
--		else
--			if not RN.multiPlayer then print("No save game yet. Save the game, quit and reload"); end;
--		end;
	end;
end;
function RealNumbersFieldInfo:update(dt)
end;
function RealNumbersFieldInfo:draw()
end;
function RealNumbersFieldInfo:deleteMap()
end;
function RealNumbersFieldInfo:mouseEvent(posX, posY, isDown, isUp, button)
end;

addModEventListener(RealNumbersFieldInfo);
