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

require "ISUI/ISPanelJoypad"

ISVehicleInfoWindow = ISPanelJoypad:derive("ISVehicleInfoWindow")
ISVehiclePartListBox = ISScrollingListBox:derive("ISVehiclePartListBox")

-----

function ISVehicleInfoWindow:createChildren()
	--ISPanel.createChildren(self)
	
    self.close = ISButton:new(self:getWidth() - 20 - 240, self:getHeight() - 50, 80, 30, getText("UI_Cancel"), self, ISVehicleInfoWindow.onOptionMouseDown);
    self.close.internal = "CLOSE";
    self.close.anchorTop = false
    self.close.anchorBottom = true
    self.close:initialise();
    self.close:instantiate();
    self.close.borderColor = {r=1, g=1, b=1, a=0.1};
    self:addChild(self.close);
	
	self.btn_partEngine = ISButton:new(self.partEngine[1], self.partEngine[2], self.partEngine[3], self.partEngine[3], "", self, ISVehicleInfoWindow.onOptionMouseDown);
	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.1};
	self.btn_partEngine:setImage(self.texEngine);
	self:addChild(self.btn_partEngine);
	
	self.btn_partBattery = ISButton:new(self.partBattery[1], self.partBattery[2], self.partBattery[3], self.partBattery[3], "", self, ISVehicleInfoWindow.onOptionMouseDown);
	self.btn_partBattery:setTooltip("Battery");
	self.btn_partBattery.internal = "BATTERY";
	self.btn_partBattery:initialise();
	self.btn_partBattery:instantiate();
	self.btn_partBattery.borderColor = {r=1, g=1, b=1, a=0.1};
	self.btn_partBattery:setImage(self.texBattery);
	self:addChild(self.btn_partBattery);
	
	self.btn_partGas = ISButton:new(self.partGas[1], self.partGas[2], self.partGas[3], self.partGas[3], "", self, ISVehicleInfoWindow.onOptionMouseDown);
	self.btn_partGas.internal = "GAS";
	self.btn_partGas:setTooltip("Gas tank");
	self.btn_partGas:initialise();
	self.btn_partGas:instantiate();
	self.btn_partGas.borderColor = {r=1, g=1, b=1, a=0.1};
	self.btn_partGas:setImage(self.texGas);
	self:addChild(self.btn_partGas);
	
	self.btn_partDoorRight = ISButton:new(self.partDoorRight[1], self.partDoorRight[2], self.partDoorRight[3], self.partDoorRight[3], "", self, ISVehicleInfoWindow.onOptionMouseDown);
	self.btn_partDoorRight:setTooltip("Right door");
	self.btn_partDoorRight.internal = "DOORRIGHT";
	self.btn_partDoorRight:initialise();
	self.btn_partDoorRight:instantiate();
	self.btn_partDoorRight.borderColor = {r=1, g=1, b=1, a=0.1};
	self.btn_partDoorRight:setImage(self.texDoor);
	self:addChild(self.btn_partDoorRight);
	
	self.btn_partDoorLeft = ISButton:new(self.partDoorLeft[1], self.partDoorLeft[2], self.partDoorLeft[3], self.partDoorLeft[3], "", self, ISVehicleInfoWindow.onOptionMouseDown);
	self.btn_partDoorLeft:setTooltip("Left Door");
	self.btn_partDoorLeft.internal = "DOORLEFT";
	self.btn_partDoorLeft:initialise();
	self.btn_partDoorLeft:instantiate();
	self.btn_partDoorLeft.borderColor = {r=1, g=1, b=1, a=0.1};
	self.btn_partDoorLeft:setImage(self.texDoor);
	self:addChild(self.btn_partDoorLeft);
	
	self.btn_partHeater = ISButton:new(self.partHeater[1], self.partHeater[2], self.partHeater[3], self.partHeater[3], "", self, ISVehicleInfoWindow.onOptionMouseDown);
	self.btn_partHeater:setTooltip("Heater");
	self.btn_partHeater.internal = "HEATER";
	self.btn_partHeater:initialise();
	self.btn_partHeater:instantiate();
	self.btn_partHeater.borderColor = {r=1, g=1, b=1, a=0.1};
	self.btn_partHeater:setImage(self.texHeater);
	self:addChild(self.btn_partHeater);
	
	self.btn_partLight = ISButton:new(self.partLight[1], self.partLight[2], self.partLight[3], self.partLight[3], "", self, ISVehicleInfoWindow.onOptionMouseDown);
	self.btn_partLight:setTooltip("Light");
	self.btn_partLight.internal = "LIGHT";
	self.btn_partLight:initialise();
	self.btn_partLight:instantiate();
	self.btn_partLight.borderColor = {r=1, g=1, b=1, a=0.1};
	self.btn_partLight:setImage(self.texLight);
	self:addChild(self.btn_partLight);
	
	self.btn_partRadio = ISButton:new(self.partRadio[1], self.partRadio[2], self.partRadio[3], self.partRadio[3], "", self, ISVehicleInfoWindow.onOptionMouseDown);
	self.btn_partRadio:setTooltip("Radio");
	self.btn_partRadio.internal = "RADIO";
	self.btn_partRadio:initialise();
	self.btn_partRadio:instantiate();
	self.btn_partRadio.borderColor = {r=1, g=1, b=1, a=0.1};
	self.btn_partRadio:setImage(self.texRadio);
	self:addChild(self.btn_partRadio);
	
	self.btn_engine_start = ISButton:new(240, 250, 90, 20, "Start engine", self, ISVehicleInfoWindow.onOptionMouseDown);
	self.btn_engine_start.internal = "ENGINE_START";
	self.btn_engine_start:initialise();
	self.btn_engine_start:instantiate();
	self.btn_engine_start:setVisible(false);
	self:addChild(self.btn_engine_start);
	
	self.btn_engine_stop = ISButton:new(340, 250, 90, 20, "Stop engine", self, ISVehicleInfoWindow.onOptionMouseDown);
	self.btn_engine_stop.internal = "ENGINE_STOP";
	self.btn_engine_stop:initialise();
	self.btn_engine_stop:instantiate();
	self.btn_engine_stop:setVisible(false);
	self:addChild(self.btn_engine_stop);
