require "ISUI/ISPanel"

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

function ISAimStatusBox:prerender()
    ISPanel.prerender(self);
    -- self:drawTextureScaled(nil, 5, 5, 64, 64, 1, 0, 0, 0)

    -- self:drawText(getText("IGUI_VehicleNameTigerII"), 33 + (155 - TextLength - 5), 15 + 1, 1, 1, 1, 1, UIFont.NewSma);
end

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

function ISAimStatusBox:render()
    -- local w = getPlayerScreenWidth(1)
    -- local h = getPlayerScreenHeight(1)
    -- local tex = getTexture("media/textures/NightVersionOverlay-noiseless.png")
    local AmmoThatNeedToBeFound = {}
    local ModDataTimes = {}
    if PanzerList[self.panzerType].AmmoCache then

        for k, v in pairs(PanzerList[self.panzerType].AmmoCache) do
            if k == "Normal" then
                local AmmoBox = self.vehicle:getPartById(self.panzerType .. "_AmmoCache")
                if AmmoBox then
                    local inv = AmmoBox:getItemContainer()
                    for k, v in pairs(PanzerList[self.panzerType].AmmoType) do
                        if v ~= "modData" then
                            if not AmmoThatNeedToBeFound[k] then
                                AmmoThatNeedToBeFound[k] = 0
                            end
                            AmmoThatNeedToBeFound[k] = AmmoThatNeedToBeFound[k] + inv:getNumberOfItem(v)
                        else
                            if not ModDataTimes[k] then
                                ModDataTimes[k] = 0
                            end
                            local modData = self.vehicle:getModData()
                            if modData[k] then
                                ModDataTimes[k] = ModDataTimes[k] + modData[k]
                            end
                        end
                    end
                end
            else
                local modData = self.vehicle:getModData()
                if modData.researchTable and modData.researchTable[k] then
                    local AmmoBox = self.vehicle:getPartById(self.panzerType .. "_AmmoCacheBig")
                    if AmmoBox then

                        local inv = AmmoBox:getItemContainer()
                        for k, v in pairs(PanzerList[self.panzerType].AmmoType) do
                            if v ~= "modData" then
                                if not AmmoThatNeedToBeFound[k] then
                                    AmmoThatNeedToBeFound[k] = 0
                                end
                                AmmoThatNeedToBeFound[k] = AmmoThatNeedToBeFound[k] + inv:getNumberOfItem(v)
                            else
                                if not ModDataTimes[k] then
                                    ModDataTimes[k] = 0
                                end
                                local modData = self.vehicle:getModData()
                                if modData[k] then
                                    ModDataTimes[k] = ModDataTimes[k] + modData[k]
                                end
                            end
                        end
                    end
                end
            end
        end
        if PanzerList[self.panzerType].AmmoType["MG"] then
            AmmoThatNeedToBeFound["MG"] = getPlayer():getInventory():getNumberOfItem(PanzerList[self.panzerType]
                                                                                         .AmmoType["MG"])
        end
        local index = 1
        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

        for k, v in pairs(ModDataTimes) 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

        -- local AnTiPersonnelAmmo = modData.AnTiPersonnelTimes or 0
        -- self:drawText(tostring(AnTiPersonnelAmmo), 5 + 5 * 3 + 64 * 3 + 5, 5 + 5, 1, 1, 1, 1, UIFont.NewSmall);
        -- local ArtSupportAmmo = modData.ArtSupportTimes or 0
        -- self:drawText(tostring(ArtSupportAmmo), 5 + 5 * 4 + 64 * 4 + 5, 5 + 5, 1, 1, 1, 1, UIFont.NewSmall);
        -- local AirSupportAmmo = modData.AirStrikeTimes or 0
        -- self:drawText(tostring(AirSupportAmmo), 5 + 5 * 5 + 64 * 5 + 5, 5 + 5, 1, 1, 1, 1, UIFont.NewSmall);
    end
end
function ISAimStatusBox:onOptionMouseDown(button, x, y)
    if button.internal == "Close" then
        self:destroy()
    end
    if button.internal == "ArtSupport" then
        local AimCursor = ISArtSupportCursor:new("", "", self.character)
        getCell():setDrag(AimCursor, 0)
        return
    end
    if button.internal == "AirSupport" then
        if not FakeISAirSupportUI.instance then
            local maxY = getCore():getScreenHeight()
            local maxX = getCore():getScreenWidth()
            local ISAimStatusBox = FakeISAirSupportUI:new(0, 0, 0, 0, self.character, self.square, self.vehicle);
            ISAimStatusBox:initialise();
            ISAimStatusBox:addToUIManager();
            local type = GetPanzerType(self.vehicle)
            local CrewLaungage = PanzerList[type].CrewLaungage
            self.character:playSound(CrewLaungage .. "CallAirSupport")
            local modData = self.vehicle:getModData()
            if modData.AirStrikeTimes and modData.AirStrikeTimes > 0 then
                modData.AirStrikeTimes = modData.AirStrikeTimes - 1
            end
        end
        return
    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(PanzerList[self.panzerType].WeaponTypeList) do
        -- "ArtSupport", "AirSupport", "AnTiPersonnel"s

        if v == button.internal then
            if v == "MG" then
                PanzerMGEnable = true
            else
                PanzerMGEnable = 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 ISAimStatusBox: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

    local WeaponList = PanzerList[self.panzerType].WeaponTypeList
    local TextureList = PanzerList[self.panzerType].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 ISAimStatusBox:update()
    local index = 0
    for k, v in pairs(PanzerList[self.panzerType].AmmoType) do
        index = index + 1
        local NowSelectWeapon = PanzerList[self.panzerType].WeaponTypeList[index]
        if v ~= "modData" then
            -- print(NowSelectWeapon)
            if HasUnlockThisReasearch(self.vehicle, NowSelectWeapon) or index == 1 then
                self[NowSelectWeapon]:setEnable(true)
            else
                self[NowSelectWeapon]:setEnable(false)
            end
        else
            local modData = self.vehicle:getModData()
            if modData[k] and modData[k] > 0 then
                self[NowSelectWeapon]:setEnable(true)
            else

                self[NowSelectWeapon]:setEnable(false)

            end
        end
    end
end

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

local function AddAimBox()
    local player = getPlayer()
    local vehicle = player:getVehicle()
    if (vehicle and GetPanzerType(vehicle)) then
        if vehicle:getSeat(player) == 0 then
            if not ISAimStatusBox.instance then
                local maxY = getCore():getScreenHeight()
                local maxX = getCore():getScreenWidth()
                local ISAimStatusBox = ISAimStatusBox:new(600 + ((maxX - (64 + 10) * 6) / 2), (maxY - 74) - 50,
                    (64 + 10) * 6, 74, player, vehicle, GetPanzerType(vehicle))
                ISAimStatusBox:initialise();
                ISAimStatusBox:addToUIManager();
            end

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

end
Events.EveryOneMinute.Add(AddAimBox)
