-- Copyright © 2024 @Groick (Twitch). All rights reserved.

-- EventFrame
local m_EventFrame = EasyOptions:CreateEventFrame("EasyOptions.EventFrame", nil)
m_EventFrame:RegisterEvent("ADDON_LOADED")

-- Slash Command Functions
local function ShowOptions()
  EasyOptions:ShowOptions("EasyOptions")
end
local function SaveAddonSettingsHandler(msg, editbox)
  local areValidArgs, addonName, saveName = EasyOptions:GetSlashCommandArgsForManageAddonSettings(msg)
  if (not areValidArgs) then
    EasyOptions:SaveAddonSettings_HandleInvalidArgs()
    return
  end
  EasyOptions:SaveAddonSettings(addonName, saveName, EasyOptionsDB["PromptBeforeOverwritingExistingSave_EnablePromptIfInitiatedBySlashCommand"], EasyOptionsDB["PrintMessageAfterSave_EnableMessageIfInitiatedBySlashCommand"])
end
local function LoadAddonSettingsHandler(msg, editbox)
  local areValidArgs, addonName, saveName = EasyOptions:GetSlashCommandArgsForManageAddonSettings(msg)
  if (not areValidArgs) then
    EasyOptions:LoadAddonSettings_HandleInvalidArgs()
    return
  end
  EasyOptions:LoadAddonSettings(addonName, saveName, EasyOptionsDB["PromptBeforeLoad_EnablePromptIfInitiatedBySlashCommand"], EasyOptionsDB["PrintMessageAfterLoad_EnableMessageIfInitiatedBySlashCommand"])
end
local function DeleteAddonSettingsHandler(msg, editbox)
  local areValidArgs, addonName, saveName = EasyOptions:GetSlashCommandArgsForManageAddonSettings(msg)
  if (not areValidArgs) then
    EasyOptions:DeleteAddonSettings_HandleInvalidArgs()
    return
  end
  EasyOptions:DeleteAddonSettings(addonName, saveName, EasyOptionsDB["PromptBeforeDelete_EnablePromptIfInitiatedBySlashCommand"], EasyOptionsDB["PrintMessageAfterDelete_EnableMessageIfInitiatedBySlashCommand"])
end

-- Event Handlers
function m_EventFrame.Events.ADDON_LOADED(addon)
  if (addon ~= "EasyOptions") then
    return
  end

  -- Initialize Options
  EasyOptions:InitializeOptions()

  -- Register Slash Commands
  SlashCmdList["EasyOptions"] = ShowOptions
  SLASH_EasyOptions1 = "/EasyOptions"
  SLASH_EasyOptions2 = "/EO"
  SlashCmdList["EasyOptions_Save"] = SaveAddonSettingsHandler
  SLASH_EasyOptions_Save1 = "/EasyOptionsSave"
  SLASH_EasyOptions_Save1 = "/EOSave"
  SlashCmdList["EasyOptions_Load"] = LoadAddonSettingsHandler
  SLASH_EasyOptions_Load1 = "/EasyOptionsLoad"
  SLASH_EasyOptions_Load1 = "/EOLoad"
  SlashCmdList["EasyOptions_Delete"] = DeleteAddonSettingsHandler
  SLASH_EasyOptions_Delete1 = "/EasyOptionsDelete"
  SLASH_EasyOptions_Delete1 = "/EODelete"
end
