-- -----------------------------------------------------------------------------
-- SimNetwork
-- balance.lua
-- handling code for balance and overall customer satisfaction
-- -----------------------------------------------------------------------------
SimNetwork_BALANCE = true

if not SimNetwork_GLOBAL	then dofile("global.lua")	end
if not SimNetwork_INPUT	then dofile("input.lua")	end

balanceString = nil				-- displays balance on the screen
custSatisfactionString = nil	-- displays overall customer satisfaction on the screen
timerString = nil
customersLeftString = nil

local custSatisfThreshold = 25		-- minimum threshold for customer satisfaction
									-- if overall cust satisf falls below this, game ends

-- -----------------------------------------------------------------------------
-- Function: create_toolbar()
-- Inputs: none
-- Returns: pivot representing the balance bar
-- Loads balance bar sprites and strings and positions them. Called at initialization
-- -----------------------------------------------------------------------------		
function create_balancebar()

	local theBalancebar = E.create_pivot()
	local width = 0
	
	local xPos = viewport.R
	local yPos = viewport.T
	
	-- load balance bar sprites: 
		
		balance = create_overlay("data/toolbar/balance.png")
		timer = create_overlay("data/toolbar/timer.png")
		customersLeft = create_overlay("data/toolbar/customersleft.png")
		customerSatisfaction = create_overlay("data/toolbar/custsatisfaction.png")

		-- position tool
		local x0, y0, z0, x1, y1, z1 = E.get_entity_bound(balance)
		local toolW = x1 - x0
		local toolH = y1 - y0
		E.set_entity_alpha(balance, 0.7)
		E.set_entity_alpha(timer, 0.7)
		E.set_entity_alpha(customersLeft, 0.7)
	
		E.set_entity_position(balance, xPos-toolW/2+width, yPos-toolH/2,0)
		E.set_entity_position(customersLeft, xPos-toolW/2+width, yPos-toolH*3/2+1,0)
		width = width - toolW
		
		x0,y0,z0,x1,y1,z1 = E.get_entity_bound(customerSatisfaction)
		local toolW = x1 - x0
		local toolH = y1 - y0
		E.set_entity_position(customerSatisfaction, xPos-toolW/2+width, yPos-toolH/2,0)
		E.set_entity_alpha(customerSatisfaction, 0.7)
		width = width - toolW/2
		
		x0,y0,z0,x1,y1,z1 = E.get_entity_bound(timer)
		local toolW = x1 - x0
		local toolH = y1 - y0
		E.set_entity_position(timer, xPos-toolW/2+width, yPos-toolH/2,0)
		
		--create string for balance
		timerString = E.create_string("")
		E.set_string_fill(timerString, blackBrush)
		E.set_entity_position(timerString, xPos - 348, yPos - 22,0)
		E.set_entity_scale(timerString, 12,12,0)
		
		E.set_brush_color(brush,0,0,0,1)
		
		customersLeftString = E.create_string("")
		E.set_string_fill(customersLeftString, blackBrush)
		E.set_entity_position(customersLeftString, xPos - 33, yPos - 52,0)
		E.set_entity_scale(customersLeftString, 13,13,0)
		
		balanceString = E.create_string(INITIAL_CAPITAL)
		E.set_string_fill(balanceString, blackBrush)
		E.set_entity_position(balanceString, xPos - 85, yPos - 26,0)
		E.set_entity_scale(balanceString, 13,13,0)
	
		-- create string for overall customer satisfaction
		custSatisfactionString = E.create_string(100)
		E.set_string_fill(custSatisfactionString, blackBrush)
		
		E.set_entity_position(custSatisfactionString, xPos - 180, yPos - 20,0)
		E.set_entity_scale(custSatisfactionString, 13,13,0)
	
		-- push the strings
		E.parent_entity(customersLeftString, theBalancebar)
		E.parent_entity(timerString, theBalancebar)
		E.parent_entity(balanceString, theBalancebar)
		E.parent_entity(custSatisfactionString, theBalancebar)
	
		-- push the sprites (need to be done after pushing the strings to preserce display order
		E.parent_entity(timer, theBalancebar)
		E.parent_entity(customersLeft, theBalancebar)
		E.parent_entity(balance, theBalancebar)
		E.parent_entity(customerSatisfaction, theBalancebar)
	
	return theBalancebar
end

-- -----------------------------------------------------------------------------
-- Function: update_balance()
-- Inputs: newBalance
-- Returns: none
-- updates the balance with new value and re-draws balance on the screen
-- -----------------------------------------------------------------------------
function update_balance(newBalance)
	if newBalance ~= nil then
		E.set_string_text(balanceString, math.floor(newBalance))
	else
		print("update_balance(): NIL newBalance")
	end
end

-- -----------------------------------------------------------------------------
-- Function: update_timer()
-- Inputs: time
-- Returns: none
-- updates the time string with new value and re-draws it on the screen
-- -----------------------------------------------------------------------------
function update_timer(time)
	
	local xPos = viewport.R
	local yPos = viewport.T
	
	local seconds = math.mod(time, 60)
	local minutes = math.floor(time / 60)
	
	if seconds < 10 then
		seconds = "0" .. seconds 
	end
	
	if minutes < 10 then
		minutes = "0" .. minutes 
	end
	
	local timeString = minutes .. ":" .. seconds
	
	E.set_entity_position(timerString, xPos - 344, yPos - 20,0)
	E.set_string_text(timerString,timeString)
	
end

-- -----------------------------------------------------------------------------
-- Function: update_customersLeft()
-- Inputs: n
-- Returns: none
-- updates the customers left string with new value and re-draws it on the screen
-- -----------------------------------------------------------------------------
function update_customersLeft(n)
	
	if n >= 0 then
		E.set_string_text(customersLeftString,n)
	end
	
end

-- -----------------------------------------------------------------------------
-- Function: add_balance()
-- Inputs: addBalance
-- Returns: none
-- adds to the balance a new value and re-draws balance on the screen
-- -----------------------------------------------------------------------------
function add_balance(addBalance)
	profitBalance = profitBalance + addBalance
	E.set_string_text(balanceString,math.floor(profitBalance))
end

-- -----------------------------------------------------------------------------
-- Function: sub_balance()
-- Inputs: subBalance
-- Returns: none
-- substracts from the balance a new value and re-draws balance on the screen
-- -----------------------------------------------------------------------------
function sub_balance(subBalance)
	profitBalance = profitBalance - subBalance
	E.set_string_text(balanceString,math.floor(profitBalance))
	
	--if balance becomes negative, end the game
	if profitBalance < 0 then
		gameEnd = true
	end
	
end

-- -----------------------------------------------------------------------------
-- Function: update_overallCustSatisfaction()
-- Inputs: newOverallCustSatisfaction
-- Returns: none
-- updates the overall customer satisfaction with new value and re-draws it on the screen
-- -----------------------------------------------------------------------------
function update_overallCustSatisfaction(satRate)
	if satRate == nil then
		print("update_overallCutSat(): NIL satRate")
	else
		E.set_string_text(custSatisfactionString,"" .. satRate)
	end	
end

-- -----------------------------------------------------------------------------
-- Function: add_overallCustSatisfaction()
-- Inputs: addOverallCustSatisfaction
-- Returns: none
-- adds to the overall customer satisfaction (up to 100%) a new value and re-draws it on the screen
-- -----------------------------------------------------------------------------
function add_overallCustSatisfaction(addOverallCustSatisfaction)
	overallCustSatisfaction = overallCustSatisfaction + addOverallCustSatisfaction
	
	-- customer satisfaction is never more than 100
	if overallCustSatisfaction > 100 then
		overallCustSatisfaction = 100
	end
	
	E.set_string_text(custSatisfactionString,overallCustSatisfaction)

end

-- -----------------------------------------------------------------------------
-- Function: sub_overallCustSatisfaction()
-- Inputs: subOverallCustSatisfaction
-- Returns: none
-- substracts from the overall customer satisfaction (down to set threshold) a new value and re-draws it on the screen
-- -----------------------------------------------------------------------------
function sub_overallCustSatisfaction(subOverallCustSatisfaction)
	overallCustSatisfaction = overallCustSatisfaction - subOverallCustSatisfaction
	E.set_string_text(custSatisfactionString,overallCustSatisfaction)
	
	--if overall customer satisfaction becomes less then specified threshold, end the game
	if overallCustSatisfaction < custSatisfThreshold then
		gameEnd = true
	end
	
end
