--***********************************************************
--**                    THE INDIE STONE                    **
--***********************************************************

require "ISUI/ISPanel"

ISVehicleDashboard = ISPanel:derive("ISVehicleDashboard")

function ISVehicleDashboard:createChildren()
	self.backgroundTex = ISImage:new(100,35, self.dashboardBG:getWidth(), self.dashboardBG:getHeight(), self.dashboardBG);
	self.backgroundTex:initialise();
	self.backgroundTex:instantiate();
	self:addChild(self.backgroundTex);

--	self.btn_partEngine = ISButton:new(100, 53, 35, 35, "", self, ISVehicleDashboard.onMouseDown);
--	self.btn_partEngine:setTooltip("Engine");
--	self.btn_partEngine.internal = "ENGINE";
--	self.btn_partEngine:initialise();
--	self.btn_partEngine:instantiate();
--	self.btn_partEngine.borderColor = {r=1, g=1, b=1, a=0};
--	self.btn_partEngine:setImage(self.texEngine);
--	self:addChild(self.btn_partEngine);
	
	self.btn_partSpeed = ISLabel:new(100, 53, 24, "S", 1,1,1,0.85, UIFont.Small);
	self.btn_partSpeed:initialise();
	self.btn_partSpeed:instantiate();
	self.btn_partSpeed.tooltip = getText("Tooltip_Dashboard_Shift")
	self:addChild(self.btn_partSpeed);
	
	self.doorTex = ISImage:new(200,35, self.iconDoor:getWidthOrig(), self.iconDoor:getHeightOrig(), self.iconDoor);
	self.doorTex:initialise();
	self.doorTex:instantiate();
	self.doorTex.onclick = ISVehicleDashboard.onClickDoors;
	self.doorTex.target = self;
	self.doorTex.mouseovertext = getText("Tooltip_Dashboard_LockedDoors")
	self:addChild(self.doorTex);

	self.engineTex = ISImage:new(100,35, self.iconEngine:getWidthOrig(), self.iconEngine:getHeightOrig(), self.iconEngine);
	self.engineTex:initialise();
	self.engineTex:instantiate();
	self.engineTex.onclick = ISVehicleDashboard.onClickEngine;
	self.engineTex.mouseovertext = getText("Tooltip_Dashboard_Engine")
	self.engineTex.target = self;
	self:addChild(self.engineTex);
	
	self.lightsTex = ISImage:new(300,35, self.iconLights:getWidthOrig(), self.iconLights:getHeightOrig(), self.iconLights);
	self.lightsTex:initialise();
	self.lightsTex:instantiate();
	self.lightsTex.onclick = ISVehicleDashboard.onClickHeadlights;
	self.lightsTex.mouseovertext = getText("Tooltip_Dashboard_Headlights")
	self.lightsTex.target = self;
	self:addChild(self.lightsTex);
	
	self.heaterTex = ISImage:new(400,35, self.iconHeater:getWidthOrig(), self.iconHeater:getHeightOrig(), self.iconHeater);
	self.heaterTex:initialise();
	self.heaterTex:instantiate();
	self.heaterTex.onclick = ISVehicleDashboard.onClickHeater;
	self.heaterTex.mouseovertext = getText("Tooltip_Dashboard_Heater")
	self.heaterTex.target = self;
	self:addChild(self.heaterTex);

	self.ignitionTex = ISImage:new(500,35, self.iconIgnition:getWidthOrig(), self.iconIgnition:getHeightOrig(), self.iconIgnition);
	self.ignitionTex:initialise();
	self.ignitionTex:instantiate();
	self.ignitionTex.onclick = ISVehicleDashboard.onClickKeys;
	self.ignitionTex.target = self;
	self.ignitionTex.mouseovertext = getText("Tooltip_Dashboard_KeysIgnition")
	self:addChild(self.ignitionTex);
	
	self.batteryTex = ISImage:new(600,35, self.iconBattery:getWidthOrig(), self.iconBattery:getHeightOrig(), self.iconBattery);
	self.batteryTex:initialise();
	self.batteryTex:instantiate();
	self.batteryTex.mouseovertext = getText("Tooltip_Dashboard_Battery")
	self:addChild(self.batteryTex);
	
	self.trunkTex = ISImage:new(700,35, self.iconTrunk:getWidthOrig(), self.iconTrunk:getHeightOrig(), self.iconTrunk);
	self.trunkTex:initialise();
	self.trunkTex:instantiate();
	self.trunkTex.onclick = ISVehicleDashboard.onClickTrunk;
	self.trunkTex.target = self;
	self:addChild(self.trunkTex);
		
	self.outsidetemp = ISImage:new(700,35, 31, 40, nil);
	self.outsidetemp:initialise();
	self.outsidetemp:instantiate();
	self.outsidetemp.backgroundColor = {r=0.9,g=0.9,b=0.9,a=0.85};
	self.outsidetemp.mouseovertext = getText("Tooltip_Dashboard_OutsideTemp");
	self:addChild(self.outsidetemp);

	local x = (self.backgroundTex:getWidth() / 2) - 40
	local y = self.backgroundTex:getHeight() - self.gaugeFull:getHeight() - 20
	self.fuelGauge = ISVehicleGauge:new(x, y, self.gaugeFull, 40, 37, 180, 0)
	self.fuelGauge:initialise()
	self.fuelGauge:instantiate()
	self.fuelGauge:setNeedleWidth(25)
	self:addChild(self.fuelGauge)

	x = 85
	y = self.backgroundTex:getHeight() - self.engineGaugeTex:getHeight()
	self.engineGauge = ISVehicleGauge:new(x, y, self.engineGaugeTex, 64, 60, 180, 0)
	self.engineGauge:initialise()
	self.engineGauge:instantiate()
	self:addChild(self.engineGauge)

	x = self.backgroundTex:getWidth() - 211
	y = self.backgroundTex:getHeight() - self.speedGaugeTex:getHeight()
	self.speedGauge = ISVehicleGauge:new(x, y, self.speedGaugeTex, 64, 60, 180, 0)
	self.speedGauge:initialise()
	self.speedGauge:instantiate()
	self:addChild(self.speedGauge)

	self:onResolutionChange()
