local Module = SUI:NewModule("ActionBars.Buttons");

function Module:RefreshText()
    local FONT = STANDARD_TEXT_FONT
    local db = SUI.db.profile.actionbar.buttons

    local function UpdateHotkeys(Button)
        if not Button or not Button.GetName then
            return
        end

        local Name = Button:GetName()
        local HotKey = _G[Name .. "HotKey"]
        local Macro = _G[Name .. "Name"]
        local Count = _G[Name .. "Count"]

        if HotKey then
            HotKey:SetFont(FONT, db.size, "OUTLINE")
            HotKey:SetAlpha(db.key and 1 or 0)
        end

        if Macro then
            Macro:SetFont(FONT, db.size, "OUTLINE")
            Macro:SetAlpha(db.macro and 1 or 0)
        end

        if Count then
            Count:SetFont(FONT, db.size, "OUTLINE")
        end
    end

    local bars = {
        _G["MainActionBar"],
        _G["MultiBarBottomLeft"],
        _G["MultiBarBottomRight"],
        _G["MultiBarRight"],
        _G["MultiBarLeft"],
        _G["MultiBar5"],
        _G["MultiBar6"],
        _G["MultiBar7"],
    }

    for _, bar in ipairs(bars) do
        if bar and bar.numButtonsShowable then
            local prefix = bar == _G["MainActionBar"] and "ActionButton" or (bar:GetName() .. "Button")
            for i = 1, bar.numButtonsShowable do
                UpdateHotkeys(_G[prefix .. i])
            end
        end
    end

    for i = 1, 10 do
        UpdateHotkeys(_G["StanceButton" .. i])
        UpdateHotkeys(_G["PetActionButton" .. i])
        UpdateHotkeys(_G["DominosPetActionButton" .. i])
        UpdateHotkeys(_G["DominosStanceButton" .. i])
        UpdateHotkeys(_G["BT4PetButton" .. i])
        UpdateHotkeys(_G["BT4StanceButton" .. i])
    end

    for i = 1, 140 do
        UpdateHotkeys(_G["DominosActionButton" .. i])
    end

    for i = 1, 180 do
        UpdateHotkeys(_G["BT4Button" .. i])
    end
end

