local AXUI = require "axui"
midMissionIntel = {}
midMissionIntel.functions = {}
midMissionIntel.Intel = {}
midMissionIntel.hudDraw = false
midMissionIntel.playerPosition = 1
midMissionIntel.playerSelectListPosition = 1





function midMissionIntel.functions.UIBase(hudOn)
    if hudOn then
        midMissionIntel.textbox:Draw()
    end
end

---Adds an intel pair
---@param intelTitle string title to add
---@param intelText string text to add
---@param position integer position to add to
---@overload fun(intelTitle: string, intelText: string)
function midMissionIntel.functions.addIntel(intelTitle,intelText,position)
    if position ~= nil and position >= 1 and position <= #midMissionIntel.Intel then
        table.insert(midMissionIntel.Intel,position,{intelTitle,intelText})
    else
        table.insert(midMissionIntel.Intel,{intelTitle,intelText})
    end
end

---display intel
---@param position integer
---@return boolean successs returns success or failure
function midMissionIntel.functions.displayIntel(position)
    if midMissionIntel.Intel[position] ~= nil then
        midMissionIntel.playerPosition = position
        midMissionIntel.textbox:ChangeText(tostring(midMissionIntel.Intel[position][1]).."\n"..tostring(midMissionIntel.Intel[position][2]))
        midMissionIntel.textbox.Offset = 3
        return true
    end
    return false
end

mn.LuaSEXPs["lua-midMissionIntel-size-box"].Action = function(xcoords,ycoords,width,height)
    midMissionIntel.textbox.TextX = xcoords
	midMissionIntel.textbox.TextY = ycoords
	midMissionIntel.textbox.TextWidth = width
	midMissionIntel.textbox.TextHeight = height
end

---adds an intel set to the tables
---@param intelTitle string title of intel
---@param intelText string text of intel
---@param position integer|nil
mn.LuaSEXPs["lua-midMissionIntel-add-intel"].Action = function(intelTitle,intelText,position)
    midMissionIntel.functions.addIntel(intelTitle,intelText)
end

---viewIntel
---@param hudActive boolean determine if the hud is active or not
mn.LuaSEXPs["lua-midMissionIntel-active-hud"].Action = function(hudActive)
    midMissionIntel.hudDraw = hudActive
end


---Deletes an intel table
---@param position integer
mn.LuaSEXPs["lua-midMissionIntel-del-intel"].Action = function(position)
    if position >= 1 and position <= #midMissionIntel.Intel then
        table.remove(midMissionIntel.Intel,position)
    end
end

midMissionIntel.selectIntel = function (doSelect)
    local gauge = hu.getHUDGaugeHandle("Target Monitor")

    local Font = gauge:getFont()

    local xCoords = midMissionIntel.textbox.TextX
    local yCoords = midMissionIntel.textbox.TextY+midMissionIntel.textbox.TextHeight+1
    local yHeight = Font.Height * 2
    local yMouse = io.getMouseY()

    if doSelect then
        if #midMissionIntel.Intel <= 10 then
            for i = 1, #midMissionIntel.Intel, 1 do
                if yMouse < yCoords+yHeight*(i+1) and yMouse > yCoords+yHeight*(i) then
                    midMissionIntel.functions.displayIntel(i)
                end
            end
        elseif #midMissionIntel.Intel > 10 then
            local tempPosition = midMissionIntel.playerSelectListPosition
            if midMissionIntel.Intel[tempPosition] == nil then
                tempPosition = 1
            end

            if yMouse < yCoords+yHeight*2 and yMouse > yCoords+yHeight then
                midMissionIntel.functions.displayIntel(tempPosition)
            end

            for i = 1, 9, 1 do
                tempPosition = tempPosition + 1
                if midMissionIntel.Intel[tempPosition] == nil then
                    tempPosition = 1
                end

                if yMouse < yCoords+yHeight*(i+2) and yMouse > yCoords+yHeight*(i+1) then
                    midMissionIntel.functions.displayIntel(tempPosition)
                end
            end
        end
    end
    --gr.CurrentResizeMode = prevMode
    --gr.resetScreenScale()
