module FeedsHelper
  PRIVATE_TEXT = "Private"

  def session_artist_name(music_session)
    (music_session.band.nil? ? nil : music_session.band.name) || music_session.creator.name
  end

  def session_artist_id(music_session)
    (music_session.band.nil? ? nil : music_session.band.id) || music_session.creator.id
  end

  def session_artist_hoveraction(music_session)
    music_session.band.nil? ? 'musician' : 'band'
  end

  def session_artist_datakey(music_session)
    music_session.band.nil? ? 'user-id' : 'band-id'
  end

  def session_avatar(music_session)
    image_tag resolve_avatarables(music_session.band, music_session.creator)
  end

  def session_duration_value(music_session)
    if music_session.session_removed_at.nil?
      Time.now.utc - session_started_at(music_session)
    else
      music_session.session_removed_at - session_started_at(music_session)
    end
  end

  def session_started_at(music_session)
    music_session.started_at.nil? ? music_session.created_at : music_session.started_at
  end

  def session_duration(music_session, options={})
    duration(session_duration_value(music_session), options)
  end

  def session_text(music_session)
    if music_session.is_over?
      'SESSION ENDED'
    else
      if music_session.fan_access
        if music_session.active_music_session && music_session.active_music_session.mount
          'SESSION IN PROGRESS'
        else
          'BROADCASTING OFFLINE' # if you see this in development, you need to set up icecast.  If you see this in production, it's not healthy
        end
      else
        'PRIVATE SESSION'
      end
    end
  end

  def session_name(music_session, user)
    if music_session.fan_access || music_session.unique_users.include?(user)
      music_session.name
    else
      PRIVATE_TEXT
    end
  end

  def session_description(music_session, user)
    if music_session.fan_access || music_session.unique_users.include?(user)
      music_session.description
    else
      PRIVATE_TEXT
    end
  end

  # grabs 1st genre
  def session_genre(music_session)
   music_session.genre.description
  end

  def recording_artist_name(recording)
    (recording.band.nil? ? nil : recording.band.name) || recording.owner.name
  end

  def recording_artist_id(recording)
    (recording.band.nil? ? nil : recording.band.id) || recording.owner.id
  end

  def recording_artist_hoveraction(recording)
    recording.band.nil? ? 'musician' : 'band'
  end

  def recording_artist_datakey(recording)
    recording.band.nil? ? 'user-id' : 'band-id'
  end

  def recording_avatar(recording)
    image_tag resolve_avatarables(recording.band, recording.owner)
  end

  def recording_duration(recording, options={})
    duration(recording.duration, options)
  end

  def recording_name(recording, user)
    r = recording.candidate_claimed_recording
    if r.is_public || recording.users.include?(user)
      r.name
    else
      PRIVATE_TEXT
    end
  end

  def recording_description(recording, user)
    r = recording.candidate_claimed_recording
    if r.is_public || recording.users.include?(user)
      r.description
    else
      PRIVATE_TEXT
    end
  end

  def recording_genre(recording)
    recording.candidate_claimed_recording.genre.description
  end
end
