module AvatarHelper

  def render_avatarable(avatarable)
    image_tag resolve_avatarable(avatarable)
  end

  def resolve_user_avatar_url(user)
    user.photo_url.nil? ? "shared/avatar_generic.png" : user.photo_url
  end

  def resolve_band_avatar_url(band)
    band.photo_url.nil? ? "shared/avatar_generic_band.png" : band.photo_url
  end

  def resolve_avatarable(avatarable)
    if avatarable.class == JamRuby::User || avatarable.class == JamRuby::MusicSessionUserHistory
      resolve_user_avatar_url(avatarable)
    elsif avatarable.class == JamRuby::Band
      resolve_band_avatar_url(avatarable)
    else
      raise "unable to resolve avatarable #{avatarable}"
    end
  end

  def resolve_avatarables(*avatarables, allow_none: false)
    avatarables.each do |avatarable|
      return resolve_avatarable(avatarable) if avatarable
    end

    if allow_none
      nil
    else
      raise "at least one avatarable must be specified"
    end
  end
end