local addonName, addon = ...

--------------------------------------------------
-- WORLD MARKER DATA
--------------------------------------------------

addon.worldMarkers = {
    {
        marker = 1,
        r = 0.20,
        g = 0.55,
        b = 1.00,
    },
    {
        marker = 2,
        r = 0.20,
        g = 0.85,
        b = 0.25,
    },
    {
        marker = 3,
        r = 0.75,
        g = 0.30,
        b = 1.00,
    },
    {
        marker = 4,
        r = 1.00,
        g = 0.20,
        b = 0.20,
    },
    {
        marker = 5,
        r = 1.00,
        g = 0.85,
        b = 0.15,
    },
    {
        marker = 6,
        r = 1.00,
        g = 0.55,
        b = 0.15,
    },
    {
        marker = 7,
        r = 0.35,
        g = 0.80,
        b = 1.00,
    },
    {
        marker = 8,
        r = 1.00,
        g = 1.00,
        b = 1.00,
    },
}

addon.worldMarkerButtons = {}

--------------------------------------------------
-- CREATE WHITE BORDER
--------------------------------------------------

local function CreateBorder(button)

    local border = CreateFrame(
        "Frame",
        nil,
        button
    )

    border:SetAllPoints()

    border:SetFrameLevel(
        button:GetFrameLevel() + 10
    )

    --------------------------------------------------
    -- TOP
    --------------------------------------------------

    local top = border:CreateTexture(
        nil,
        "OVERLAY"
    )

    top:SetColorTexture(
        1,
        1,
        1,
        1
    )

    top:SetPoint(
        "TOPLEFT"
    )

    top:SetPoint(
        "TOPRIGHT"
    )

    top:SetHeight(2)

    --------------------------------------------------
    -- BOTTOM
    --------------------------------------------------

    local bottom = border:CreateTexture(
        nil,
        "OVERLAY"
    )

    bottom:SetColorTexture(
        1,
        1,
        1,
        1
    )

    bottom:SetPoint(
        "BOTTOMLEFT"
    )

    bottom:SetPoint(
        "BOTTOMRIGHT"
    )

    bottom:SetHeight(2)

    --------------------------------------------------
    -- LEFT
    --------------------------------------------------

    local left = border:CreateTexture(
        nil,
        "OVERLAY"
    )

    left:SetColorTexture(
        1,
        1,
        1,
        1
    )

    left:SetPoint(
        "TOPLEFT"
    )

    left:SetPoint(
        "BOTTOMLEFT"
    )

    left:SetWidth(2)

    --------------------------------------------------
    -- RIGHT
    --------------------------------------------------

    local right = border:CreateTexture(
        nil,
        "OVERLAY"
    )

    right:SetColorTexture(
        1,
        1,
        1,
        1
    )

    right:SetPoint(
        "TOPRIGHT"
    )

    right:SetPoint(
        "BOTTOMRIGHT"
    )

    right:SetWidth(2)

    return border
end

--------------------------------------------------
-- UPDATE BORDER VISIBILITY
--------------------------------------------------

function addon:UpdateWorldMarkerIndicators()

    for markerIndex, button in pairs(
        self.worldMarkerButtons
    ) do

        if IsRaidMarkerActive(markerIndex) then
            button.border:Show()
        else
            button.border:Hide()
        end

    end
end

--------------------------------------------------
-- WORLD MARKER BUTTON
--------------------------------------------------

function addon:CreateWorldMarkerButton(
    parent,
    markerData
)

    local button = self:CreateButton(
        parent,
        22,
        22
    )

    --------------------------------------------------
    -- COLOR SQUARE
    --------------------------------------------------

    local square = button:CreateTexture(
        nil,
        "ARTWORK"
    )

    square:SetPoint(
        "TOPLEFT",
        4,
        -4
    )

    square:SetPoint(
        "BOTTOMRIGHT",
        -4,
        4
    )

    square:SetColorTexture(
        markerData.r,
        markerData.g,
        markerData.b,
        1
    )

    button.square = square

    --------------------------------------------------
    -- ACTIVE BORDER
    --------------------------------------------------

    button.border = CreateBorder(button)

    button.border:Hide()

    --------------------------------------------------
    -- SECURE WORLD MARKER ACTION
    --------------------------------------------------

    button:SetAttribute(
        "type",
        "worldmarker"
    )

    button:SetAttribute(
        "marker",
        markerData.marker
    )

    button:SetAttribute(
        "action",
        "set"
    )

    addon.worldMarkerButtons[
        markerData.marker
    ] = button

    return button
end

--------------------------------------------------
-- CLEAR ALL WORLD MARKERS
--------------------------------------------------

function addon:CreateWorldMarkerClearButton(parent)

    local button = self:CreateButton(
        parent,
        22,
        22
    )

    --------------------------------------------------
    -- X ICON
    --------------------------------------------------

    local icon = CreateFrame(
        "Frame",
        nil,
        button
    )

    icon:SetSize(
        12,
        12
    )

    icon:SetPoint(
        "CENTER"
    )

    --------------------------------------------------
    -- LINE 1
    --------------------------------------------------

    local line1 = icon:CreateTexture(
        nil,
        "OVERLAY"
    )

    line1:SetColorTexture(
        1,
        1,
        1,
        0.85
    )

    line1:SetSize(
        2,
        14
    )

    line1:SetPoint(
        "CENTER"
    )

    line1:SetRotation(
        math.rad(45)
    )

    --------------------------------------------------
    -- LINE 2
    --------------------------------------------------

    local line2 = icon:CreateTexture(
        nil,
        "OVERLAY"
    )

    line2:SetColorTexture(
        1,
        1,
        1,
        0.85
    )

    line2:SetSize(
        2,
        14
    )

    line2:SetPoint(
        "CENTER"
    )

    line2:SetRotation(
        math.rad(-45)
    )

    button.line1 = line1
    button.line2 = line2

    --------------------------------------------------
    -- SECURE CLEAR ACTION
    --------------------------------------------------

    button:SetAttribute(
        "type",
        "worldmarker"
    )

    button:SetAttribute(
        "action",
        "clear"
    )

    --------------------------------------------------
    -- HOVER
    --------------------------------------------------

    button:SetScript(
        "OnEnter",
        function(self)

            self.background:SetColorTexture(
                0.15,
                0.15,
                0.15,
                1
            )

            self.line1:SetColorTexture(
                1,
                1,
                1,
                1
            )

            self.line2:SetColorTexture(
                1,
                1,
                1,
                1
            )

        end
    )

    button:SetScript(
        "OnLeave",
        function(self)

            self.background:SetColorTexture(
                0,
                0,
                0,
                0
            )

            self.line1:SetColorTexture(
                1,
                1,
                1,
                0.85
            )

            self.line2:SetColorTexture(
                1,
                1,
                1,
                0.85
            )

        end
    )

    return button
end

--------------------------------------------------
-- WORLD MARKER EVENT
--------------------------------------------------

local eventFrame = CreateFrame(
    "Frame"
)

eventFrame:RegisterEvent(
    "RAID_TARGET_UPDATE"
)

eventFrame:SetScript(
    "OnEvent",
    function()

        -- Panel unsichtbar? Dann lohnt sich das Update nicht --
        -- CreateMainPanel() hängt einen OnShow-Handler an, der
        -- beim nächsten Sichtbarwerden einmal nachholt.
        if addon.panel and not addon.panel:IsShown() then
            return
        end

        if addon.UpdateWorldMarkerIndicators then
            addon:UpdateWorldMarkerIndicators()
        end

    end
)