--
-- Mod: FS19_RealLifeNumbers by Kaj-Aage Henneberg "Ki", ArmChairFarming
-- Script: RealNumbersLeasing 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

--[[
	This mod allows the user to define the terms of equipment leasing.

	RN.equipmentLifeYears = 6;				-- Life time of equipment in years
	RN.equipmentAnnualLoss = 0.15;			-- Anunual depreciation fctor
	RN.leaseInitialInstallment = 0.005;		-- Initial extra downpayment factor
	RN.leaseAnnualInterestRate = 0.05;		-- Lease annual interest rate
. 
	The initial installement (0.5%) is set low to fit short term leasing.
	For long term leasing (years) the initial installment should probably
	be higher.
	
	The daily and hourly components of the lease is calculated from the amount
	left when the initial installment and return value have been subtracted
	from the purchase price. The higher the initial installment, the lower the 
	daily and hourly rates.

--]]

RealNumbersLeasing = {};


function RealNumbersLeasing:loadMap(name)
	local currency = "€";	-- or  "$" or "£"
	local textWidth = 40; 	-- text width in log file
	local isDebug = RN.isDebug;  	-- Change to False when not debugging
	local lifeTime = RN.equipmentLifeYears ;
	local annualLoss = RN.equipmentAnnualLoss;
	local intialPayment = RN.leaseInitialInstallment;
	local leaseInterest = RN.leaseAnnualInterestRate;

	local baseValue = 100000;
	local endTimeValue = (1-annualLoss)^lifeTime * baseValue;
	local initialPaymentValue = intialPayment * baseValue;
	local baseLoan = baseValue - (initialPaymentValue + endTimeValue);
	local loanInterest = ((1+leaseInterest)^lifeTime - 1) * baseLoan;
	local totalLoan = baseLoan + loanInterest;
	local installmentPerMonth = 0.5*totalLoan/(12*lifeTime);
	local installmentPerHour = installmentPerMonth/24;
	local leaseFactorPerMonth = installmentPerMonth/baseValue;
	local leaseFactorPerHour = installmentPerHour/baseValue;

	local gameMoneyUnit = g_i18n.monloanInteresteyUnit;
	if gameMoneyUnit == 1 then
        currency = "€";
	elseif gameMoneyUnit == 2 then
        currency = "$";
	elseif gameMoneyUnit == 3 then
        currency = "£";
	end;

	print("\nFS19_RealLifeNumbers: RealNumbersLeasing active\n")



	if isDebug then
		myPrint("Equipment life time",lifeTime,textWidth," years ");
		myPrintFloat("Annual depreciation factor",annualLoss,textWidth," ");
		myPrintFloat("Initial down payment",intialPayment,textWidth, " ");
		myPrintFloat("Loan interest",leaseInterest,textWidth, " ");
		myPrint("Example loan", baseValue, textWidth, currency);
		myPrint("End time value", endTimeValue, textWidth, currency);
		myPrint("Initial down payment", initialPaymentValue, textWidth, currency);
		myPrint("Base loan", baseLoan, textWidth, currency);
		myPrint("Loan interest (accumulated)", loanInterest, textWidth, currency);
		myPrint("Total loan", totalLoan, textWidth, currency);
		myPrint("Monthly payment", installmentPerMonth, textWidth, currency);
		myPrint("Hourly payment", installmentPerHour, textWidth, currency);
		myPrintFloat("Monthly payment factor", leaseFactorPerMonth, textWidth, " ");
		myPrintFloat("Hourly payment factor", leaseFactorPerHour, textWidth, " ");
		print(" ");
	end;
	RN.DEFAULT_RUNNING_LEASING_FACTOR = leaseFactorPerHour; 		-- Lease/hour as fraction of price. Ingame = 0.021.
	RN.PER_DAY_LEASING_FACTOR         = leaseFactorPerMonth;		-- Lease/hour as fraction of price. Ingame default = 0.01
	RN.DEFAULT_LEASING_DEPOSIT_FACTOR = intialPayment;				-- Lease deposit as fraction of price. Ingame default = 0.02
end;



function RealNumbersLeasing:keyEvent(unicode, sym, modifier, isDown)
end;

function RealNumbersLeasing:update(dt)

	if g_storeManager.items ~= nil then
		for k,items in pairs(g_storeManager.items) do
			if items.allowLeasing then
				items.runningLeasingFactor = RN.DEFAULT_RUNNING_LEASING_FACTOR;
			end;
		end;
	end;
	EconomyManager.DEFAULT_RUNNING_LEASING_FACTOR = RN.DEFAULT_RUNNING_LEASING_FACTOR;
	EconomyManager.PER_DAY_LEASING_FACTOR = RN.PER_DAY_LEASING_FACTOR;
	EconomyManager.DEFAULT_LEASING_DEPOSIT_FACTOR = RN.DEFAULT_LEASING_DEPOSIT_FACTOR;

end;
function RealNumbersLeasing:draw()
end;
function RealNumbersLeasing:deleteMap()
end;
function RealNumbersLeasing:mouseEvent(posX, posY, isDown, isUp, button)
end;

addModEventListener(RealNumbersLeasing);
