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

function Module:OnEnable()

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

    local Backdrop = {
      bgFile = "",
      edgeFile = "Interface\\Addons\\SUI\\Media\\Textures\\Core\\outer_shadow",
      tile = false,
      tileSize = 32,
      edgeSize = 5,
      insets = { left = 5, right = 5, top = 5, bottom = 5 }
    }

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

    EventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
    EventFrame:RegisterEvent("UPDATE_BINDINGS")

    if IsAddOnLoaded("Masque") and (dominos or bartender) then return end

    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["MainMenuBar"].numButtonsShowable

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

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

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

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

    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

    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", 1, -2)
        Cooldown:SetPoint("BOTTOMRIGHT", Button, "BOTTOMRIGHT", -2, 2)

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

        Button:SetNormalTexture("Interface\\Addons\\SUI\\Media\\Textures\\Core\\UIActionBar")
        Button:GetNormalTexture():SetTexCoord(0.701171875, 0.880859375, 0.31689453125, 0.36083984375)

        local ButtonWidth, ButtonHeight = Button:GetSize()
        Button:GetNormalTexture():SetSize(ButtonWidth + 2, ButtonHeight + 1)
        Button.Shadow = CreateFrame("Frame", nil, Button, "BackdropTemplate")
        Button.Shadow:SetPoint("TOPLEFT", Button, "TOPLEFT", -3, 3)
        Button.Shadow:SetPoint("BOTTOMRIGHT", Button, "BOTTOMRIGHT", 2, -2)
        Button.Shadow:SetFrameLevel(Button:GetFrameLevel() - 1)
        Button.Shadow:SetBackdrop(Backdrop)
        Button.Shadow:SetBackdropBorderColor(unpack(SUI:Color(0.15)))
        --Button.Shadow:SetBackdropBorderColor(0, 0, 0)
      end
    end

    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, 14, "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

    EventFrame:SetScript("OnEvent", Init)
  end
end