module ApplicationHelper
  def page_title
    @page.try(:full_page_title) || 'Biola University'
  end

  def article_path(article)
    if article.external_url.present?
      article.external_url
    else
      site_article_path(article.site, article.publish_at.year, article.slug)
    end
  end

  def file_link(title: '', path: '#', type: '')
    content_tag :div do
      link_to(title, path, class: 'file-link') + " (#{type})"
    end
  end

  def hosting_player(item)
    return unless item.present?
    host = URI.parse(item).host

    case host
    when 'cdn.biola.edu', /.*\.cloudfront.net/
      'popup-jwvideo'
    when 'www.youtube.com', 'www.youtube-nocookie.com', 'youtu.be'
      'popup-youtube'
    when /.*vimeo.com/
      'popup-vimeo'
    end
  end

  def root_url_with_no_trailing_slash
    if root_url.to_s.length > 1
      # Remove the trailing '/' if there is one
      root_url.gsub(/\/$/, '')
    else
      # Leave it alone if it is just a '/'
      root_url
    end
  end
end