end

function vehicleEngine2str(info)
	if info.engine_running then
		return "Running";
	end
	if info.engine_started then
		return "Started";
	end
	return "Stoped";
end

function bool2yesno(b)
	if b then
		return "yes"
	else
		return "no"
	end
end

function ISVehicleInfoWindow:prerender()
	ISPanelJoypad.prerender(self)
	local info = getVehicleInfo(self.vehicle)
	if not info then
		self:close()
	end
	-- vehicle
	self:drawTexture(self.texVehicle, self.partVehicle[1], self.partVehicle[2], 1);
	self:drawTextCentre(info.currentFrontEndDurability.."/"..info.frontEndDurability, self.textFrontDurability[1], self.textFrontDurability[2], 0, 0, 0, 1, UIFont.Medium)
	self:drawTextCentre(info.currentRearEndDurability.."/"..info.rearEndDurability, self.textRearDurability[1], self.textRearDurability[2], 0, 0, 0, 1, UIFont.Medium)
	-- info
	self:drawText("Model name:", 240, 40, 1, 1, 1, 1, UIFont.Medium)
	self:drawText(info.name, 340, 40, 1, 1, 1, 1, UIFont.Medium)
	self:drawText("Weight:", 240, 70, 1, 1, 1, 1, UIFont.Medium)
	self:drawText(tostring(info.weight), 340, 70, 1, 1, 1, 1, UIFont.Medium)
	self:drawText("Speed", 240, 100, 1, 1, 1, 1, UIFont.Medium)
	self:drawText(tostring(info.speed), 340, 100, 1, 1, 1, 1, UIFont.Medium)
	self:drawText("______________________________", 220, 120, 1, 1, 1, 1, UIFont.Medium)
	
	if self.select == "ENGINE" then
		self.btn_partEngine.backgroundColor = {r=0, g=0.8, b=0, a=1.0};
		self.btn_partBattery.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partGas.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorLeft.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorRight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partRadio.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partHeater.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partLight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self:drawText("Engine", 320, 140, 1, 1, 1, 1, UIFont.Medium)
		self:drawText("Status: " .. vehicleEngine2str(info), 240, 170, 1, 1, 1, 1, UIFont.Small)
		self:drawText("Quality: " .. tostring(info.engine_quality), 240, 190, 1, 1, 1, 1, UIFont.Small)
		self:drawText("Loudness: " .. tostring(info.engine_loudness), 240, 210, 1, 1, 1, 1, UIFont.Small)
