indexing
	description: "Represents the logical version of the game timer."

deferred class GAME_TIMER

inherit 
  TM_CLIENT

feature{NONE} -- private

  room : GAME_ROOM	 
  game_time_event : TM_EVENT
  game_time_interval : INTEGER is 1000
  initial_time : STRING is "Time"

  initialize_game_timer is
  --setup initial values
  require
    room_is_present: room /= Void
  do
    create time_value
    set_zero_game_time
    create game_time_event.make_periodic( Current, game_time_interval )
    change_clock( room.clock )
  ensure
    clock = room.clock
    time_value /= Void
  end

  set_zero_game_time is
  do
    time_value.set_time_value( 0, 0, 0 )
  end

  increment_timer is
  -- Add one to the time elapsed
  do 
    time_value.increment_time_value
  end

  check_wake_up : BOOLEAN is
  do
    if ( room.stooge /= Void ) then
      Result := room.stooge.dead implies not is_running
    else
      Result := True
    end
  end

  parse_game_time( time : STRING ) is
  do
    if time.is_equal( initial_time ) then
      set_zero_game_time
    else
      time_value.parse_time( time )
    end
  end

feature -- public

  time_value : TIME_VALUE

  is_running : BOOLEAN
  -- Is the timer incrementing?
 
  start_clock is
  -- Start the timer
  do
    if not is_running then
      is_running := True
      clock.register( game_time_event )
    end
  ensure
    game_clock_running: is_running
    game_clock_event_registered: clock.is_present( game_time_event )
  end

  stop_clock is
  -- stop the timer
  do
    if is_running then
      is_running := False
      clock.cancel( game_time_event )
    end
  ensure
    game_clock_not_running: not is_running
    game_clock_event_canceled: not clock.is_present( game_time_event )
  end

  reset_clock is
  -- stop the clock and set the time elapsed to zero
  do
    stop_clock
    set_zero_game_time
    room.display_game_clock( initial_time )
  ensure
    game_clock_not_running: not is_running
  end	
 
  wake_up (e : TM_EVENT) is
  do
    if room.stooge /= Void then
    -- the room may be stoogeless
      if room.stooge.dead then
      -- Stop the clock if stooge is dead.
        stop_clock
      else
        increment_timer
        refresh_game_clock_display
      end
    end
  ensure then
    proper_wake_up: check_wake_up
  end

  restore_clock( time : STRING ) is 
  -- Restore the elapsed time to a specific point
  do
    stop_clock
    parse_game_time( time )
    refresh_game_clock_display
    start_clock
  ensure 
    game_clock_running: is_running
  end

  elapsed_game_time : STRING is
  do
    Result := time_value.out
  end

  refresh_game_clock_display is
  do
    room.display_game_clock( elapsed_game_time )
  end
 
end 
 