function Module:OnEnable()

    if (SUI:Color()) then
        local EventFrame = CreateFrame("Frame")
        local FONT = STANDARD_TEXT_FONT
        local dominos = C_AddOns.IsAddOnLoaded("Dominos")
        local bartender = C_AddOns.IsAddOnLoaded("Bartender4")
        local db = { 
            buttons = SUI.db.profile.actionbar.buttons,
            menu = SUI.db.profile.actionbar.menu
        }

        local Bars = {
            _G["MultiBarBottomLeft"],
            _G["MultiBarBottomRight"],
            _G["MultiBarRight"],
            _G["MultiBarLeft"],
            _G["MultiBarRight"],
            _G["MultiBar5"],
            _G["MultiBar6"],
            _G["MultiBar7"],
        }

        local function UpdateHotkeys(Button)
            local Name = Button:GetName()
            local HotKey = _G[Name .. "HotKey"]
            local Macro = _G[Name .. "Name"]
            local Count = _G[Name .. "Count"]

            HotKey:SetFont(FONT, db.buttons.size, "OUTLINE")
            Macro:SetFont(FONT, db.buttons.size, "OUTLINE")
            Count:SetFont(FONT, db.buttons.size, "OUTLINE")

            local HotKeyAlpha = db.buttons.key and 1 or 0
            local MacroAlpha = db.buttons.macro and 1 or 0

            HotKey:SetAlpha(HotKeyAlpha)
            Macro:SetAlpha(MacroAlpha)
        end

        local function StyleButton(Button, Type)
            local Name = Button:GetName()
            local NormalTexture = _G[Name .. "NormalTexture"]
            local Icon = _G[Name .. "Icon"]
            local Cooldown = _G[Name .. "Cooldown"]

            if Button.Shadow == nil then
                NormalTexture:SetDesaturated(true)
                NormalTexture:SetVertexColor(unpack(SUI:Color(0.15)))

                Cooldown:ClearAllPoints()
                Cooldown:SetPoint("TOPLEFT", Button, "TOPLEFT", 2, -2.5)
                Cooldown:SetPoint("BOTTOMRIGHT", Button, "BOTTOMRIGHT", -3, 3)

                Icon:SetTexCoord(.08, .92, .08, .92)

                if bartender then
                    local ButtonWidth, ButtonHeight = Button:GetSize()
                    Button:GetNormalTexture():SetSize(ButtonWidth + 2, ButtonHeight + 1)

                   if Type ~= "Stance" and Type ~= "Pet" then
                        Button:GetNormalTexture():SetTexCoord(0, 1, 0, 1)
                        Button:GetNormalTexture():SetSize(ButtonWidth + 6, ButtonHeight + 5)
                   end
                end
            end
        end

        local function StyleAction(Bar, Num)
            for i = 1, Num do
                local Name = Bar:GetName()
                local Button = _G[Name .. "Button" .. i]

                StyleButton(Button, "Actionbar")
                UpdateHotkeys(Button)
            end
        end

        local function StyleMainActionBarChildFrames()
            local mainActionBar = _G["MainActionBar"]
            if not mainActionBar then
                return
            end

            for _, child in pairs({ mainActionBar:GetChildren() }) do
                if child then
                    local topEdge = child.TopEdge
                    local bottomEdge = child.BottomEdge
                    local center = child.Center

                    if topEdge and topEdge.SetDesaturated and topEdge.SetVertexColor then
                        topEdge:SetDesaturated(true)
                        topEdge:SetVertexColor(unpack(SUI:Color(0.15)))
                    end

                    if bottomEdge and bottomEdge.SetDesaturated and bottomEdge.SetVertexColor then
                        bottomEdge:SetDesaturated(true)
                        bottomEdge:SetVertexColor(unpack(SUI:Color(0.15)))
                    end

                    if center and center.SetDesaturated and center.SetVertexColor then
                        center:SetDesaturated(true)
                        center:SetVertexColor(unpack(SUI:Color(0.15)))
                    end
                end
            end
        end

        local function Init()
            for j = 1, #Bars do
                local Bar = Bars[j]
                if Bar then
                    local Num = Bar.numButtonsShowable
                    StyleAction(Bar, Num)
                end
            end

            local DefaultActionBarShowable = _G["MainActionBar"].numButtonsShowable

            for i = 1, DefaultActionBarShowable do
                local Button = _G["ActionButton" .. i]

                UpdateHotkeys(Button)

                if C_AddOns.IsAddOnLoaded("Masque") and C_AddOns.IsAddOnLoaded("MasqueBlizzBars") then return end
                StyleButton(Button, "Actionbar")

            end

            for i = 1, 10 do
                local StanceButton = _G["StanceButton" .. i]
                local PetButton = _G["PetActionButton" .. i]

                StyleButton(StanceButton, "StanceOrPet")
                StyleButton(PetButton, "StanceOrPet")
            end

            StyleMainActionBarChildFrames()
        end

        if dominos then
            if C_AddOns.IsAddOnLoaded("Masque") then return end
            for i = 1, 140 do
                local ActionButton = _G["DominosActionButton" .. i]
                if ActionButton then
                    StyleButton(ActionButton)
                end
            end

            for i = 1, 10 do
                local PetButton = _G["DominosPetActionButton" .. i]
                local StanceButton = _G["DominosStanceButton" .. i]

                if PetButton then
                    StyleButton(PetButton)
                end

                if StanceButton then
                    StyleButton(StanceButton)
                end
            end
        end

        if bartender then
            if C_AddOns.IsAddOnLoaded("Masque") then return end
            for i = 1, 180 do
                local ActionButton = _G["BT4Button" .. i]
                if ActionButton then
                    StyleButton(ActionButton)
                end
            end

            for i = 1, 10 do
                local PetButton = _G["BT4PetButton" .. i]
                local StanceButton = _G["BT4StanceButton" .. i]

                if PetButton then
                    StyleButton(PetButton, "Pet")
                end

                if StanceButton then
                    StyleButton(StanceButton, "Stance")
                end
            end
        end

        Init()
        self:RefreshText()
    end
end