--		self:drawText("Power: " .. tostring(info.engine_power), 240, 230, 1, 1, 1, 1, UIFont.Small)
		self.btn_engine_start:setEnable(not info.engine_started and not info.engine_running);
		self.btn_engine_stop:setEnable(info.engine_running);
	end
	if self.select == "BATTERY" then
		self.btn_partEngine.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partBattery.backgroundColor = {r=0, g=0.8, b=0, a=1.0};
		self.btn_partGas.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorLeft.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorRight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partRadio.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partHeater.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partLight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self:drawText("Battery", 320, 140, 1, 1, 1, 1, UIFont.Medium)
		self:drawText("Set: " .. tostring(info.battery_isset), 240, 170, 1, 1, 1, 1, UIFont.Small)
		self:drawText("Charge: " .. tostring(info.battery_charge), 240, 190, 1, 1, 1, 1, UIFont.Small)
	end
	if self.select == "GAS" then
		self.btn_partEngine.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partBattery.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partGas.backgroundColor = {r=0, g=0.8, b=0, a=1.0};
		self.btn_partDoorLeft.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorRight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partRadio.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partHeater.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partLight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self:drawText("Gas tank", 320, 140, 1, 1, 1, 1, UIFont.Medium)
		self:drawText("Amount: " .. tostring(info.gas_amount), 240, 170, 1, 1, 1, 1, UIFont.Small)
		self:drawText("Capacity: " .. tostring(info.gas_capacity), 240, 190, 1, 1, 1, 1, UIFont.Small)
	end
	if self.select == "DOORLEFT" then
		self.btn_partEngine.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partBattery.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partGas.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorLeft.backgroundColor = {r=0, g=0.8, b=0, a=1.0};
		self.btn_partDoorRight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partRadio.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partHeater.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partLight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self:drawText("Left door", 320, 140, 1, 1, 1, 1, UIFont.Medium)
		if (info.doorleft_exist) then
			self:drawText("Open: " .. bool2yesno(info.doorleft_open), 240, 170, 1, 1, 1, 1, UIFont.Small)
			self:drawText("Locked: " .. bool2yesno(info.doorleft_locked), 240, 190, 1, 1, 1, 1, UIFont.Small)
			self:drawText("Lock broken: " .. bool2yesno(info.doorleft_lockbroken), 240, 210, 1, 1, 1, 1, UIFont.Small)
			self:drawText("Window", 320, 230, 1, 1, 1, 1, UIFont.Medium)
			self:drawText("______________________________", 220, 250, 1, 1, 1, 1, UIFont.Medium)
			if (info.windowleft_exist) then
				self:drawText("Open: " .. bool2yesno(info.windowleft_open), 240, 270, 1, 1, 1, 1, UIFont.Small)
				self:drawText("Health: " .. tostring(info.windowleft_health), 240, 290, 1, 1, 1, 1, UIFont.Small)
			else
				self:drawText("Window is not exist", 240, 270, 1, 1, 1, 1, UIFont.Small)
			end
		else
			self:drawText("Door is not exist", 240, 170, 1, 1, 1, 1, UIFont.Small)
		end
	end
	if self.select == "DOORRIGHT" then
		self.btn_partEngine.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partBattery.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partGas.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorLeft.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorRight.backgroundColor = {r=0, g=0.8, b=0, a=1.0};
		self.btn_partRadio.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partHeater.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partLight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self:drawText("Right door", 320, 140, 1, 1, 1, 1, UIFont.Medium)
		if (info.doorright_exist) then
			self:drawText("Open: " .. bool2yesno(info.doorright_open), 240, 170, 1, 1, 1, 1, UIFont.Small)
			self:drawText("Locked: " .. bool2yesno(info.doorright_locked), 240, 190, 1, 1, 1, 1, UIFont.Small)
			self:drawText("Lock broken: " .. bool2yesno(info.doorright_lockbroken), 240, 210, 1, 1, 1, 1, UIFont.Small)
			self:drawText("Window", 320, 230, 1, 1, 1, 1, UIFont.Medium)
			self:drawText("______________________________", 220, 250, 1, 1, 1, 1, UIFont.Medium)
			if (info.windowright_exist) then
				self:drawText("Open: " .. bool2yesno(info.windowright_open), 240, 270, 1, 1, 1, 1, UIFont.Small)
				self:drawText("Health: " .. tostring(info.windowright_health), 240, 290, 1, 1, 1, 1, UIFont.Small)
			else
				self:drawText("Window is not exist", 240, 270, 1, 1, 1, 1, UIFont.Small)
			end
		else
			self:drawText("Door is not exist", 240, 170, 1, 1, 1, 1, UIFont.Small)
		end
	end
	if self.select == "RADIO" then
		self.btn_partEngine.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partBattery.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partGas.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorLeft.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorRight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partRadio.backgroundColor = {r=0, g=0.8, b=0, a=1.0};
		self.btn_partHeater.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partLight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self:drawText("Radio", 320, 140, 1, 1, 1, 1, UIFont.Medium)
	end
	if self.select == "LIGHT" then
		self.btn_partEngine.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partBattery.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partGas.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorLeft.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorRight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partRadio.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partHeater.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partLight.backgroundColor = {r=0, g=0.8, b=0, a=1.0};
		self:drawText("Headlight", 320, 140, 1, 1, 1, 1, UIFont.Medium)
		self:drawText("Set: " .. bool2yesno(info.headlights_set), 240, 170, 1, 1, 1, 1, UIFont.Small)
		self:drawText("Charge: " .. bool2yesno(info.headlights_on), 240, 190, 1, 1, 1, 1, UIFont.Small)
	end
	if self.select == "HEATER" then
		self.btn_partEngine.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partBattery.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partGas.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorLeft.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partDoorRight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partRadio.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self.btn_partHeater.backgroundColor = {r=0, g=0.8, b=0, a=1.0};
		self.btn_partLight.backgroundColor = {r=0, g=0, b=0, a=1.0};
		self:drawText("Heater", 320, 140, 1, 1, 1, 1, UIFont.Medium)
		self:drawText("Set: " .. bool2yesno(info.heater_isset), 240, 170, 1, 1, 1, 1, UIFont.Small)
		if info.heater_isset then
			self:drawText("On: " .. bool2yesno(info.heater_on), 240, 190, 1, 1, 1, 1, UIFont.Small)
		end
	end
