object @feed


glue :music_session do

  node :type do |i|
    'music_session'
  end

  attributes :id, :genres, :created_at, :started_at, :session_removed_at, :comment_count, :like_count, :play_count, :fan_access, :is_over?, :has_mount?

  node do |history|
  {
    helpers: {
      avatar: asset_path(resolve_avatarables(history.band, history.creator)),
      artist_name: session_artist_name(history),
      artist_id: session_artist_id(history),
      artist_datakey: session_artist_datakey(history),
      artist_hoveraction: session_artist_hoveraction(history),
      utc_created_at: session_started_at(history).getutc.iso8601,
      name: session_name(history, current_user),
      description: session_description(history, current_user),
      status: session_text(history),
      duration: session_duration_value(history),
      duration_secs: session_started_at(history).to_i,
      genre: session_genre(history)
    }
  }
  end

  child(:creator => :creator) {
      attributes :id, :first_name, :last_name, :photo_url
  }

  child(:unique_user_histories => :participants) {
    attributes :first_name, :last_name, :photo_url

    node :id do |user|
      user.user_id
    end

    node do |user|
      {
        helpers: {
          avatar: asset_path(resolve_avatarables(user))
        }
      }
      end

    # total_duration comes back from the database as a string
    node :duration do |user|
      user.total_duration.nil? ? 0 : user.total_duration.to_i
    end

    node :instruments do |user|
      user.total_instruments.nil? ? [] : user.total_instruments.split('|').uniq
    end
  }

  child(:band => :band) {
    attributes :id, :name, :location, :photo_url
  }

  child(:active_music_session => :active_music_session) do
    # 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
end

glue :recording do

  node :type do |i|
    'recording'
  end

  attributes :id, :band, :created_at, :duration, :comment_count, :like_count, :play_count, :has_mix?, :mix_state, :when_will_be_discarded?

  node do |recording|
    {
      helpers: {
        avatar: asset_path(resolve_avatarables(recording.band, recording.owner)),
        artist_name: recording_artist_name(recording),
        artist_id: recording_artist_id(recording),
        artist_hoveraction: recording_artist_hoveraction(recording),
        artist_datakey: recording_artist_datakey(recording),
        utc_created_at: recording.created_at.getutc.iso8601,
        name: recording_name(recording, current_user),
        description: recording_description(recording, current_user),
        genre: recording_genre(recording)
      }
    }
    end

  node :mix do |recording|
    {
      state: recording.mix_state,
      error: recording.mix_error
    }
  end unless @object.mix.nil?

  child(:owner => :owner) {
    attributes :id, :name, :location, :photo_url
  }

  child(:band => :band) {
      attributes :id, :name, :location, :photo_url
  }

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

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

  child(:grouped_tracks => :grouped_tracks) {

    node :instrument_ids do |track|
      track.instrument_ids
    end

    child(:musician => :musician) {
      attributes :id, :first_name, :last_name, :city, :state, :country, :location, :photo_url

       node do |user|
         {
           helpers: {
             avatar: asset_path(resolve_avatarables(user))
           }
         }
       end
    }
  }

  child(:comments => :comments) {
    attributes :comment, :created_at

    child(:user => :creator) {
      attributes :id, :first_name, :last_name, :photo_url
    }
  }

  child(:claimed_recordings => :claimed_recordings) {

    attributes :id, :is_public, :genre_id, :has_mix?, :user_id

    child(:user => :creator) {
      attributes :id, :first_name, :last_name, :photo_url
    }

    node :share_url do |claimed_recording|
        unless claimed_recording.share_token.nil?
          share_token_url(claimed_recording.share_token.token)
        end
    end

    node :mix do |claimed_recording|
     listen_mix_url(claimed_recording.recording) if claimed_recording.has_mix?
    end
  }

end
