require "ISUI/ISPanel"

ISAimHeliStatusBox = ISPanel:derive("ISAimHeliStatusBox");
local this = ISAimHeliStatusBox
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 ISAimHeliStatusBox:initialise()
    ISPanel.initialise(self);
    self:create();
end

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

function ISAimHeliStatusBox:destroy()
    UIManager.setShowPausedMessage(true)
    self:setVisible(false)
    self:removeFromUIManager()
    this.instance = nil
end

local function GetTargetEquipmentAmmo(vehicle, target)
    local HeliType = GetHeliType(vehicle)
    local Count = 0
    for i = 1, HeliList[HeliType].EquipmentMaxCount do
        local RocketTable = vehicle:getPartById(HeliList[HeliType].EquipmentName .. i)
        if RocketTable and RocketTable:getInventoryItem() then
            if RocketTable:getInventoryItem():getType() == target then
                Count = Count + 1
            end
        end
    end
    return Count
end

function ISAimHeliStatusBox:render()
    -- local w = getPlayerScreenWidth(1)
    -- local h = getPlayerScreenHeight(1)
    -- local tex = getTexture("media/textures/NightVersionOverlay-noiseless.png")
    local AmmoThatNeedToBeFound = {}
    local index = 1
    local ModDataTimes = {}
    if HeliList[self.HeliType].AmmoCache then
        for k, v in pairs(HeliList[self.HeliType].AmmoCache) do
            if k == "Normal" then
                local AmmoBox = self.vehicle:getPartById(self.HeliType .. "_AmmoCache")
                if AmmoBox then
                    local inv = AmmoBox:getItemContainer()
                    for k, v in pairs(HeliList[self.HeliType].AmmoType) do
                        if v ~= "modData" and
                            (HeliList[self.HeliType].EquipmentAmmoType and
                                not HeliList[self.HeliType].EquipmentAmmoType[k]) then
                            if not AmmoThatNeedToBeFound[k] then
                                AmmoThatNeedToBeFound[k] = 0
                            end
                            AmmoThatNeedToBeFound[k] = AmmoThatNeedToBeFound[k] + inv:getNumberOfItem(v)
                        end
                    end
                end
            end
        end

        for k, v in pairs(AmmoThatNeedToBeFound) do
            self:drawText(tostring(v), 5 + 5 * (index - 1) + 64 * (index - 1) + 5, 5 + 5, 1, 1, 1, 1, UIFont.NewSmall);
            index = index + 1
        end
    end
    if HeliList[self.HeliType].EquipmentName then
        local EquipMentAmmoList = {}
        local MaxCount = HeliList[self.HeliType].EquipmentMaxCount
        for i = 1, MaxCount do
            local RocketTable = self.vehicle:getPartById(HeliList[self.HeliType].EquipmentName .. i)
            if RocketTable and RocketTable:getInventoryItem() then
                for k, v in pairs(HeliList[self.HeliType].EquipmentAmmoType) do
                    if RocketTable:getInventoryItem():getType() == v then
                        if not EquipMentAmmoList[k] then
                            EquipMentAmmoList[k] = 0
                        end
                        if v == "RocketNest10" then
                            local PartContion = RocketTable:getCondition()
                            EquipMentAmmoList[k] = EquipMentAmmoList[k] + math.floor(PartContion / 10)
                        else
                            EquipMentAmmoList[k] = EquipMentAmmoList[k] + 1
                        end

                    end
                end
            end
        end
        for k, v in pairs(HeliList[self.HeliType].AmmoType) do
            if k == "Normal" then
                self:drawText(tostring(v), 5 + 5 * (index - 1) + 64 * (index - 1) + 5, 5 + 5, 1, 1, 1, 1,
                    UIFont.NewSmall);
                index = index + 1
            end
        end
        for k, v in pairs(HeliList[self.HeliType].EquipmentAmmoType) do
            if not EquipMentAmmoList[k] then
                EquipMentAmmoList[k] = 0
            end
            self:drawText(tostring(EquipMentAmmoList[k]), 5 + 5 * (index - 1) + 64 * (index - 1) + 5, 5 + 5, 1, 1, 1, 1,
                UIFont.NewSmall);
            index = index + 1

        end
    end
