require "ISUI/ISPanel"

PanzerThermal = ISPanel:derive("PanzerThermal");
local this = PanzerThermal

function PanzerThermal:initialise()
    ISPanel.initialise(self);
    self:create();
end

function PanzerThermal:prerender()
    ISPanel.prerender(self);
end

function PanzerThermal:destroy()
    UIManager.setShowPausedMessage(true)
    self:setVisible(false)
    self:removeFromUIManager()
end

function PanzerThermal:render()
    local w = getPlayerScreenWidth(1)
    local h = getPlayerScreenHeight(1)
    self:drawTextureScaled(nil, 0, 0, w, h, 0.1, 0, 0.5, 1)
end

function PanzerThermal:create()

end

function PanzerThermal:new(x, y, width, height, player)
    local o = {};
    o = ISPanel:new(x, y, width, height);
    setmetatable(o, self);
    o.character = player;
    self.__index = self;
    this.instance = o
    o.anchorRight = true
    o.anchorBottom = true
    o.anchorTop = true
    o.anchorLeft = true
    return o;
end

local LightMode = false

local function MarkZombie(playerObj)
    if not LightMode then
        return
    end
    local cellZombieList = getCell():getZombieList()
    local RenderList = {}
    for i = 0, cellZombieList:size() - 1 do
        local zombie = cellZombieList:get(i)
        if zombie then
            local square = zombie:getCurrentSquare()
            if square:isCouldSee(playerObj:getPlayerNum()) then
                local damagea = TextDrawObject.new()
                damagea:setDefaultColors(1, 1, 0.1, 0.7)
                damagea:setOutlineColors(0, 0, 0, 1)
                local AimBox = "[  ]"
                damagea:ReadString(UIFont.Middle, AimBox, -1)
                local sx = IsoUtils.XToScreen(zombie:getX(), zombie:getY(), zombie:getZ(), 0);
                local sy = IsoUtils.YToScreen(zombie:getX(), zombie:getY(), zombie:getZ(), 0);
                sx = sx - IsoCamera.getOffX() - zombie:getOffsetX();
                sy = sy - IsoCamera.getOffY() - zombie:getOffsetY();
                sy = sy - 64
                sx = sx / getCore():getZoom(0)
                sy = sy / getCore():getZoom(0)
                sy = sy - damagea:getHeight()
                table.insert(RenderList, {60, damagea, sx, sy, sx, sy})
            end
        end
    end
    local timemultiplier = getGameTime():getMultiplier()
    for la, lb in pairs(RenderList) do

        lb[1] = lb[1] - timemultiplier
        if lb[1] < 0 then
            lb = nil
        else
            lb[3] = lb[3] + timemultiplier
            lb[4] = lb[4] - timemultiplier
            lb[2]:AddBatchedDraw(lb[3], lb[4], true)
        end
    end
end

local LightMode = false
local function OnKeyPressed(_key)
    local playerObj = getPlayer()
    local vehicle = playerObj:getVehicle()
    if _key == getCore():getKey("OpenThermalPanzer") and (vehicle and GetPanzerType(vehicle)) and
        HasUnlockThisReasearch(vehicle, "NightVersion") then
        if PanzerThermal.instance and LightMode then
            PanzerThermal.instance:destroy()
            PanzerThermal.instance = nil
            LightMode = false
            playerObj:playSound("NightVersionSW")
            Events.OnPlayerUpdate.Remove(MarkZombie)
            return
        end
        LightMode = true
        playerObj:playSound("NightVersionSW")
        local maxY = getCore():getScreenHeight()
        local maxX = getCore():getScreenWidth()
        local PanzerThermal = PanzerThermal:new(0, 0, 0, 0, getSpecificPlayer(0));
        PanzerThermal:initialise();
        PanzerThermal:addToUIManager();
        Events.OnPlayerUpdate.Add(MarkZombie)
    else
        if not vehicle then
            if PanzerThermal.instance then
                PanzerThermal.instance:destroy()
                PanzerThermal.instance = nil
                LightMode = false
                Events.OnPlayerUpdate.Remove(MarkZombie)
            end
        end
    end
end

local function CheckLocal()
    Events.OnKeyPressed.Add(OnKeyPressed)
end

Events.OnGameStart.Add(CheckLocal)

