-- Shopkeeper Utility Functions File
-- Last Updated August 26, 2014
-- Written August 2014 by Dan Stone (@khaibit) - dankitymao@gmail.com
-- Released under terms in license accompanying this file.
-- Distribution without license is prohibited!

function Shopkeeper.LocalizedNumber(numberValue)
  local stringPrice = numberValue
  local subString = "%1" .. GetString(SK_THOUSANDS_SEP) .."%2"

  -- Insert thousands separators for the price
  while true do
    stringPrice, k = string.gsub(stringPrice, "^(-?%d+)(%d%d%d)", subString)
    if (k == 0) then break end
  end

  return stringPrice
end

-- Create a textual representation of a time interval
-- (X and Y) or Z in LUA is the equivalent of C-style
-- ternary syntax X ? Y : Z so long as Y is not false or nil
function Shopkeeper.TextTimeSince(theTime, useLowercase)
  local secsSince = GetTimeStamp() - theTime
  if secsSince < 75 then
    return ((useLowercase and zo_strformat(GetString(SK_TIME_SECONDS_LC), secsSince)) or
             zo_strformat(GetString(SK_TIME_SECONDS), secsSince))
  elseif secsSince < 4500 then
    return ((useLowercase and zo_strformat(GetString(SK_TIME_SECONDS_LC), math.floor(secsSince / 60.0))) or
             zo_strformat(GetString(SK_TIME_MINUTES), math.floor(secsSince / 60.0)))
  elseif secsSince < 86400 then
    return ((useLowercase and zo_strformat(GetString(SK_TIME_SECONDS_LC), math.floor(secsSince / 3600.0))) or
             zo_strformat(GetString(SK_TIME_HOURS), math.floor(secsSince / 3600.0)))
  else
    return ((useLowercase and zo_strformat(GetString(SK_TIME_SECONDS_LC), math.floor(secsSince / 86400.0))) or
             zo_strformat(GetString(SK_TIME_DAYS), math.floor(secsSince / 86400.0)))
  end
end

-- Grabs the first and last events in guildID's sales history and compares the secsSince
-- values returned.  Returns true if the first event (ID 1) is newer than the last event,
-- false otherwise.
function Shopkeeper.IsNewestFirst(guildID)
  local numEvents = GetNumGuildEvents(guildID, GUILD_HISTORY_SALES)
  local _, secsSinceFirst, _, _, _, _, _, _ = GetGuildEventInfo(guildID, GUILD_HISTORY_SALES, 1)
  local _, secsSinceLast, _, _, _, _, _, _ = GetGuildEventInfo(guildID, GUILD_HISTORY_SALES, numEvents)
  return (secsSinceFirst < secsSinceLast)
end

-- A simple utility function to return which set of settings are active,
-- based on the allSettingsAccount option setting.
function Shopkeeper:ActiveSettings()
  if self.acctSavedVariables.allSettingsAccount then return self.acctSavedVariables
  else return self.savedVariables end
end

-- A utility function to grab all the keys of the sound table
-- to populate the options dropdown
function Shopkeeper:SoundKeys()
  local keyList = {}
  for i = 1, #self.alertSounds do table.insert(keyList, self.alertSounds[i].name) end
  return keyList
end

-- A utility function to find the key associated with a given value in
-- the sounds table.  Best we can do is a linear search unfortunately,
-- but it's a small table.
function Shopkeeper:SearchSounds(sound)
  for i, theSound in ipairs(self.alertSounds) do
    if theSound.sound == sound then return theSound.name end
  end

  -- If we hit this point, we didn't find what we were looking for
  return nil
end

-- Same as searchSounds, above, but compares names instead of sounds.
function Shopkeeper:SearchSoundNames(name)
  for i,theSound in ipairs(self.alertSounds) do
    if theSound.name == name then return theSound.sound end
  end
end