--[[
Author: Jarth
Filename: SummonAssistant_Buttons.lua
]] --

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local SummonAssistant = SummonAssistant

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-- Button highlight --
-------------------------------------------------------------------------------------------------
function SummonAssistant.ButtonHighlightEnter(frame)
    local highlightColor = SummonAssistant.Saved.HighlightColor
    local buttonBackdrop = GetControl(frame, "Backdrop")
    if buttonBackdrop ~= nil then
        PlaySound(SOUNDS.QUICKSLOT_MOUSEOVER)
        buttonBackdrop:SetEdgeColor(highlightColor.r, highlightColor.g, highlightColor.b, highlightColor.a)
    end
end

function SummonAssistant.ButtonHighlightExit(frame)
    local edgeColor = SummonAssistant.Saved.EdgeColor
    local buttonBackdrop = GetControl(frame, "Backdrop")
    if buttonBackdrop ~= nil then
        buttonBackdrop:SetEdgeColor(edgeColor.r, edgeColor.g, edgeColor.b, edgeColor.a)
    end
end

function SummonAssistant.ButtonOnClicked(button)
    local id = button:GetId()
    if id > 0 then
        if IsCollectibleUnlocked(id) then
            UseCollectible(id)
        else
            UseCollectible(GetActiveCollectibleByType(COLLECTIBLE_CATEGORY_TYPE_ASSISTANT))
        end
    end
end

-------------------------------------------------------------------------------------------------
-- PRIVATE FUNCTIONS --
-------------------------------------------------------------------------------------------------
function SummonAssistant:SetupButton(button, left, top)
    button:ClearAnchors()
    button:SetAnchor(TOPLEFT, SummonAssistant_Frame, TOPLEFT, left, top)
    button:SetHeight(SummonAssistant.Saved.Height)
    button:SetWidth(SummonAssistant.Saved.Width)
    button:SetHidden(false)
end

function SummonAssistant:SetupButtonTexture(button, key, left, top)
    local buttonTexture = GetControl(button, "Texture")
    if buttonTexture ~= nil then
        button:SetHandler("OnClicked", SummonAssistant.ButtonOnClicked)
        buttonTexture:SetDrawLevel(3)
        buttonTexture:SetTexture(SummonAssistant.Types[key].EnabledTexture)
        buttonTexture:ClearAnchors()
        buttonTexture:SetAnchor(TOPLEFT, SummonAssistant_Frame, TOPLEFT, left + SummonAssistant.Saved.Margin, top + SummonAssistant.Saved.Margin)
        buttonTexture:SetHeight(button:GetHeight() - (2 * SummonAssistant.Saved.Margin))
        buttonTexture:SetWidth(button:GetWidth() - (2 * SummonAssistant.Saved.Margin))
    end
end

function SummonAssistant:SetupButtonBackdrop(button, key, left, top)
    local buttonBackdrop = GetControl(button, "Backdrop")
    if buttonBackdrop ~= nil then
        buttonBackdrop:SetDrawLevel(2)
        buttonBackdrop:SetEdgeTexture(nil, 1, 1, SummonAssistant.Saved.Margin)
        buttonBackdrop:ClearAnchors()
        buttonBackdrop:SetAnchor(TOPLEFT, SummonAssistant_Frame, TOPLEFT, left, top)
        buttonBackdrop:SetHeight(button:GetHeight())
        buttonBackdrop:SetWidth(button:GetWidth())
    end
end

function SummonAssistant:SetButtonFrameWidth()
    local count, show, height, width = 0, 0, 0, 0

    for key, value in pairs(SummonAssistant.Saved.ShowAssistants) do
        if IsCollectibleUnlocked(key) and SummonAssistant.Saved.ShowAssistants[key] then
            count = count + 1
        end
    end

    if count > 0 then
        show = 1
    end
    height = SummonAssistant.Saved.Height * (SummonAssistant.Saved.Horizontal and show or count)
    width = SummonAssistant.Saved.Width * (SummonAssistant.Saved.Horizontal and count or show)

    SummonAssistant:SetFrameSettings(SummonAssistant_Frame, count == 0, height, width)
    SummonAssistant:SetFrameSettings(SummonAssistant_FrameBackdrop, count == 0, height, width)
end

function SummonAssistant:SetFrameSettings(frame, isHidden, height, width)
    if frame ~= nil then
        frame:SetHidden(count == 0)
        frame:SetHeight(height)
        frame:SetWidth(width)
    end
end

function SummonAssistant:InitializeButtons()
    local index = 1
    for _, _value in ipairs(SummonAssistant.OrderedTypes) do
        local id = _value.Id
        local key = _value.Name
        local left, top = SummonAssistant:GetButtonPosition(index)
        if SummonAssistant.Saved.ShowAssistants[id] and IsCollectibleUnlocked(_value.Id) then
            if SummonAssistant.Buttons[key] == nil then
                SummonAssistant.Buttons[key] = WINDOW_MANAGER:CreateControlFromVirtual("SummonAssistant_Button", SummonAssistant_Frame, "SummonAssistant_Button", key)
                SummonAssistant.Buttons[key]:SetId(id)
            end
            SummonAssistant:SetupButton(SummonAssistant.Buttons[key], left, top)
            SummonAssistant:SetupButtonBackdrop(SummonAssistant.Buttons[key], key, left, top)
            SummonAssistant:SetupButtonTexture(SummonAssistant.Buttons[key], key, left, top)
            SummonAssistant.Buttons[key]:SetHandler("OnMouseEnter", SummonAssistant.ButtonHighlightEnter)
            SummonAssistant.Buttons[key]:SetHandler("OnMouseExit", SummonAssistant.ButtonHighlightExit)
            index = index + 1
        elseif SummonAssistant.Buttons[key] ~= nil then
            SummonAssistant.Buttons[key]:SetHidden(true)
        end
    end
end

function SummonAssistant:GetButtonPosition(index)
    local left = 0
    local top = SummonAssistant.Saved.Height * (index - 1)
    if SummonAssistant.Saved.Horizontal then
        left = SummonAssistant.Saved.Width * (index - 1)
        top = 0
    end
    return left, top
end

function SummonAssistant:ButtonsBackdropColor()
    local centerColor = SummonAssistant.Saved.CenterColor
    local edgeColor = SummonAssistant.Saved.EdgeColor

    for _, _value in ipairs(SummonAssistant.OrderedTypes) do
        if SummonAssistant.Buttons[_value.Name] ~= nil then
            local buttonBackdrop = GetControl(SummonAssistant.Buttons[_value.Name], "Backdrop")
            if buttonBackdrop ~= nil then
                buttonBackdrop:SetCenterColor(centerColor.r, centerColor.g, centerColor.b, centerColor.a)
                buttonBackdrop:SetEdgeColor(edgeColor.r, edgeColor.g, edgeColor.b, edgeColor.a)
            end
        end
    end
end