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

require "ISUI/ISCollapsableWindow"

ISVehicleInfoWindowDetail = ISCollapsableWindow:derive("ISVehicleInfoWindowDetail")
ISVehiclePartListBox = ISScrollingListBox:derive("ISVehiclePartListBox")

-----

function ISVehiclePartListBox:onRightMouseUp(x, y)
	local row = self:rowAt(x, y)
	if row < 1 or row > #self.items then return end
	self.selected = row
	ISVehiclePartMenu.create(self.parent.playerNum, self.items[row].item.part,
		self.parent:getAbsoluteX() + self:getX() + x,
		self.parent:getAbsoluteY() + self:getYScroll() + self:getY() + y)
end

-----

function ISVehicleInfoWindowDetail:createChildren()
	ISCollapsableWindow.createChildren(self)

	self.partList = ISVehiclePartListBox:new(20, self:titleBarHeight() + 20, 300, self.height - 20 * 2 - self:titleBarHeight())
	self.partList:initialise()
	self.partList:setAnchorRight(true)
	self.partList:setAnchorBottom(true)
	self.partList.drawBorder = true
	self.partList:setFont('Medium', 7)
	self:addChild(self.partList)
end

function ISVehicleInfoWindowDetail:prerender()
	ISCollapsableWindow.prerender(self)
	if not self.vehicle or not getCell():getVehicles():contains(self.vehicle) then
		self:setVisible(false)
	end

	local item = self.partList.items[self.partList.selected]
	if item and item.item.part then
		local part = item.item.part
		local y = 40
		local lineHgt = 20
		local inventoryItem = part:getInventoryItem()
		if inventoryItem then
			self:drawText("Item = " .. inventoryItem:getFullType() .. " (installed)", self.partList:getRight() + 20, y, 1, 1, 1, 1)
			y = y + lineHgt
			if instanceof(inventoryItem, "DrainableComboItem") then
				self:drawText("UsedDelta = " .. tostring(inventoryItem:getUsedDelta()), self.partList:getRight() + 20, y, 1, 1, 1, 1)
				y = y + lineHgt
			end
		elseif part:getItemType() then
			self:drawText("Item = " .. part:getItemType() .. " (uninstalled)", self.partList:getRight() + 20, y, 1, 0, 0, 1)
			y = y + lineHgt
		end
		if part:getContainerContentType() then
			self:drawText(part:getContainerContentType() .. " amount = " .. round(part:getContainerContentAmount(), 3) .. " / " .. part:getContainerCapacity(), self.partList:getRight() + 20, y, 1, 1, 1, 1)
			y = y + lineHgt
		end
		local door = part:getDoor()
		if door then
			self:drawText("door", self.partList:getRight() + 20, y, 1, 1, 1, 1)
			y = y + lineHgt
			self:drawText("open = " .. tostring(door:isOpen()), self.partList:getRight() + 40, y, 1, 1, 1, 1)
			y = y + lineHgt
			self:drawText("locked = " .. tostring(door:isLocked()), self.partList:getRight() + 40, y, 1, 1, 1, 1)
			y = y + lineHgt
			self:drawText("lockBroken = " .. tostring(door:isLockBroken()), self.partList:getRight() + 40, y, 1, 1, 1, 1)
			y = y + lineHgt
		end
		local window = part:getWindow()
		if window then
			self:drawText("window", self.partList:getRight() + 20, y, 1, 1, 1, 1)
			y = y + lineHgt
			self:drawText("open = " .. tostring(window:isOpen()), self.partList:getRight() + 40, y, 1, 1, 1, 1)
			y = y + lineHgt
			self:drawText("health = " .. window:getHealth(), self.partList:getRight() + 40, y, 1, 1, 1, 1)
			y = y + lineHgt
		end
		local functionName = part:getLuaFunction("checkEngine")
		if functionName then
			local check = VehicleUtils.callLua(functionName, self.vehicle, part)
			local r,g,b,a = 1,1,1,1
			if not check then r,g,b,a = 1,0,0,1 end
			self:drawText("checkEngine = " .. tostring(check), self.partList:getRight() + 20, y, r, g, b, a)
			y = y + lineHgt
		end
		functionName = part:getLuaFunction("checkOperate")
		if functionName then
			local check = VehicleUtils.callLua(functionName, self.vehicle, part)
			local r,g,b,a = 1,1,1,1
			if not check then r,g,b,a = 1,0,0,1 end
			self:drawText("checkOperate = " .. tostring(check), self.partList:getRight() + 20, y, r, g, b, a)
			y = y + lineHgt
		end
		if part:hasModData() then
			local r,g,b,a = 1,1,1,1
			self:drawText("modData", self.partList:getRight() + 20, y, r, g, b, a)
			y = y + lineHgt
			local keys = {}
			for k,v in pairs(part:getModData()) do
				table.insert(keys, k)
			end
			table.sort(keys, function(a,b) return not string.compare(a, b) end)
			for _,k in ipairs(keys) do
				self:drawText(k .. " = " .. tostring(part:getModData()[k]), self.partList:getRight() + 40, y, r, g, b, a)
				y = y + lineHgt
			end
		end
	end
end

function ISVehicleInfoWindowDetail:setVehicle(vehicle)
	self.vehicle = vehicle
	self:setPartList()
end

function ISVehicleInfoWindowDetail:setPartList()
	self.partList:clear()
	for i=1,self.vehicle:getPartCount() do
		local part = self.vehicle:getPartByIndex(i-1)
		self.partList:addItem(part:getId(), { part = part } )
	end
end

function ISVehicleInfoWindowDetail:onGainJoypadFocus(joypadData)
	self.drawJoypadFocus = true
end

function ISVehicleInfoWindowDetail:onJoypadDown(button, joypadData)
	if button == Joypad.AButton then
		ISVehiclePartMenu.create(self.playerNum, self.partList.items[self.partList.selected].item.part,
			self:getAbsoluteX() + self.partList:getX() + 50,
			self:getAbsoluteY() + self.partList:getY() + 50)
	end
	if button == Joypad.BButton then
		self.vehicle = nil
		self:setVisible(false)
		joypadData.focus = nil
	end
end

function ISVehicleInfoWindowDetail:onJoypadDirUp()
	self.partList:onJoypadDirUp()
end

function ISVehicleInfoWindowDetail:onJoypadDirDown()
	self.partList:onJoypadDirDown()
end

function ISVehicleInfoWindowDetail:new(x, y, width, height, playerNum)
	local o = ISCollapsableWindow:new(x, y, width, height)
	setmetatable(o, self)
	self.__index = self
	o.playerNum = playerNum
	o:setResizable(false)
	return o
end

