require "ISUI/ISPanel"

ISAimHeliHUD = ISPanel:derive("ISAimHeliHUD");
local this = ISAimHeliHUD
local FONT_HGT_SMALL = getTextManager():getFontHeight(UIFont.NewSmall)
local FONT_HGT_MEDIUM = getTextManager():getFontHeight(UIFont.NewMedium)
local FONT_HGT_LARGE = getTextManager():getFontHeight(UIFont.NewLarge)
local btnClose = getText("IGUI_CraftUI_Close")
local btnUpload = getText("IGUI_ISTigerCrewLevel_Equip")
local btnCloseWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnClose) + 10
local btnCloseHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnClose) + 10
local btnUploadWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnUpload) + 10
local btnUploadHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnUpload) + 10
function ISAimHeliHUD:initialise()
    ISPanel.initialise(self);
    self:create();
end
local tempVector2 = Vector3f.new()
function ISAimHeliHUD:prerender()
    ISPanel.prerender(self);
end

function ISAimHeliHUD:destroy()
    UIManager.setShowPausedMessage(true)
    self:setVisible(false)
    self:removeFromUIManager()
    this.instance = nil
end
function ISAimHeliHUD:render()
    local maxY = getCore():getScreenHeight()
    local maxX = getCore():getScreenWidth()
    local curr_z = self.vehicle:getWorldPos(0, 0, 0, tempVector2):z()
    local YIndex = maxY / 2 - 400 * self.scale
    local DataList = {{
        Text = "IGUI_Heli_HUD_Height",
        Value = tostring(round(curr_z, 1))
    }, {
        Text = "IGUI_Heli_HUD_Speed",
        Value = tostring(round(Heli_GlobalSpeed, 1))
    }, {
        Text = "IGUI_Heli_HUD_Heading",
        Value = tostring(round(self.vehicle:getAngleZ(), 1))
    }}
    -- self:drawText(tostring(math.floor(curr_z)), self.width - 100 * self.scale, YIndex, 1, 1, 1, UIFont.NewSmall)
    -- YIndex = YIndex - 20 * self.scale - FONT_HGT_SMALL
    for i, data in ipairs(DataList) do
        self:drawText(getText(data.Text) .. ": " .. data.Value, maxX - 500 * self.scale, YIndex, 0, 1, 0, 1,
            UIFont.NewSmall)
        YIndex = YIndex + 20 * self.scale + FONT_HGT_SMALL
    end

end
function ISAimHeliHUD:onOptionMouseDown(button, x, y)
    if button.internal == "Close" then
        self:destroy()
    end

end
function ISAimHeliHUD:create()
    if getDebug() then
        self.close = ISButton:new(self.width - btnCloseWid, self.height - btnCloseHgt, btnCloseWid, btnCloseHgt,
            btnClose, self, self.onOptionMouseDown);
        self.close.internal = "Close";
        self.close:initialise();
        self.close:instantiate();

        self.close.borderColor = {
            r = 1,
            g = 1,
            b = 1,
            a = 0
        }
        self:addChild(self.close);
    end

end

function ISAimHeliHUD:update()

end

function ISAimHeliHUD:new(x, y, width, height, player, vehicle, HeliType, scale)
    local o = {};
    o = ISPanel:new(x, y, width, height);
    setmetatable(o, self);
    o.character = player;
    o.scale = scale or 1;
    self.__index = self;
    this.instance = o
    o.anchorRight = true
    o.anchorBottom = true
    o.anchorTop = true
    o.anchorLeft = true
    o.vehicle = vehicle
    o.HeliType = HeliType
    o.background = false
    o.moveWithMouse = true
    return o;
end

local function AddAimBox()
    local player = getPlayer()
    local vehicle = player:getVehicle()
    if (vehicle and GetHeliType(vehicle)) then
        if not HasUnlockThisReasearch(vehicle, "BetterHUD") then
            return
        end
        if vehicle:getSeat(player) == 0 then
            if not ISAimHeliHUD.instance then
                local maxY = getCore():getScreenHeight()
                local maxX = getCore():getScreenWidth()
                local scale = maxX / 1920
                local ISAimHeliHUD = ISAimHeliHUD:new(0, 0, 0, 0, player, vehicle, GetHeliType(vehicle), scale)
                ISAimHeliHUD:initialise();
                ISAimHeliHUD:addToUIManager();
            end

        else
            if ISAimHeliHUD.instance then
                ISAimHeliHUD.instance:destroy()
                ISAimHeliHUD.instance = nil
                return
            end
        end
    else
        if ISAimHeliHUD.instance then
            ISAimHeliHUD.instance:destroy()
            ISAimHeliHUD.instance = nil
            return
        end
    end

end
Events.EveryOneMinute.Add(AddAimBox)
