local _, ns = ...
local MythicTools = ns.MythicTools

function MythicTools:InitializeSlashCommands()
    SLASH_SKYMYTHICHISTORY1 = "/smh"

    SlashCmdList.SKYMYTHICHISTORY = function(message)
        MythicTools:HandleSlashCommand(message)
    end
end

function MythicTools:PrintHelp()
    self:Print(self:L("SLASH_OPEN"))
    self:Print(self:L("SLASH_ME"))
    self:Print(self:L("SLASH_PLAYER"))
    self:Print(self:L("SLASH_TEST"))
    self:Print(self:L("SLASH_SAVE"))
    self:Print(self:L("SLASH_DEBUG"))
    self:Print(self:L("SLASH_DEBUG_TOGGLE"))
    self:Print(self:L("SLASH_DEBUG_LAST"))
    self:Print(self:L("SLASH_DEBUG_CLEAR"))
end

function MythicTools:HandleSlashCommand(message)
    message = self:TrimText(message)
    if message == "" then
        self:ToggleMainFrame()
        return
    end

    local command, rest = message:match("^(%S+)%s*(.-)$")
    command = command and command:lower() or ""

    if command == "help" then
        self:PrintHelp()
        return
    end

    if command == "player" and rest and rest ~= "" then
        self:OpenPlayerHistory(rest)
        return
    end

    if command == "save" then
        if not (self.SaveHistoryToDisk and self:SaveHistoryToDisk()) then
            self:Print(self:L("AUTO_SAVE_UNAVAILABLE"))
        end
        return
    end

    if command == "me" or command == "self" then
        if self.OpenCurrentCharacterDetail and self:OpenCurrentCharacterDetail() then
            return
        end
        self:Print(self:L("CHARACTER_NOT_FOUND"))
        return
    end

    if command == "test" then
        local testMode = rest and self:TrimText(rest):lower() or ""
        if self.ShowTestCompletionPopup then
            if testMode == "compact" or testMode == "toast" then
                self:ShowTestCompletionPopup("compact")
            elseif testMode == "full" or testMode == "popup" then
                self:ShowTestCompletionPopup("full")
            else
                self:ShowTestCompletionPopup()
            end
        end
        return
    end

    if command == "debug" then
        local subcommand = rest:match("^(%S+)")
        subcommand = subcommand and subcommand:lower() or "status"

        if subcommand == "on" then
            self.db.debug.enabled = true
            self:Print(self:L("DEBUG_ENABLED"))
            return
        end

        if subcommand == "off" then
            self.db.debug.enabled = false
            self:Print(self:L("DEBUG_DISABLED"))
            return
        end

        if subcommand == "clear" then
            self.db.debug.reports = {}
            self:Print(self:L("DEBUG_CLEARED"))
            return
        end

        if subcommand == "last" then
            if self.PrintLatestDebugReport then
                self:PrintLatestDebugReport()
            end
            return
        end

        if subcommand == "status" or subcommand == "" then
            local reportCount = #(self.db.debug.reports or {})
            self:Print(self:L("DEBUG_STATUS", self.db.debug.enabled ~= false and "ON" or "OFF", reportCount))
            self:Print(self:L("DEBUG_OPTIONS"))
            return
        end

        self:Print(self:L("UNKNOWN_DEBUG_SUBCOMMAND", subcommand))
        self:Print(self:L("HELP_HINT"))
        return
    end

    self:Print(self:L("UNKNOWN_COMMAND", command))
    self:Print(self:L("HELP_HINT"))
end