end
function ISAimHeliStatusBox:onOptionMouseDown(button, x, y)
    if button.internal == "Close" then
        self:destroy()
    end
    -- if button.internal == "AnTiPersonnel" then
    --     self.character:playSound("ActiveSystem")
    --     local modData = self.vehicle:getModData()
    --     if modData.AnTiPersonnelTimes and modData.AnTiPersonnelTimes > 0 then
    --         modData.AnTiPersonnelTimes = modData.AnTiPersonnelTimes - 1
    --     end
    --     PanzerDetectTarget(self.character:getCurrentSquare(), "AnTiPersonnel")
    --     return
    -- end
    local modData = self.vehicle:getModData()
    modData.AmmoTypeNow = button.internal
    for k, v in pairs(HeliList[self.HeliType].WeaponTypeList) do
        if v == button.internal then
            if HeliList[self.HeliType].EquipmentAmmoType[v] then
                HeliRocketEnable = true
            else
                HeliRocketEnable = false
            end
            self[v].borderColor = {
                r = 1,
                g = 1,
                b = 1,
                a = 1
            }
        else
            self[v].borderColor = {
                r = 1,
                g = 1,
                b = 1,
                a = 0.2
            }
        end

    end

end
function ISAimHeliStatusBox: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
    if not self.HeliType then
        self.HeliType = GetHeliType(self.vehicle)
    end
    local Temp = self.HeliType
    local WeaponList = HeliList[self.HeliType].WeaponTypeList
    local TextureList = HeliList[self.HeliType].WeaponTypeListTexture
    for i, v in ipairs(WeaponList) do
        self[v] = ISButton:new(5 + 5 * (i - 1) + 64 * (i - 1), 5, 64, 64, "", self, self.onOptionMouseDown);
        self[v].internal = v;
        self[v]:initialise();
        self[v]:instantiate();

        self[v].borderColor = {
            r = 1,
            g = 1,
            b = 1,
            a = 0.2
        }
        if TextureList[i] then
            self[v]:setImage(getTexture("media/textures/UI/" .. TextureList[i] .. ".png"))
        end
        self:addChild(self[v]);
    end
end

function ISAimHeliStatusBox:update()
    local index = 0
    for k, v in pairs(HeliList[self.HeliType].AmmoType) do
        index = index + 1
        local NowSelectWeapon = HeliList[self.HeliType].WeaponTypeList[index]

        -- print(NowSelectWeapon)

        if HasUnlockThisReasearch(self.vehicle, NowSelectWeapon) then
            if HeliList[self.HeliType].EquipmentAmmoType[NowSelectWeapon] then
                if GetTargetEquipmentAmmo(self.vehicle, v) > 0 or index == 1 then
                    self[NowSelectWeapon]:setEnable(true)
                else
                    self[NowSelectWeapon]:setEnable(false)
                end
            else
                self[NowSelectWeapon]:setEnable(true)
            end
        else
            self[NowSelectWeapon]:setEnable(false)
        end

    end
end

function ISAimHeliStatusBox:new(x, y, width, height, player, vehicle, HeliType)
    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
    o.vehicle = vehicle
    o.HeliType = HeliType
    o.moveWithMouse = true
    return o;
end

local function AddAimBox()
    local player = getPlayer()
    local vehicle = player:getVehicle()
    if (vehicle and GetHeliType(vehicle)) then

        if not HeliList[GetHeliType(vehicle)].HasWeaponAimStatusBox then
            return
        end
        if vehicle:getSeat(player) == 0 then
            if not ISAimHeliStatusBox.instance then
                local maxY = getCore():getScreenHeight()
                local maxX = getCore():getScreenWidth()
                local ISAimHeliStatusBox = ISAimHeliStatusBox:new(600 + ((maxX - (64 + 10) * 6) / 2), (maxY - 74) - 50,
                    (64 + 10) * 6, 74, player, vehicle, GetHeliType(vehicle))
                ISAimHeliStatusBox:initialise();
                ISAimHeliStatusBox:addToUIManager();
            end

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

end
Events.EveryOneMinute.Add(AddAimBox)
