--profile import / export functions
--[[
    the method to share and import profiles is based on:
    https://github.com/brittyazel/EnhancedRaidFrames/blob/main/EnhancedRaidFrames.lua
]]--
local addonName, addonTable = ...
local addon = addonTable.addon
local LibDeflate = LibStub:GetLibrary("LibDeflate")
local L = LibStub("AceLocale-3.0"):GetLocale(addonName)

function addon:ShareProfile()
  --AceSerialize
  local serialized_profile = self:Serialize(self.db.profile) 
  --LibDeflate
  local compressed_profile = LibDeflate:CompressZlib(serialized_profile) 
  local encoded_profile = LibDeflate:EncodeForPrint(compressed_profile)
  return encoded_profile
end

function addon:ImportProfile(input)
  --validate input
  --empty?
  if input == "" then
    self:Print(L["import_empty_string_error"])
    return
  end
  --LibDeflate decode
  local decoded_profile = LibDeflate:DecodeForPrint(input)
  if decoded_profile == nil then
    self:Print(L["import_decoding_failed_error"])
    return
  end
  --LibDefalte uncompress
  local uncompressed_profile = LibDeflate:DecompressZlib(decoded_profile)
  if uncompressed_profile == nil then
    self:Print(L["import_uncompression_failed_error"])
    return
  end
  --AceSerialize
  --deserialize the profile and overwirte the current values
  local valid, imported_Profile = self:Deserialize(uncompressed_profile)
  if valid and imported_Profile then
  for i,v in pairs(imported_Profile) do
    self.db.profile[i] = CopyTable(v)
  end
  else
    self:Print(L["invalid_profile_error"])
  end
end