--
-- player's heads up display
--

function hud_ctor (self)

   self.flash_counter = 0
   self.flash_state = nil

   self.message = nil
   self.notice = nil
   self.notice_counter = 0
end

hud = class (hud_ctor)

function hud:update (T)

   self.notice_counter = self.notice_counter - T
   if (self.notice_counter <= 0) then

      self.notice = nil
      self.notice_counter = 0
   end

   -- update the flash counter
   self.flash_counter = self.flash_counter - T
   if (self.flash_counter < T) then

      self.flash_counter = 0.3
      self.flash_state = not self.flash_state
   end

   -- check for an update message
   self.message = cur_play.road:get_message (cur_play.car.distance_m)
end

function hud:render (R)

   local Car = cur_play.car
   local Road = cur_play.road
   local Traffic = cur_play.traffic
   local Money = cur_play.money
   local Score = cur_play.score
   local Distance = math.floor (cur_play.distance_m / 1000)
   local MaxFuelL = cur_play.car.max_fuel_l
   local FuelL = cur_play.fuel_l

   local RList = {}

   -- shade bar
   table.insert (RList, R:rgba (0, 0, 0, .75))
   table.insert (RList, R:rect (0, 280, 400, 20))

   -- fuel bar
   table.insert (RList, R:rgba (.75, .75, .5, .25))
   table.insert (RList, R:rect (0, 290, 2.5 * MaxFuelL, 10))
   table.insert (RList, R:rgba (1, 1, 0, .5))
   table.insert (RList, R:rect (0, 290, 2.5 * FuelL, 10))

   -- kph
   table.insert (RList, R:rgba (1, 1, 1, 1))
   table.insert (RList, R:text (5, 295, 15, "KPH"))
   table.insert (RList, R:righttext (-5, 295, 20, tostring (math.floor (Car.speed_kph))))

   -- dollars, km, pts
   table.insert (RList, R:text (-295, 295, 12, "$" .. tostring (Money)))
   table.insert (RList, R:text (-395, 280, 8, tostring (Distance) .. " KM"))
   table.insert (RList, R:text (-395, 295, 8, tostring (Score) .. " pts"))

   -- bonuses
   local BonusText = ""
   for i = 1, cur_play.special, 1 do

      BonusText = BonusText .. "?"
   end
   table.insert (RList, R:righttext (245, 295, 12, BonusText))

   -- mood
   table.insert (RList, R:righttext (395, 295, 12, cur_play.mood))

   -- warning?
   if (not cur_play.adventure_flag and not (cur_play.blood_alpha > 0)) then

      if (Car.too_fast) then table.insert (RList, self:get_warning (R, "REDUCE SPEED"))
      elseif (Traffic.trouble) then table.insert (RList, self:get_warning (R, "TROUBLE AHEAD"))
      elseif (cur_play.fuel_l < 5) then table.insert (RList, self:get_warning (R, "FUEL LOW"))
      elseif (self.message) then table.insert (RList, self:get_message (R, self.message))
      elseif (self.notice) then table.insert (RList, self:get_notice (R, self.notice))
      end
   end

   return R:union (RList)
--   table.insert (render_list, R:rgba (1, 1, 1, 1.0))
--   table.insert (render_list, R:centertext (0, 280, 15, "Roads at Night"))
end

function hud:get_warning (R, Message)

   local RList = {}
   table.insert (RList, R:rgba (1, 0, 0, .75))
   table.insert (RList, R:rect (0, 0, 400, 40))
   if (self.flash_state) then

      table.insert (RList, R:rgba (1, 1, 0, 0.5))
      table.insert (RList, R:centertext (0, 20, 30, Message))
   end
   return R:union (RList)
end

function hud:get_message (R, Message)

   local RList = {}
   table.insert (RList, R:rgba (0, 1, 0, .75))
   table.insert (RList, R:rect (0, 0, 400, 40))
   table.insert (RList, R:rgba (1, 1, 1, 0.5))
   table.insert (RList, R:centertext (0, 20, 30, Message))
   return R:union (RList)
end

function hud:get_notice (R, Message)

   local RList = {}
   table.insert (RList, R:rgba (0, 0, 1, .75))
   table.insert (RList, R:rect (0, 0, 400, 40))
   table.insert (RList, R:rgba (1, 1, 1, 0.5))
   table.insert (RList, R:centertext (0, 20, 30, Message))
   return R:union (RList)
end

function hud:set_notice (Message)

   self.notice = Message
   self.notice_counter = 3
end