end

function ISVehicleDashboard:setVehicle(vehicle)
	self.vehicle = vehicle
	for _,gauge in ipairs(self.gauges) do
		gauge:setVisible(false)
	end
	self.gauges = {}
	if not vehicle then
		self:removeFromUIManager()
		return
	end
	local part = vehicle:getPartById("GasTank")
	if part and part:isContainer() and part:getContainerContentType() then
		self.gasTank = part
		if self.vehicle:isEngineRunning() then
			self.fuelValue = self.gasTank:getContainerContentAmount() / self.gasTank:getContainerCapacity()
		else
			self.fuelValue = 0.0
		end	
		self.fuelGauge:setVisible(true)
		table.insert(self.gauges, self.fuelGauge)
	else
		self.gasTank = nil
		self.fuelGauge:setVisible(false)
	end
	self.engineGauge:setVisible(true)
	table.insert(self.gauges, self.engineGauge)
	self.speedGauge:setVisible(true)
	table.insert(self.gauges, self.speedGauge)
	if #self.gauges > 0 then
		self:addToUIManager()
		self:onResolutionChange()
	else
		self:removeFromUIManager()
	end
	
end

function ISVehicleDashboard:prerender()
	if not self.vehicle then return end
	local greyBg = {r=1, g=1, b=1, a=0.3};
	local alpha = 0.65;
	if self.gasTank then
		local current = 0.0
		if self.vehicle:isEngineRunning() or self.vehicle:isKeysInIgnition() then
			current = self.gasTank:getContainerContentAmount() / self.gasTank:getContainerCapacity()
		end
		if self.fuelValue < current then
			self.fuelValue = math.min(self.fuelValue + 0.015 * (30 / getPerformance():getUIRenderFPS()), current)
		elseif self.fuelValue > current then
			self.fuelValue = math.max(self.fuelValue - 0.05 * (30 / getPerformance():getUIRenderFPS()), current)
		end
		self.fuelGauge:setValue(self.fuelValue)
		local engineSpeedValue = 0;
		local speedValue = 0;
		if self.vehicle:isEngineRunning() then
			engineSpeedValue = math.max(0,math.min(1,(self.vehicle:getEngineSpeed()-1000)/7000));
			speedValue = math.max(0,math.min(1,math.abs(self.vehicle:getCurrentSpeedKmHour())/120));
		end
		self.engineGauge:setValue(engineSpeedValue)
		-- RJ: Fake the speedometer a tad
		self.speedGauge:setValue(speedValue * BaseVehicle.getFakeSpeedModifier())
	end
	self.batteryTex.backgroundColor = greyBg;
	if not self:checkEngineFull() and self.vehicle:isKeysInIgnition() and (not self.vehicle:isEngineRunning() and not self.vehicle:isEngineStarted()) then
		self.engineTex.backgroundColor = {r=1, g=0, b=0, a=alpha};
	else
		if self.vehicle:isEngineRunning() then
			self.engineTex.backgroundColor = {r=0, g=1, b=0, a=alpha};
			self.btn_partSpeed.name = self.vehicle:getTransmissionNumberLetter();
		elseif self.vehicle:isEngineStarted() then
			self.engineTex.backgroundColor = {r=1, g=0.5, b=0.1, a=alpha};
			self.btn_partSpeed.name = "P";
		else
			self.engineTex.backgroundColor = greyBg;
			self.btn_partSpeed.name = "P";
		end
	end
	if self.vehicle:isEngineRunning() or self.vehicle:isKeysInIgnition() then
		if self.vehicle:getBatteryCharge() > 0 then
			self.batteryTex.backgroundColor = {r=0, g=1, b=0, a=alpha};
		else
			self.batteryTex.backgroundColor = {r=1, g=0, b=0, a=alpha};
		end
	end
	if self.vehicle:isKeysInIgnition() and not self.vehicle:isHotwired() and not (self.vehicle:isEngineRunning() or self.vehicle:isStarting()) then
		self.ignitionTex.texture = self.iconIgnitionKey;
	elseif (self.vehicle:isEngineRunning() or self.vehicle:isStarting()) and not self.vehicle:isHotwired() then
		self.ignitionTex.texture = self.iconIgnitionStarted;
	elseif self.vehicle:isHotwired() then
		self.ignitionTex.texture = self.iconIgnitionHotwired;
	else
		self.ignitionTex.texture = self.iconIgnition;
	end
	if self.vehicle:getHeadlightsOn() then
		self.lightsTex.backgroundColor = {r=0, g=1, b=0, a=alpha};
	else
		self.lightsTex.backgroundColor = greyBg;
	end
	if self.vehicle:areAllDoorsLocked() then
		self.doorTex.backgroundColor = {r=0, g=1, b=0, a=alpha};
	else
		self.doorTex.backgroundColor = greyBg;
	end
	if self.vehicle:getPartById("Heater") then
		if self.vehicle:getPartById("Heater"):getModData().active then
			self.heaterTex.backgroundColor = {r=0, g=1, b=0, a=alpha};
		else
			self.heaterTex.backgroundColor = greyBg;
		end
	end
	self.trunkTex:setVisible(self.vehicle:getPartById("TruckBed") ~= nil)
	if self.vehicle:isTrunkLocked() then
		self.trunkTex.backgroundColor = {r=0, g=1, b=0, a=alpha};
		self.trunkTex.mouseovertext = getText("Tooltip_Dashboard_TrunkLocked")
	else
		self.trunkTex.backgroundColor = greyBg;
		self.trunkTex.mouseovertext = getText("Tooltip_Dashboard_TrunkUnlocked")
	end

	if (self.vehicle:isEngineRunning() or self.vehicle:isKeysInIgnition()) and self.vehicle:getRemainingFuelPercentage() < 15 then
		self.fuelGauge:setTexture(self.gaugeLow);
		if self.vehicle:getRemainingFuelPercentage() < 5 then
			self.fuelGauge:setTexture(self.gaugeEmpty);
		end
	else
		self.fuelGauge:setTexture(self.gaugeFull);
	end
