-- read namespace from global env
local _G = _G
local Grichelde = _G.Grichelde or {}

local cPrefix, cGreen, cRed
    = Grichelde.F.cPrefix, Grichelde.F.cGreen, Grichelde.F.cRed


--- add Minimap button
function Grichelde:MinimapButton()
    local function clickHandler(_, button)
        if (button == "LeftButton") then
            self:ToggleOptions()
        elseif (button == "RightButton") then
            self:ToggleActivation()
        end
    end

    local function updateTooltip(tooltip)
        if (tooltip == nil) or (tooltip.AddLine == nil) then return end

        local tooltipTitle = self:Format(self.L.Minimap_Tooltip_Enabled, self.L.AddonName)
        if not self.db.profile.enabled then
            tooltipTitle = self:Format(self.L.Minimap_Tooltip_Disabled, self.L.AddonName)
        end

        tooltip:SetText(tooltipTitle,
            Grichelde.COLORS.HIGHLIGHT.r, Grichelde.COLORS.HIGHLIGHT.g, Grichelde.COLORS.HIGHLIGHT.b, Grichelde.COLORS.HIGHLIGHT.a
        )

        tooltip:AddDoubleLine(self.L.Minimap_Tooltip_Options_Left, self.L.Minimap_Tooltip_Options_Right,
            Grichelde.COLORS.GREEN.r, Grichelde.COLORS.GREEN.g, Grichelde.COLORS.GREEN.b, Grichelde.COLORS.GREEN.a,
            Grichelde.COLORS.NORMAL.r, Grichelde.COLORS.NORMAL.g, Grichelde.COLORS.NORMAL.b, Grichelde.COLORS.NORMAL.a
        )
        tooltip:AddDoubleLine(self.L.Minimap_Tooltip_Mappings_Left, self.L.Minimap_Tooltip_Mappings_Right,
            Grichelde.COLORS.GREEN.r, Grichelde.COLORS.GREEN.g, Grichelde.COLORS.GREEN.b, Grichelde.COLORS.GREEN.a,
            Grichelde.COLORS.NORMAL.r, Grichelde.COLORS.NORMAL.g, Grichelde.COLORS.NORMAL.b, Grichelde.COLORS.NORMAL.a
        )
    end

    local darkened = Grichelde.MINIMAP_ENABLED
    if (self.db.profile.enabled == false) then
        darkened = Grichelde.MINIMAP_DARKENDED
    end

    local ldb = LibStub("LibDataBroker-1.1"):NewDataObject(self.name, {
        type = "launcher",
        text = self.AddonName,
        icon = "Interface\\Icons\\Spell_Holy_Silence",
        --icon = ([[Interface\Addons\%s\%s]]):format(self.name, self.name),
        OnClick = clickHandler,
        OnRightClick = function() self:ShowMappings() end,
        OnTooltipShow = updateTooltip,
        iconR = darkened,
        iconG = darkened,
        iconB = darkened
    })

    local icon = LibStub("LibDBIcon-1.0")
    self:DebugPrint("MinimapButton : hidden: ", self.db.profile.minimapButton.hide)
    icon:Register(self.name, ldb, self.db.profile.minimapButton)

    return ldb, icon
end

function Grichelde:ToggleMinimapButton()
    self.db.profile.minimapButton.hide = not self.db.profile.minimapButton.hide
    self:DebugPrint("ToggleMinimapButton : hidden: ", self.db.profile.minimapButton.hide)

    if (self.db.profile.minimapButton.hide == true) then
        self:HideMinimapButton()
    else
        self:ShowMinimapButton()
    end
end

function Grichelde:ShowMinimapButton()
    if (self.icon ~= nil) then
        self.icon:Show(self.name)
    end
end

function Grichelde:HideMinimapButton()
    if (self.icon ~= nil) then
        self.icon:Hide(self.name)
    end
end

function Grichelde:ToggleActivation()
    if (self.db.profile.enabled == true) then
        if (self.dialog == nil) or (self.dialog.OpenFrames[self.name] == nil) then
            self:PrefixedPrint(self.L.Profiles_Deactivated, cRed(self.db:GetCurrentProfile()))
        end
        self:Deactivate()
    else
        if (self.dialog == nil) or (self.dialog.OpenFrames[self.name] == nil) then
            self:PrefixedPrint(self.L.Profiles_Activated, cGreen(self.db:GetCurrentProfile()))
        end
        self:Activate()
    end
end

function Grichelde:RefreshMinimap()
    if (self.db.profile.enabled == true) then
        self:Activate()
    else
        self:Deactivate()
    end
end

function Grichelde:Activate()
    self.db.profile.enabled = true

    -- refresh option UI if open at the moment
    if (self.dialog ~= nil) and (self.dialog.OpenFrames[self.name] ~= nil) then
        self.dialog:SelectGroup(self.name)
        local namePlusVersion = self:Format(self.L.AddonNamePlusVersion, self.L.AddonName, self.version)
        local statusText = self:Format(self.L.AddonLoaded, namePlusVersion)
        self.dialog.OpenFrames[self.name]:SetStatusText(statusText)
    end

    if (self.ldb ~= nil) then
        self.ldb.iconR = Grichelde.MINIMAP_ENABLED
        self.ldb.iconG = Grichelde.MINIMAP_ENABLED
        self.ldb.iconB = Grichelde.MINIMAP_ENABLED
    end
end

function Grichelde:Deactivate()
    self.db.profile.enabled = false

    -- refresh option UI if open at the moment
    if (self.dialog ~= nil) and (self.dialog.OpenFrames[self.name] ~= nil) then
        self.dialog:SelectGroup(self.name)
        local namePlusVersion = self:Format(self.L.AddonNamePlusVersion, self.L.AddonName, self.version)
        local statusText = self:Format(self.L.AddonUnloaded, namePlusVersion)
        self.dialog.OpenFrames[self.name]:SetStatusText(statusText)
    end

    if (self.ldb ~= nil) then
        self.ldb.iconR = Grichelde.MINIMAP_DARKENDED
        self.ldb.iconG = Grichelde.MINIMAP_DARKENDED
        self.ldb.iconB = Grichelde.MINIMAP_DARKENDED
    end
end