end


function ISVehicleInfoWindow:setVehicle(vehicle)
	self.vehicle = vehicle
end


function ISVehicleInfoWindow:onGainJoypadFocus(joypadData)
	self.drawJoypadFocus = true
	--self:setISButtonForA(ISVehicleInfoWindow.instance.partsButton)
end

function ISVehicleInfoWindow:onJoypadDown(button, joypadData)
	if button == Joypad.BButton then
		self.vehicle = nil
		self:setVisible(false)
		self:setEnabled(false)
		joypadData.focus = nil
	end
end

function ISVehicleInfoWindow:onJoypadDirUp()

end

function ISVehicleInfoWindow:onJoypadDirDown()

end

function ISVehicleInfoWindow:onOptionMouseDown(button, x, y)
	if button.internal == "ENGINE" then
		self.select = "ENGINE" 
		self.btn_engine_start:setVisible(true);
		self.btn_engine_stop:setVisible(true);
	elseif button.internal == "BATTERY" then
		self.select = "BATTERY" 
		self.btn_engine_start:setVisible(false);
		self.btn_engine_stop:setVisible(false);
	elseif button.internal == "GAS" then
		self.select = "GAS" 
		self.btn_engine_start:setVisible(false);
		self.btn_engine_stop:setVisible(false);
	elseif button.internal == "DOORLEFT" then
		self.select = "DOORLEFT" 
		self.btn_engine_start:setVisible(false);
		self.btn_engine_stop:setVisible(false);
	elseif button.internal == "DOORRIGHT" then
		self.select = "DOORRIGHT" 
		self.btn_engine_start:setVisible(false);
		self.btn_engine_stop:setVisible(false);
	elseif button.internal == "HEATER" then
		self.select = "HEATER" 
		self.btn_engine_start:setVisible(false);
		self.btn_engine_stop:setVisible(false);
	elseif button.internal == "LIGHT" then
		self.select = "LIGHT" 
		self.btn_engine_start:setVisible(false);
		self.btn_engine_stop:setVisible(false);
	elseif button.internal == "RADIO" then
		self.select = "RADIO" 
		self.btn_engine_start:setVisible(false);
		self.btn_engine_stop:setVisible(false);
	elseif button.internal == "ENGINE_START" then
		local playerObj = getSpecificPlayer(self.playerNum)
		ISVehicleMenu.onStartEngine(playerObj)
	elseif button.internal == "ENGINE_STOP" then
		local playerObj = getSpecificPlayer(self.playerNum)
		ISVehicleMenu.onStopEngine(playerObj)

	elseif button.internal == "PARTS" then
		self.select = "NONE" 
		self:close()
	elseif button.internal == "CLOSE" then
        self:setVisible(false);
        --self:removeFromUIManager();
        if JoypadState.players[self.playerNum+1] then
            setJoypadFocus(self.playerNum, nil)
        end
	end
