--[[---------------------------------------------------------------------------

  Grichelde - Text Replacer

  Copyright 2020 Teilzeit-Jedi <tj@teilzeit-jedi.de>

  This addon is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with the addon. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.

-----------------------------------------------------------------------------]]

-- read namespace from global env
local AddonName, AddonTable = ...
local _G = _G

-- initialize addon
local Grichelde = LibStub("AceAddon-3.0"):NewAddon(AddonTable, AddonName, "AceConsole-3.0", "AceEvent-3.0", "AceHook-3.0")
Grichelde.version = GetAddOnMetadata(AddonName, "Version")
Grichelde.build = GetAddOnMetadata(AddonName, "X-Build") or "Development"
--Grichelde.era = _G.WOW_PROJECT_ID == _G.WOW_PROJECT_CLASSIC
--Grichelde.bcc = _G.WOW_PROJECT_ID == _G.WOW_PROJECT_BURNING_CRUSADE_CLASSIC
Grichelde.logLevel = 0      -- cannot reference Grichelde.LOG_LEVELs here as they are loaded afterwards

-- publish to global env
_G[AddonName] = Grichelde

-- Ace3 callbacks
function Grichelde:OnInitialize()
    self.L = LibStub("AceLocale-3.0"):GetLocale(self.name, true)

    -- Build Interface Options window
    self.db = self:LoadDatabase()
    self:UpgradeDatabase()
end

function Grichelde:OnEnable()
    -- Hook in before message is sent to replace all character occurrences where replacements have been defined in the options
    self:RawHook("SendChatMessage", true)

    self.options, self.dialog = self:SetupOptions()
    self.ldb, self.icon = self:MinimapButton()

    self:RefreshProfiles("OnEnable")
    self:SetupSlashCommands()

    -- tell the world we are listening
    if (self.db.profile.enabled == true) then
        local namePlusVersion = self:Format(self.L.AddonNamePlusVersion, self.L.AddonName, self.version)
        self.F.print(self:Format(self.L.AddonLoaded, self.COLOR_CODES.PREFIX .. namePlusVersion .. self.COLOR_CODES.CLOSE))
    end
end

function Grichelde:OnDisable()
    self:Unhook("SendChatMessage")
    self:CloseOptions()
    self:HideMinimapButton()
    self:UnregisterChatCommand("grichelde")
    self:UnregisterChatCommand("gri")
end

--- Register  slash commands 'gri' and 'grichelde'
function Grichelde:SetupSlashCommands()
    self:RegisterChatCommand("grichelde", "HandleSlashCommand")
    self:RegisterChatCommand("gri", "HandleSlashCommand")
end

function Grichelde:HandleSlashCommand(input, ...)
    -- Show the GUI if no input is supplied, otherwise handle the chat input.
    if (self.F.nilOrEmpty(input)) then
        self:ToggleOptions()
    else
        -- handle slash ourselves
        self:DebugPrint("Handle slash command: " .. input)
        if input == "mappings" then
            self:ToogleMappings()
        elseif input == "options" then
            self:ToggleOptions()
        elseif input == "profile" then
            self:PrintProfile()
        elseif input == "on" or input == "enable" then
             self:Activate()
        elseif input == "off" or input == "disable" then
             self:Deactivate()
        else
            self:SendChatMessageOverride(input, ...)
        end
    end
end