object @music_session

if !current_user
  # there should be more data returned, but we need to think very carefully about what data is public for a music session
  attributes :id

  # only show mount info if fan_access is public.  Eventually we'll also need to show this in other scenarios, like if invited
  child({:mount => :mount}, :if => lambda { |music_session| music_session.fan_access}) {
    attributes :id, :name, :sourced, :listeners, :bitrate, :subtype, :url
    node(:mime_type) { |mount|  mount.resolve_string(:mime_type) }
    node(:bitrate) { |mount|  mount.resolve_string(:bitrate) }
    node(:subtype) { |mount|  mount.resolve_string(:subtype) }
  }
else

  attributes :id, :name, :description, :musician_access, :approval_required, :friends_can_join, :fan_access, :fan_chat, :band_id, :user_id, :claimed_recording_initiator_id, :track_changes_counter, :max_score, :backing_track_path, :metronome_active, :jam_track_initiator_id, :jam_track_id, :music_session_id_int, :use_video_conferencing_server, :created_at

  if @on_join
    node :subscription do |session|
      @subscription_rules
    end

    node :session_rules do |session|
      @session_rules
    end
  end

  node :can_join do |session|
    session.can_join?(current_user, true)
  end

  node :genres do |item|
    [item.genre.description] # XXX: need to return single genre; not array
  end

  child(:music_notations => :music_notations) {
    attributes :id, :file_name

    node do |music_notation|
      { file_url: "/api/music_notations/#{music_notation.id}" }
    end
  }

  if :is_recording?
      node do |music_session|
          { :recording => partial("api_recordings/show", :object => music_session.recording) }
      end
  end

  node do |music_session|
    session_props = {}
    unless music_session.music_session.share_token.nil?
      session_props[:share_url] = share_token_url(music_session.music_session.share_token.token)
    end

    session_props[:session_controller_id] = music_session.music_session.session_controller_id
    session_props
  end


  child(:connections => :participants) {
    collection @music_sessions, :object_root => false
    attributes :ip_address, :client_id, :joined_session_at, :audio_latency, :id, :metronome_open, :is_jamblaster, :client_role, :parent_client_id, :client_id_int

    node :user do |connection|
      { :id => connection.user.id, :photo_url => connection.user.photo_url, :name => connection.user.name, :is_friend => connection.user.friends?(current_user), :connection_state => connection.aasm_state, :subscription => connection.user.subscription_plan_code }
    end

    child(:tracks => :tracks) {
        attributes :id, :connection_id, :instrument_id, :sound, :client_track_id, :client_resource_id, :updated_at

        node :instrument do |track|
          track.instrument.description
        end

    }

    child(:backing_tracks => :backing_tracks) {
        attributes :id, :connection_id, :filename, :client_track_id, :client_resource_id, :updated_at
    }
  }

  child({:invitations => :invitations}) {
    attributes :id, :sender_id, :receiver_id
  }

  child(lesson_session: :lesson_session) {
    attributes :id, :scheduled_start, :status, :teacher_id, :success, :duration, :student_id
  }

  # only show join_requests if the current_user is in the session
  child({:join_requests => :join_requests}, :if => lambda { |music_session| music_session.users.exists?(current_user.id) } ) {
    attributes :id, :text
    child(:user => :user) {
        attributes :id, :name
    }
  }

  # only show currently open jam track info if the current user is in the session
  child({:jam_track => :jam_track}, :if => lambda { |music_session| music_session.users.exists?(current_user.id) }) {
    attributes :id, :name, :description

    node :mixdown do |jam_track|
      right = jam_track.right_for_user(User.find(@music_session.jam_track_initiator_id))

      {id: right ? right.last_mixdown_id : nil}
    end

    child(:jam_track_tracks => :tracks) {
      attributes :id, :part, :instrument, :track_type
    }
  }

  # only show currently playing recording data if the current_user is in the session
  child({:claimed_recording => :claimed_recording}, :if => lambda { |music_session| music_session.users.exists?(current_user.id) }) {
      attributes :id, :name, :description, :is_public

      child(:recording => :recording) {
          attributes :id, :created_at, :duration
          child(:band => :band) {
            attributes :id, :name
          }

          child(:mixes => :mixes) {
            attributes :id, :is_completed

            node :mp3_url do |mix|
              mix[:mp3_url]
            end

            node :ogg_url do |mix|
              mix[:ogg_url]
            end
          }

          child({:jam_track => :jam_track}) {
              attributes :id, :name, :description
          }

          child(:recorded_tracks => :recorded_tracks) {
              attributes :id, :fully_uploaded, :client_track_id, :client_id, :instrument_id

              node :url do |recorded_track|
                recorded_track[:url]
              end

              child(:user => :user) {
                attributes :id, :first_name, :last_name, :name, :city, :state, :country, :photo_url
              }
          }

          child(:recorded_backing_tracks => :recorded_backing_tracks) {
              attributes :id, :fully_uploaded, :client_track_id, :client_id, :filename

              child(:user => :user) {
                attributes :id, :first_name, :last_name, :name, :city, :state, :country, :photo_url
              }
          }

          child(:recorded_jam_track_tracks => :recorded_jam_track_tracks) {
            node do |recorded_jam_track_track|
              {
                id: recorded_jam_track_track.jam_track_track.id,
                user_id: recorded_jam_track_track.user_id,
                part: recorded_jam_track_track.jam_track_track.part,
                instrument: recorded_jam_track_track.jam_track_track.instrument,
                track_type: recorded_jam_track_track.jam_track_track.track_type,
                timeline: recorded_jam_track_track.timeline ? recorded_jam_track_track.timeline : []
              }
            end
          }
      }
  }

  # only show mount info if fan_access is public.  Eventually we'll also need to show this in other scenarios, like if invited
  child({:mount => :mount}, :if => lambda { |music_session| music_session.fan_access}) {
    attributes :id, :name, :sourced, :listeners, :bitrate, :subtype, :url
    node(:mime_type) { |mount|  mount.resolve_string(:mime_type) }
    node(:bitrate) { |mount|  mount.resolve_string(:bitrate) }
    node(:subtype) { |mount|  mount.resolve_string(:subtype) }
  }
end