end

function ISVehicleDashboard:checkEngineFull()
	for i=0,self.vehicle:getPartCount() do
		local part = self.vehicle:getPartByIndex(i);
		if part and part:getLuaFunction("checkEngine") and not VehicleUtils.callLua(part:getLuaFunction("checkEngine"), self.vehicle, part) then
			return false;
		end
	end
	return true;
end
		
function ISVehicleDashboard:render()
	self:onResolutionChange();
	if self.outsidetemp then
		self.outsidetemp.name = round(getWorld():getGlobalTemperature(),1) .. "";
	end
end

function ISVehicleDashboard:onResolutionChange()

	local screenLeft = getPlayerScreenLeft(self.playerNum)
	local screenTop = getPlayerScreenTop(self.playerNum)
	local screenWidth = getPlayerScreenWidth(self.playerNum)
	local screenHeight = getPlayerScreenHeight(self.playerNum)

	if self.backgroundTex == nil then
		return;
	end
	
	self:setHeight(self.backgroundTex:getHeight());
	self:setX(screenLeft + (screenWidth - self.backgroundTex:getWidth()) / 2);
	self:setY(screenTop + screenHeight);

	if self.backgroundTex then
		self.backgroundTex:setX(0)
		self.backgroundTex:setY(0)
	end
	
	local x = self:getX();

	if self.btn_partSpeed ~= nil then
		self.btn_partSpeed:setX(self.backgroundTex:getX() + 60);
		self.btn_partSpeed:setY(self.backgroundTex:getY() + 55);
	end
	
	if self.engineTex then
		self.engineTex:setX(self.backgroundTex:getX() + 105);
		self.engineTex:setY(self.backgroundTex:getY() + 26);
	end
	
	if self.batteryTex then
		self.batteryTex:setX(self.engineTex:getX() + 36);
		self.batteryTex:setY(self.engineTex:getY());
	end
	
	if self.outsidetemp then
		self.outsidetemp:setX(self.batteryTex:getX() + 34);
		self.outsidetemp:setY(self.batteryTex:getY() - 20);
	end
	
	if self.doorTex then
		self.doorTex:setX(self.engineTex:getX() + 250);
		self.doorTex:setY(self.engineTex:getY());
	end
	
	if self.lightsTex then
		self.lightsTex:setX(self.doorTex:getX() + 29);
		self.lightsTex:setY(self.doorTex:getY());
	end
	
	if self.heaterTex then
		self.heaterTex:setX(self.lightsTex:getX() + 25);
		self.heaterTex:setY(self.lightsTex:getY());
	end
	
	if self.trunkTex then
		self.trunkTex:setX(self.heaterTex:getX() + 24);
		self.trunkTex:setY(self.heaterTex:getY());
	end

	if self.ignitionTex then
		self.ignitionTex:setX(self.trunkTex:getX() + 42);
		self.ignitionTex:setY(self.trunkTex:getY() + 34);
	end
