-- Prevent redefinition if already loaded
if Common then return end
Common = {}

-----------------------------------------------------------
-- MUTE BUTTON
-----------------------------------------------------------
function Common.CreateMuteButton(args)
    local modelKey = args.modelKey
    local textures = args.textures or {}
    local x, y = args.x or 0, args.y or 0
    local size = args.size or 44
    local soundFile = args.soundOn

    return {
        name = "MuteButton",
        x = x,
        y = y,
        size = size,
        buttonType = "Button", -- plain button, no CheckButton behavior
        template = nil,
        tooltip = args.tooltip,
        highlight = textures.highlight,
        onClick = function(self)
            BuzzerSettings.muted = not BuzzerSettings.muted
            if self.UpdateIcon then self:UpdateIcon() end

            if BuzzerSettings.muted then
                
                if BuzzerSettings.enableSound then
                    PlaySoundFile("Interface\\AddOns\\Buzzer\\Media\\off.ogg", BuzzerSettings.soundChannel)
                end
                --print("Buzzer is turned off.")
                SendChatMessage("turns off their " .. Buzzer_Models[modelKey].name, "EMOTE")
            else
                
                if BuzzerSettings.enableSound then
                    print("Sound file: " .. soundFile)
                    PlaySoundFile(soundFile, BuzzerSettings.soundChannel)
                end
                print("Buzzer is turned on.")
                SendChatMessage("turns on their " .. Buzzer_Models[modelKey].name, "EMOTE")
            end
        end,
        onLoad = function(self)
            -- Icon
            if not self.icon then
                self.icon = self:CreateTexture(nil, "ARTWORK")
                self.icon:SetAllPoints(self)
            end

            

            -- Update logic
            self.UpdateIcon = function()
                if BuzzerSettings and BuzzerSettings.muted then
                    self.icon:SetTexture(textures.muted)
                    if self.Glow then self.Glow:Show() end
                else
                    self.icon:SetTexture(textures.unmuted)
                    if self.Glow then self.Glow:Hide() end
                end
            end

            self:UpdateIcon()
        end
    }
end

-----------------------------------------------------------
-- CLOSE BUTTON
-----------------------------------------------------------
function Common.CreateCloseButton(args)
    return {
        name = "CloseButton",
        x = args.x or 0,
        y = args.y or 0,
        size = args.size or 32,
        texture = args.texture or "Interface\\Buttons\\UI-Panel-MinimizeButton-Up",
        highlight = args.highlight or "Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight",
        pushed = args.pushed or "Interface\\Buttons\\UI-Panel-MinimizeButton-Down",
        onClick = function()
            BuzzerMainFrame:Hide()
        end
    }
end

-----------------------------------------------------------
-- OPTIONS BUTTON
-----------------------------------------------------------
function Common.CreateOptionsButton(args)
    return {
        name = args.name or "OptionsButton",
        x = args.x or 0,
        y = args.y or 0,
        size = args.size or 22,
        buttonType = "Button",
        template = nil, -- No Blizzard visuals
        texture = args.texture, -- optional background
        highlight = args.highlight or "Interface\\Buttons\\ButtonHilight-Square",
        tooltip = args.tooltip,
        onClick = args.onClick,
        onLoad = function(self)
            -- Remove default textures
            self:SetNormalTexture("")
            self:SetPushedTexture("")
            self:SetHighlightTexture("")

            -- Custom label
            if not self.Label then
                self.Label = self:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
                self.Label:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
                self.Label:SetPoint("CENTER", self, "CENTER", 0, 0)
                self.Label:SetText(args.txt or "?")
                self.Label:SetTextColor(unpack(args.txtCol or {1, 0.82, 0}))
            end

            -- Optional hover glow
            if not self.Glow then
                self.Glow = self:CreateTexture(nil, "OVERLAY")
                self.Glow:SetTexture("Interface\\Buttons\\UI-ActionButton-Border")
                self.Glow:SetBlendMode("ADD")
                self.Glow:SetAlpha(0.7)
                self.Glow:SetSize(self:GetWidth() * 1.4, self:GetHeight() * 1.4)
                self.Glow:SetPoint("CENTER", self, "CENTER", 0, 0)
                self.Glow:Hide()
            end
        end
    }
end



-----------------------------------------------------------
-- CHANNEL BUTTON
-----------------------------------------------------------
function Common.CreateChannelButton(index, args)
    return {
        name = "ChannelButton" .. index,
        x = args.x or 0,
        y = args.y or 0,
        size = args.size or 22,
        tooltip = "Change channel to " .. index,
        --texture = args.texture or "Interface\\Buttons\\UI-Quickslot2",
        texture = "",
        highlight = args.highlight or "Interface\\Buttons\\ButtonHilight-Square",
        label = tostring(index),
        
        onClick = function(self)
            BuzzerSettings.selectedChannel = index
            BuzzerSettings.channel = index
            print(string.format("New Buzzer Channel: |cff00ff00%d|r.", BuzzerSettings.channel or 1))
            Buzzer_UpdateChannelButtons()
            if BuzzerSettings.enableSound then
                local soundFile = "Interface\\AddOns\\Buzzer\\Media\\" .. index .. ".ogg"
                PlaySoundFile(soundFile, BuzzerSettings.soundChannel)
            end
        end,
        onLoad = function(self)
            if not self.Glow then
                self.Glow = self:CreateTexture(nil, "OVERLAY")
                self.Glow:SetTexture("Interface\\Buttons\\UI-ActionButton-Border")
                self.Glow:SetBlendMode("ADD")
                self.Glow:SetAlpha(0.7)
                self.Glow:SetSize(self:GetWidth() * 1.6, self:GetHeight() * 1.6)
                self.Glow:SetPoint("CENTER", self, "CENTER", 0, 0)
                self.Glow:Hide()
            end

            if not self.Label then
                self.Label = self:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
                self.Label:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
                self.Label:SetPoint("CENTER", self, "CENTER", 0, 0)
                self.Label:SetText(tostring(index))
                self.Label:SetTextColor(1, 1, 1)
            end
        end
    }
end

-- Make globally accessible
_G["Common"] = Common