end

---Hud Draw Function
---@param hudOn boolean Determines if script should run or not
midMissionIntel.displayHud = function (hudOn)
    if hudOn then
        local gauge = hu.getHUDGaugeHandle("Target Monitor")
		local BaseWidth = 0
		local BaseHeight = 0
		--BaseWidth, BaseHeight = gauge:getBaseResolution()

        local prevFont = gr.CurrentFont
        local prevMode = gr.CurrentResizeMode
        local Font = gauge:getFont()
        gr.CurrentFont = Font
        --gr.CurrentResizeMode = GR_RESIZE_FULL
        --gr.setScreenScale(BaseWidth, BaseHeight)

        gr.setColor(255,255,255,200)
        local yMouse = io.getMouseY()



        local xCoords = midMissionIntel.textbox.TextX
        local yCoords = midMissionIntel.textbox.TextY+midMissionIntel.textbox.TextHeight+1
        local yHeight = Font.Height * 2
        if #midMissionIntel.Intel <= 10 then
            for i = 1, #midMissionIntel.Intel, 1 do
                if yMouse < yCoords+yHeight*(i+1) and yMouse > yCoords+yHeight*(i) then
                    gr.setColor(255,255,0,200)
                else
                    gr.setColor(255,255,255,200)
                end
                gr.drawString(midMissionIntel.Intel[i][1],xCoords,yCoords+yHeight*i)
            end
        elseif #midMissionIntel.Intel > 0 then
            local tempPosition = midMissionIntel.playerSelectListPosition
            if midMissionIntel.Intel[tempPosition] == nil then
                tempPosition = 1
            end

            if yMouse < yCoords+yHeight*2 and yMouse > yCoords+yHeight then
                gr.setColor(255,255,0,200)
            else
                gr.setColor(255,255,255,200)
            end
            gr.drawString(midMissionIntel.Intel[tempPosition][1],xCoords,yCoords+yHeight)

            for i = 1, 9, 1 do
                tempPosition = tempPosition + 1
                if midMissionIntel.Intel[tempPosition] == nil then
                    tempPosition = 1
                end

                if yMouse < yCoords+yHeight*(i+2) and yMouse > yCoords+yHeight*(i+1) then
                    gr.setColor(255,255,0,200)
                else
                    gr.setColor(255,255,255,200)
                end

                gr.drawString(midMissionIntel.Intel[tempPosition][1],xCoords,yCoords+yHeight*(i+1))
            end
        end



        gr.CurrentFont = prevFont
        --gr.CurrentResizeMode = prevMode
        --gr.resetScreenScale()
    end
end

--used to draw the close box in both the selected and non-selected form
midMissionIntel.drawCloseBox = function (hudOn)
    if hudOn then
        ---change these two to what you want and do the same for the select function below
        local xCoords = midMissionIntel.textbox.TextX+midMissionIntel.textbox.TextWidth+1
        local yCoords = midMissionIntel.textbox.TextY
        local yMouse = io.getMouseY()
        local xMouse = io.getMouseX()
        if xMouse >= xCoords and xMouse <= xCoords+30 and yMouse >= yCoords and yMouse <= yCoords+30 then
            gr.drawImageCentered("closeBoxSelected",xCoords,yCoords,30,30)
        else
            gr.drawImageCentered("closeBox",xCoords,yCoords,30,30)
        end 
    end
end

--used to determine if the selectclosebox is true or not
midMissionIntel.selectCloseBox = function (hudOn)
    if hudOn then
        ---change these two to what you want and do the same for the select function below
        local xCoords = midMissionIntel.textbox.TextX+midMissionIntel.textbox.TextWidth+1
        local yCoords = midMissionIntel.textbox.TextY
        local yMouse = io.getMouseY()
        local xMouse = io.getMouseX()
        if xMouse >= xCoords and xMouse <= xCoords+30 and yMouse >= yCoords and yMouse <= yCoords+30 then
            return true
        end
    end
    return false
end