 # Controls the display of the users arranged on a histogram. 
 # 
 # The user avatars are arranged imprecisely on the histogram
-# based on the user's opinion, using a physics simulation. 
+# based on the user's opinion, using a layout_params simulation. 
 #
 # The pros and cons can be filtered to specific opinion regions
 # (individual and collective). 

#
# Local state: 
#   simulation_opinion_hash
#      Hash of all opinion stances. Used to determine if the physics
#      simulation needs to be rerun on a rerender.
#   mouse_opinion_value
#      Stores the mapped opinion value of the current mouse position
#      within the histogram. 
#   avatar_size
#      The base size of the avatars, as determined by the physics 
#      simulation. This piece of state would be local, but it needs
#      to be settable from the physics simulation.

# Accessibility notes: 
#   - histogram itself should be tabbable. Should summarize results. 

      opinions = for opinion, i in filtered_opinions
        {stance: opinion.stance, user: opinion.user}
      
      # use a less cpu intensive layout for mobile browsers
      layout_params = _.defaults {}, (@props.layout_params or {}), 
        engine: if browser.is_mobile then 'tile' else 'matterjs'
        initial_layout: if browser.is_mobile then 'packed-tile' else 'tiled-with-wiggle'
        fill_ratio: 1
        show_histogram_physics: show_histogram_physics
        motionSleepThreshold: .005
        motionSleepThresholdIncrement: .000025
        enable_boosting: false
        wake_every_x_ticks: 9999
        global_swap_every_x_ticks: 150
        reduce_sleep_threshold_if_little_movement: true 
        sleep_reduction_exponent: .5
        cleanup_layout_every_x_ticks: 50
        x_force_mult: .008
        gravity_scale: .000012
        restack_top: false 
        final_cleanup_stability: .5
        change_cleanup_stability: -0.2
        cleanup_stability: .9
        cleanup_when_percent_sleeping: .4
        end_sleep_percent: .75
        filter_to_inflections_and_flats: true
        cleanup_overlap: 1.8
        cascade_instability: true


      setTimeout =>
        if @isMounted()
          layoutAvatars
            running_state: @props.key 
            k: histocache_key
            w: @props.width
            h: @props.height
            o: opinions
            r: @local.avatar_size / 2
            layout_params: layout_params
            abort: => 
              abort = !@isMounted() || @current_request != histocache_key
              abort