end

function ISVehicleDashboard:onClickEngine()
	if getGameSpeed() == 0 then return; end
	if getGameSpeed() > 1 then setGameSpeed(1); end
	if not self.vehicle then return end
	if self.vehicle:isEngineRunning() then
		ISVehicleMenu.onShutOff(self.character)
	else
		ISVehicleMenu.onStartEngine(self.character)
	end
end

function ISVehicleDashboard:onClickHeadlights()
	if getGameSpeed() == 0 then return; end
	if getGameSpeed() > 1 then setGameSpeed(1); end
	ISVehicleMenu.onToggleHeadlights(self.character);
end

function ISVehicleDashboard:onClickDoors()
	if getGameSpeed() == 0 then return; end
	if getGameSpeed() > 1 then setGameSpeed(1); end
	ISVehiclePartMenu.onLockDoors(self.character, self.vehicle, not self.vehicle:areAllDoorsLocked());
end

function ISVehicleDashboard:onClickTrunk()
	if getGameSpeed() == 0 then return; end
	if getGameSpeed() > 1 then setGameSpeed(1); end
	ISVehicleMenu.onToggleTrunkLocked(self.character);
end

function ISVehicleDashboard:onClickHeater()
	if getGameSpeed() == 0 then return; end
	if getGameSpeed() > 1 then setGameSpeed(1); end
	ISVehicleMenu.onToggleHeater(self.character)
end

