local _, Addon = ...

local Commands = {}
Addon.Commands = Commands
local L = Addon.L

function Commands:PrintHelp()
    local keys = {
        "HELP_HEADER", "HELP_TOGGLE", "HELP_CONFIG", "HELP_SIZE", "HELP_SHAPE",
        "HELP_BORDER", "HELP_BUTTONS", "HELP_ICON_SIZE", "HELP_ICON_GAP",
        "HELP_COLUMNS", "HELP_HUB_RESET", "HELP_PROFILE", "HELP_ARROW",
        "HELP_ALPHA", "HELP_LOCK", "HELP_REPLACE", "HELP_MINIMAP_BUTTON",
        "HELP_ENEMIES", "HELP_GATHERING", "HELP_ROUTE", "HELP_ROUTE_RESET",
        "HELP_DEBUG", "HELP_RESET",
    }
    for _, key in ipairs(keys) do
        Addon:Print(L[key])
    end
end

function Commands:Handle(message)
    local trimmed = strtrim(message or "")
    local command, argument = strsplit(" ", trimmed, 2)
    command = string.lower(command or "")

    if command == "" or command == "toggle" then
        Addon.UI:ToggleMap()
    elseif command == "help" or command == "ayuda" then
        self:PrintHelp()
    elseif command == "config" or command == "opciones" or command == "options" then
        Addon.Options:Toggle()
    elseif command == "reset" then
        Addon.UI:ResetSettings()
    elseif command == "lock" or command == "bloquear" then
        Addon.db.locked = not Addon.db.locked
        Addon:Print(Addon.db.locked and L.WINDOW_LOCKED or L.WINDOW_UNLOCKED)
    elseif command == "replace" or command == "reemplazar" then
        Addon.UI:ToggleReplacementMode(true)
    elseif command == "forma" or command == "shape" then
        local shape = argument and string.lower(strtrim(argument))
        if shape == "circular" or shape == "circulo" or shape == "círculo" or shape == "circle" then
            Addon.UI:SetShape("circle")
            Addon:Print(L.CIRCLE_APPLIED)
        elseif shape == "cuadrada" or shape == "cuadrado" or shape == "square" then
            Addon.UI:SetShape("square")
            Addon:Print(L.SQUARE_APPLIED)
        else
            Addon:Print(L.SHAPE_USAGE)
        end
    elseif command == "circular" or command == "circulo" or command == "círculo" then
        Addon.UI:SetShape("circle")
        Addon:Print(L.CIRCLE_APPLIED)
    elseif command == "cuadrado" or command == "cuadrada" then
        Addon.UI:SetShape("square")
        Addon:Print(L.SQUARE_APPLIED)
    elseif command == "marco" or command == "border" then
        local value = tonumber(argument)
        if not value then
            Addon:Print(L.BORDER_USAGE)
            return
        end
        value = Addon:Clamp(math.floor(value + 0.5), Addon.MIN_BORDER_SIZE, Addon.MAX_BORDER_SIZE)
        Addon.UI:SetBorderSize(value)
        Addon:Print(Addon:Format("BORDER_VALUE", value))
    elseif command == "botones" or command == "addonbuttons" then
        local option = argument and string.lower(strtrim(argument))
        local shown
        if option == "on" or option == "si" or option == "sí" or option == "mostrar" then
            shown = true
        elseif option == "off" or option == "no" or option == "ocultar" then
            shown = false
        else
            shown = not Addon.db.showAddonButtons
        end
        Addon.UI:SetAddonButtonsShown(shown)
        Addon:Print(shown and L.ADDON_BUTTONS_VISIBLE or L.ADDON_BUTTONS_HIDDEN)
    elseif command == "iconos" or command == "addonsize" then
        local value = tonumber(argument)
        if not value then
            Addon:Print(L.ICON_SIZE_USAGE)
            return
        end
        Addon.db.addonButtonSize = Addon:Clamp(
            math.floor(value + 0.5),
            Addon.MIN_ADDON_BUTTON_SIZE,
            Addon.MAX_ADDON_BUTTON_SIZE
        )
        Addon.AddonButtons:Layout()
        Addon:Print(Addon:Format("ICON_SIZE_VALUE", Addon.db.addonButtonSize))
    elseif command == "distancia" or command == "buttondistance" then
        local value = tonumber(argument)
        if not value then
            Addon:Print(L.ICON_GAP_USAGE)
            return
        end
        Addon.db.addonButtonDistance = Addon:Clamp(
            math.floor(value + 0.5),
            Addon.MIN_BUTTON_DISTANCE,
            Addon.MAX_BUTTON_DISTANCE
        )
        Addon.AddonButtons:Layout()
        Addon:Print(Addon:Format("ICON_GAP_VALUE", Addon.db.addonButtonDistance))
    elseif command == "columnas" or command == "columns" then
        local value = tonumber(argument)
        if not value then
            Addon:Print(L.COLUMNS_USAGE)
            return
        end
        Addon.db.addonHubColumns = Addon:Clamp(
            math.floor(value + 0.5),
            Addon.MIN_HUB_COLUMNS,
            Addon.MAX_HUB_COLUMNS
        )
        Addon.AddonButtons:Layout()
        Addon:Print(Addon:Format("COLUMNS_VALUE", Addon.db.addonHubColumns))
    elseif command == "moverboton" or command == "hubreset" then
        Addon.AddonButtons:ResetAnchor()
    elseif command == "perfil" or command == "profile" then
        local profile = argument and string.lower(strtrim(argument))
        if not Addon.UI:ApplyProfile(profile) then
            Addon:Print(L.PROFILE_USAGE)
            return
        end
        Addon:Print(Addon:Format("PROFILE_APPLIED", profile))
    elseif command == "button" or command == "boton" or command == "botón" then
        Addon.db.showButton = not Addon.db.showButton
        Addon.UI:SetMinimapButtonShown(Addon.db.showButton)
        Addon:Print(
            Addon.db.showButton
                and L.MINIMAP_BUTTON_VISIBLE
                or L.MINIMAP_BUTTON_HIDDEN
        )
    elseif command == "adds" or command == "enemigos" then
        local option = argument and string.lower(strtrim(argument))
        local shown
        if option == "on" or option == "si" or option == "sí" or option == "mostrar" then
            shown = true
        elseif option == "off" or option == "no" or option == "ocultar" then
            shown = false
        else
            shown = not Addon.db.showAdds
        end
        Addon.Markers:SetAddsShown(shown)
        Addon:Print(shown and L.ENEMIES_VISIBLE or L.ENEMIES_HIDDEN)
    elseif command == "recoleccion" or command == "recolección"
        or command == "gather" or command == "gathering"
    then
        Addon.Gathering:Toggle(true, true)
    elseif command == "ruta" or command == "route" or command == "importar" then
        local option = argument and string.lower(strtrim(argument))
        if option == "on" or option == "si" or option == "sí" or option == "mostrar" then
            if not Addon.Routes:HasRoutes() then
                Addon.Routes:OpenImportDialog()
            else
                Addon.Routes:SetVisible(true)
                Addon:Print(L.ROUTE_VISIBLE_MESSAGE)
            end
        elseif option == "off" or option == "no" or option == "ocultar" then
            Addon.Routes:SetVisible(false)
            Addon:Print(L.ROUTE_HIDDEN_MESSAGE)
        elseif option == "reset" or option == "reiniciar" or option == "bajas" then
            local reset = Addon.Routes:ResetProgress()
            Addon:Print(reset and L.KILL_PROGRESS_RESET or L.NO_ROUTE_SELECTED_LOWER)
        elseif option == "borrar" or option == "clear" then
            local removed = Addon.Routes:Clear()
            Addon:Print(removed and Addon:Format("ROUTE_DELETED_LOWER", removed.name) or L.NO_ROUTE_SELECTED_LOWER)
        else
            Addon.Routes:OpenImportDialog()
        end
    elseif command == "debug" then
        local mapID = Addon.state.mapID or C_Map.GetBestMapForUnit("player")
        for _, line in ipairs(Addon.Coordinates:GetDiagnostics(mapID, "player")) do
            Addon:Print(line)
        end
        Addon:Print(Addon:Format(
            "DEBUG_NATIVE_FRAME",
            Addon.Markers:IsNativePlayerFrameAvailable() and L.AVAILABLE or L.UNAVAILABLE
        ))
        local shape = Addon.db.shape == "circle" and L.SHAPE_CIRCLE or L.SHAPE_SQUARE
        Addon:Print(Addon:Format("DEBUG_SHAPE_BUTTONS", shape, Addon.AddonButtons:GetVisibleCount()))
        Addon:Print(Addon:Format("DEBUG_CENTER", Addon:GetCenterModeLabel(Addon.state.protectedCenterMode)))
    elseif command == "flecha" or command == "arrowsize" then
        local value = tonumber(argument)
        if not value then
            Addon:Print(L.ARROW_USAGE)
            return
        end
        Addon.db.playerArrowSize = Addon:Clamp(
            math.floor(value + 0.5),
            Addon.MIN_ARROW_SIZE,
            Addon.MAX_ARROW_SIZE
        )
        Addon.Markers:SetPlayerArrowSize(Addon.db.playerArrowSize)
        Addon:Print(Addon:Format("ARROW_VALUE", Addon.db.playerArrowSize))
    elseif command == "size" or command == "tamano" or command == "tamaño" then
        local value = tonumber(argument)
        if not value then
            Addon:Print(L.SIZE_USAGE)
            return
        end
        local minimumSize = Addon.db.shape == "circle" and Addon.MIN_SIZE or Addon.MIN_SQUARE_SIZE
        Addon.db.size = Addon:Clamp(math.floor(value + 0.5), minimumSize, Addon.MAX_SIZE)
        Addon.UI:SetSize(Addon.db.size)
        Addon:Print(Addon:Format("SIZE_VALUE", Addon.db.size))
    elseif command == "alpha" or command == "opacidad" then
        local normalized = argument and string.gsub(argument, ",", ".")
        local value = tonumber(normalized)
        if not value then
            Addon:Print(L.ALPHA_USAGE)
            return
        end
        Addon.db.alpha = Addon:Clamp(value, Addon.MIN_ALPHA, Addon.MAX_ALPHA)
        Addon.UI:SetAlpha(Addon.db.alpha)
        Addon:Print(Addon:Format("ALPHA_VALUE", Addon.db.alpha))
    else
        Addon:Print(L.UNKNOWN_COMMAND)
    end
end

function Commands:Register()
    SLASH_MAPAENMINIATURA1 = "/mzm"
    SLASH_MAPAENMINIATURA2 = "/minimapa"
    SLASH_MAPAENMINIATURA3 = "/miyamap"
    SlashCmdList.MAPAENMINIATURA = function(message)
        self:Handle(message)
    end
end
