class MoviesServiceClient 
  def get_movie_showtimes_by_theatre(theatre_id)
    result = self.get_showtimes('', [theatre_id], []) 
    group_by_movie(result) 
  end
 
  protected 

  # Zakadamy, e lista bdca argumentem wywoania zawiera seanse 
  # zwizane z dokadnie jednym kinem; jeli warunek ten nie jest speniony,
  # nie jest przeprowadzane adne grupowanie wzgldem kin 
  def group_by_movie(showtimes_result) 
    movies hash = Hash.new
    theatres_hash = Hash.new 
    showtimes_hash = Hash.new 
  
    for movie in showtimes_result.movies do 
      movies_hash[movie.id] = movie 
    end 
  
    for theatre in showtimes_result.theatres do 
      theatres_hash[theatre.id] = theatre 
    end 

    result = Array.new 

    for showtime in showtimes_result.showtimes_light do 
      showtimes_hash[showtime.movie_id] ||= Array.new 
      showtimes_hash[showtime.movie_id] << Logical::Showtime.new(
        :movie => movies hash[showtime.movie id], 
        :theatre => theatres hash[showtime.theatre id], 
        :start_time => showtime.start_time,
        :auditorium => showtime.auditorium 
      )
    end 
  
    showtimes_hash.collect{|movie_id, showtimes| 
      Logical::MovieShowtimes.new( 
        :movie => movies_hash[movie_id],
        :showtimes => showtimes
      )
    }
  end
end