function ISVehicleDashboard:onClickKeys()
	if getGameSpeed() == 0 then return; end
	if getGameSpeed() > 1 then setGameSpeed(1); end
	if not self.vehicle then return end
	if self.vehicle:isEngineRunning() then
		ISVehicleMenu.onShutOff(self.character)
	elseif not self.vehicle:isEngineStarted() then
		self.vehicle:setKeysInIgnition(not self.vehicle:isKeysInIgnition());
	end
end

function ISVehicleDashboard:new(playerNum, chr)
	local o = ISPanel:new(0, 0, 200, 200)
	setmetatable(o, self)
	self.__index = self
	o.playerNum = playerNum
	o.character = chr;
	o.gauges = {}
	o.texEngine = getTexture("media/ui/vehicle/engine.png")
	o.iconEngine = getTexture("media/ui/vehicles/icon_enginetrouble_light.png");
	o.iconAirCondition = getTexture("media/ui/vehicles/icon_airconditioning_light.png");
	o.iconDoor = getTexture("media/ui/vehicles/icon_doorslocked_light.png");
	o.iconLights = getTexture("media/ui/vehicles/icon_headlights_light.png");
	o.iconHeater = getTexture("media/ui/vehicles/icon_heating_light.png");
	o.iconIgnition = getTexture("media/ui/vehicles/ignition.png");
	o.iconIgnitionKey = getTexture("media/ui/vehicles/ignition_key_off.png");
	o.iconIgnitionStarted = getTexture("media/ui/vehicles/ignition_key_on.png");
	o.iconIgnitionHotwired = getTexture("media/ui/vehicles/ignition_hotwired.png");
	o.iconBattery = getTexture("media/ui/vehicles/icon_lowbattery_light.png");
	o.iconTrunk = getTexture("media/ui/vehicles/icon_trunk_light.png")
	o.dashboardBG = getTexture("media/ui/vehicles/dashboard.png");
	o.gaugeFull = getTexture("media/ui/vehicles/rj-vehicle_fuelguage-full.png");
	o.gaugeLow = getTexture("media/ui/vehicles/rj-vehicle_fuelguage-low.png");
	o.gaugeEmpty = getTexture("media/ui/vehicles/rj-vehicle_fuelguage-empty.png");
	o.engineGaugeTex = getTexture("media/ui/vehicles/rj-vehicle_engineguage.png")
	o.speedGaugeTex = getTexture("media/ui/vehicles/vehicle_spedometer.png")
	o:setWidth(o.dashboardBG:getWidth());
	return o
end

function ISVehicleDashboard.onEnterVehicle(character)
	local vehicle = character:getVehicle()
	if instanceof(character, 'IsoPlayer') and character:isLocalPlayer() and vehicle:isDriver(character) then
		getPlayerVehicleDashboard(character:getPlayerNum()):setVehicle(vehicle)
	end
end

function ISVehicleDashboard.onExitVehicle(character)
	if instanceof(character, 'IsoPlayer') and character:isLocalPlayer() then
		getPlayerVehicleDashboard(character:getPlayerNum()):setVehicle(nil)
	end
end

function ISVehicleDashboard.onSwitchVehicleSeat(character)
	if instanceof(character, 'IsoPlayer') and character:isLocalPlayer() then
		local vehicle = character:getVehicle()
		if vehicle:isDriver(character) then
			getPlayerVehicleDashboard(character:getPlayerNum()):setVehicle(vehicle)
		else
			getPlayerVehicleDashboard(character:getPlayerNum()):setVehicle(nil)
		end
	end
end

function ISVehicleDashboard.OnGameStart()
	if isServer() then return end
	for i=1,getNumActivePlayers() do
		local playerObj = getSpecificPlayer(i-1)
		if playerObj and not playerObj:isDead() and playerObj:getVehicle() then
			ISVehicleDashboard.onEnterVehicle(playerObj)
		end
	end
end

LuaEventManager.AddEvent("OnExitVehicle")
LuaEventManager.AddEvent("OnSwitchVehicleSeat")
Events.OnEnterVehicle.Add(ISVehicleDashboard.onEnterVehicle)
Events.OnExitVehicle.Add(ISVehicleDashboard.onExitVehicle)
Events.OnSwitchVehicleSeat.Add(ISVehicleDashboard.onSwitchVehicleSeat)
Events.OnGameStart.Add(ISVehicleDashboard.OnGameStart)
