local addOnName, namespace = ...

local WTUI = namespace.WildThingsUI
local Theme = namespace.Theme

local BORDER_COLOR = WTUI.settings.BORDER
local WINDOW_BG_BOTTOM = Theme.WINDOW_BG_BOTTOM
local WINDOW_BG_HOVER = Theme.WINDOW_BG_HOVER
local WINDOW_BORDER = Theme.WINDOW_BORDER
local CHROME_ICON_IDLE = Theme.CHROME_ICON_IDLE
local CHROME_ICON_HOVER = Theme.CHROME_ICON_HOVER

local BUTTON_SIZE = 18
local BUTTON_MARGIN_X = 4
local BUTTON_MARGIN_Y = 2

local MOVER_FRAME_HEIGHT = BUTTON_SIZE + 2 * BUTTON_MARGIN_Y

-- Name is used to save position/size of the window (if set)
function namespace:CreateWindow(name, exitable, movable, resizable)
    local window = CreateFrame("Frame", nil, UIParent)

    window.buttons = {}

    window:SetScript("OnMouseWheel", function() end) -- Stop scroll wheel propagation
    window:EnableMouse(true)

    -- Background — rounded fill replaces the old two-texture gradient (Pitfall 2)
    WTUI:AddRoundedFill(window, Theme.RADIUS_LG)
    window:SetRoundedFillColor(WINDOW_BG_BOTTOM.r, WINDOW_BG_BOTTOM.g, WINDOW_BG_BOTTOM.b, WINDOW_BG_BOTTOM.a)

    -- 1px dark-green border, following the same rounded corners as the fill above.
    WTUI:AddRoundedBorder(window, Theme.RADIUS_LG, 1)
    window:SetBorderColor(WINDOW_BORDER.r, WINDOW_BORDER.g, WINDOW_BORDER.b, WINDOW_BORDER.a)

    function window:AddButton(texture, tooltip, onClick)
        if not window.buttonFrame then
            window.buttonFrame = CreateFrame("Button", nil, window.moverFrame)
            window.buttonFrame:SetPoint("TOPRIGHT", window)
            window.buttonFrame:SetHeight(MOVER_FRAME_HEIGHT)
        end

        local button = CreateFrame("Button", nil, window.buttonFrame)

        button:SetSize(BUTTON_SIZE, BUTTON_SIZE)
        button:SetPoint("RIGHT", window.buttonFrame, "RIGHT", -#window.buttons * (BUTTON_SIZE + BUTTON_MARGIN_X) - BUTTON_MARGIN_Y, 0)

        window.buttonFrame:SetWidth((#window.buttons + 1) * (BUTTON_SIZE + BUTTON_MARGIN_X))

        button.tex = button:CreateTexture(nil, "ARTWORK")
        button.tex:SetTexture(texture)
        button.tex:SetVertexColor(CHROME_ICON_IDLE.r, CHROME_ICON_IDLE.g, CHROME_ICON_IDLE.b)
        button.tex:SetSnapToPixelGrid(false)
        button.tex:SetTexelSnappingBias(0)
        button.tex:SetAllPoints(button)

        button:SetScript(
            "OnEnter",
            function()
                button.tex:SetVertexColor(CHROME_ICON_HOVER.r, CHROME_ICON_HOVER.g, CHROME_ICON_HOVER.b)
            end
        )

        button:SetScript(
            "OnLeave",
            function()
                button.tex:SetVertexColor(CHROME_ICON_IDLE.r, CHROME_ICON_IDLE.g, CHROME_ICON_IDLE.b)
            end
        )

        button:SetScript(
            "OnClick",
            onClick
        )

        if tooltip then
            WTUI:AddTooltip(button, tooltip)
        end

        table.insert(window.buttons, button)
    end

    -- Restores the previous (prior to relog/reload) size/position of the window, if any
    -- Returns whether a saved size/position existed, so that the user can apply some default sizing/positioning if not
    function window:RestoreSize()
        return WTUI:RestoreSize(window, name)
    end

    function window:RestorePosition()
        return WTUI:RestorePosition(window, name)
    end

    -- Layers this window in front of other windows when clicked
    -- Only layers in front of other windows that have RaiseOnClick set
    -- WILDTHINGS_WINDOW_RAISER is a global frame, and windows from multiple addons can be added to it
    -- This function should be called AFTER parenting to another frame, since parenting overrides frames strata
    function window:RaiseOnClick()
        WILDTHINGS_WINDOW_RAISER:AddWindow(window)
    end

    -- Mover frame
    if movable then
        window:SetMovable(true)

        window.moverFrame = CreateFrame("Frame", nil, window)
        window.moverFrame:SetPoint("TOPLEFT", window)
        window.moverFrame:SetPoint("TOPRIGHT", window)
        window.moverFrame:SetHeight(MOVER_FRAME_HEIGHT)

        window.moverFrame.tex = window.moverFrame:CreateTexture(nil, "BACKGROUND")
        window.moverFrame.tex:SetPoint("TOPLEFT", window)
        window.moverFrame.tex:SetPoint("BOTTOMRIGHT", window, "TOPRIGHT", 0, -MOVER_FRAME_HEIGHT)
        window.moverFrame.tex:SetColorTexture(WINDOW_BG_BOTTOM.r, WINDOW_BG_BOTTOM.g, WINDOW_BG_BOTTOM.b)
        window.moverFrame.tex:SetSnapToPixelGrid(false)
        window.moverFrame.tex:SetTexelSnappingBias(0)

        window.moverFrame:SetScript(
            "OnEnter",
            function()
                window.moverFrame.tex:SetColorTexture(WINDOW_BG_HOVER.r, WINDOW_BG_HOVER.g, WINDOW_BG_HOVER.b)
            end
        )

        window.moverFrame:SetScript(
            "OnLeave",
            function()
                window.moverFrame.tex:SetColorTexture(WINDOW_BG_BOTTOM.r, WINDOW_BG_BOTTOM.g, WINDOW_BG_BOTTOM.b)
            end
        )

        window.moverFrame:SetScript(
            "OnMouseDown",
            function(_, button)
                if button == "LeftButton" then
                    window:StartMoving()
                end
            end
        )

        window.moverFrame:SetScript(
            "OnMouseUp",
            function(_, button)
                if button == "LeftButton" then
                    window:StopMovingOrSizing()

                    WTUI:SavePosition(window, name)
                end
            end
        )
    end

    -- Exit cross
    if exitable then
        window:AddButton(
            string.format("Interface\\Addons\\%s\\UI\\Media\\Textures\\ExitCross.tga", addOnName),
            nil,
            function()
                window:Hide()
            end
        )
    end

    -- Resize frame
    if resizable then
        window:SetResizable(true)

        window.resizeFrame = CreateFrame("Frame", nil, window)
        window.resizeFrame:SetSize(24, 24)
        window.resizeFrame:SetPoint("BOTTOMRIGHT", window, "BOTTOMRIGHT")

        window.resizeFrame.tex = window.resizeFrame:CreateTexture(nil, "BACKGROUND")
        window.resizeFrame.tex:SetTexture(string.format("Interface\\Addons\\%s\\UI\\Media\\Textures\\ResizeTriangle.tga", addOnName))
        window.resizeFrame.tex:SetVertexColor(CHROME_ICON_IDLE.r, CHROME_ICON_IDLE.g, CHROME_ICON_IDLE.b)
        window.resizeFrame.tex:SetSnapToPixelGrid(false)
        window.resizeFrame.tex:SetTexelSnappingBias(0)
        window.resizeFrame.tex:SetAllPoints(window.resizeFrame)

        window.resizeFrame:SetScript(
            "OnEnter",
            function()
                window.resizeFrame.tex:SetVertexColor(CHROME_ICON_HOVER.r, CHROME_ICON_HOVER.g, CHROME_ICON_HOVER.b)
            end
        )

        window.resizeFrame:SetScript(
            "OnLeave",
            function()
                window.resizeFrame.tex:SetVertexColor(CHROME_ICON_IDLE.r, CHROME_ICON_IDLE.g, CHROME_ICON_IDLE.b)
            end
        )

        window.resizeFrame:SetScript(
            "OnMouseDown",
            function(_, button)
                if button == "LeftButton" then
                    window:StartSizing()
                end
            end
        )

        window.resizeFrame:SetScript(
            "OnMouseUp",
            function(_, button)
                if button == "LeftButton" then
                    window:StopMovingOrSizing()

                    WTUI:SaveSize(window, name)
                end
            end
        )
    end

    return window
end
