--- Divvy's Preview for Balatro - Utils.lua
--
-- Utilities for checking states and formatting display.

function DV.PRE.is_enough_to_win(chips)
   if G.GAME.blind and
      (G.STATE == G.STATES.SELECTING_HAND or
       G.STATE == G.STATES.DRAW_TO_HAND or
       G.STATE == G.STATES.PLAY_TAROT)
   then
      return (DV.PRE.to_big(G.GAME.chips) + DV.PRE.to_big(chips) >= DV.PRE.to_big(G.GAME.blind.chips))
   else
      return false
   end
end

function DV.PRE.format_number(num)
   if not num or type(num) ~= 'number' then return num or '' end
   -- Start using e-notation earlier to reduce number length, if showing min and max for preview:
   if G.SETTINGS.DV.show_min_max and num >= 1e7 then
      local x = string.format("%.4g",num)
      local fac = math.floor(math.log(tonumber(x), 10))
      return string.format("%.2f",x/(10^fac))..'e'..fac
   end
   return number_format(num) -- Default Balatro function.
end

function DV.PRE.get_dollar_colour(n)
   if n == 0 then return HEX("7e7667")
   elseif n > 0 then return G.C.MONEY
   elseif n < 0 then return G.C.RED
   end
end

function DV.PRE.get_sign_str(n)
   if n >= 0 then return "+"
   else return "" -- Negative numbers already have a sign
   end
end

function DV.PRE.get_score_ui_scale(force_small)
   local text_scale = nil
   local node_height = nil
   if G.SETTINGS.DV.show_min_max or force_small then
      text_scale = 0.5
      node_height = 0.42
   else
      text_scale = 0.75
      node_height = 0.62
   end
   return {text_scale = text_scale, node_height = node_height}
end

function DV.PRE.enabled()
   return G.SETTINGS.DV.preview_score or G.SETTINGS.DV.preview_dollars
end

-- The following is necessary for custom handling of Talisman.
-- Specifically, we have to deal with Talisman's big numbers,
-- so this function provides a safe way to convert a number to Talisman's Big.
function DV.PRE.to_big(x)
   if type(x) == 'number' and to_big then
      return to_big(x)
   else
      return x
   end
end
