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

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

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

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")
		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_position(balance, xPos-toolW/2+width, yPos-toolH/2,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)
	
		-- create brush to be used for strings
		brush = E.create_brush()
		E.set_brush_color(brush,0,0,0,1)
		E.set_typeface("data/Coprgtb.TTF")
		
		--create string for balance
		balanceString = E.create_string(profitBalance)
		E.set_string_fill(balanceString, brush)
		
		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(overallCustSatisfaction)
		E.set_string_fill(custSatisfactionString, brush)
		
		E.set_entity_position(custSatisfactionString, xPos - 180, yPos - 20,0)
		E.set_entity_scale(custSatisfactionString, 13,13,0)
	
		-- push the strings
		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(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)
	profitBalance = newBalance
	E.set_string_text(balanceString,profitBalance)
	
	--if balance becomes negative, end the game
	if profitBalance < 0 then
		gameEnd = true
	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,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,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(newOverallCustSatisfaction)
	overallCustSatisfaction = newOverallCustSatisfaction
	
	-- customer satisfaction is never more than 100
	if overallCustSatisfaction > 100 then
		overallCustSatisfaction = 100
	end
	
	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

-- -----------------------------------------------------------------------------
-- 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