end

function ISVehicleInfoWindow:new(x, y, width, height, playerNum)
	local o = ISPanelJoypad:new(x, y, width, height)
	setmetatable(o, self)
	self.__index = self
    if y == 0 then
        o.y = getPlayerScreenTop(playerNum) + (getPlayerScreenHeight(playerNum) - height) / 2
        o:setY(o.y)
    end
    if x == 0 then
        o.x = getPlayerScreenLeft(playerNum) + (getPlayerScreenWidth(playerNum) - width) / 2
        o:setX(o.x)
    end
	o.playerNum = playerNum
	--o:setResizable(false)
	o.texVehicle = getTexture("media/ui/vehicle/vehicle.png")
	o.texEngine = getTexture("media/ui/vehicle/engine.png")
	o.texGas = getTexture("media/ui/vehicle/gas.png")
	o.texBattery = getTexture("media/ui/vehicle/battery.png")
	o.texDoor = getTexture("media/ui/vehicle/door.png")
	o.texHeater = getTexture("media/ui/vehicle/heater.png")
	o.texLight = getTexture("media/ui/vehicle/light.png")
	o.texRadio = getTexture("media/ui/vehicle/radio.png")
	o.partVehicle = {20, 20} -- x, y
	o.partEngine = {46,86,60} -- x, y, size(w,h)
	o.partBattery = {126,86,60}
	o.partGas = {145,333,60}
	o.partDoorRight = {145,223,60}
	o.partDoorLeft = {34,223,60}
	o.partLight = {23,23,60}
	o.partHeater = {122,163,60}
	o.partRadio = {50,163,60}
	o.textFrontDurability = {120,43}
	o.textRearDurability = {120,424}
	o.select=""
	return o
end

