module EventSessionHelper

  def event_session_img(event_session)

    # need to figure out img, width, height

    # prefer the session URL if specified; otherwise use the band/user
    url = nil
    width = nil
    height = nil

    if event_session.img_url
      url = image_path(event_session.img_url)
    else
      url = resolve_avatarables(event_session.band, event_session.user, allow_none: true)
      url = image_path(url) if url
    end

    if url
      width = event_session.img_width
      height = event_session.img_height
    else
      url = image_path('web/logo-256.png')
      width = 115
    end

    content_tag(:img, nil, src: url, width: width, height: height)
  end

  def event_session_title(event_session)
    return event_session.band.name if event_session.band
    return event_session.user.name if event_session.user
    'TBD'
  end

  def event_session_start_hour(event_session)
    return 'TBD' unless event_session.starts_at
    timezone = ActiveSupport::TimeZone.new('Central Time (US & Canada)')
    timezone.at(event_session.starts_at.to_i).strftime('%l:%M %P')
  end

  def fetch_last_session(event_session)
    # if no pinned state, then we try to find if there is a session currently on going during the specified time range
    # if so, then we are playing.
    # if there has been none, we say it's still coming,
    # if there has been at least one, and it's over, we say session over
    query = MusicSession.where(fan_access: true).where(created_at: (event_session.starts_at - 12.hours)..(event_session.ends_at + 12.hours))
    if event_session.user_id
      query = query.where(user_id: event_session.user_id)
    elsif event_session.band_id
      query = query.where(band_id: event_session.band_id)
    else
      raise 'invalid state in event_session_button'
    end

    query.order('created_at DESC').first
  end

  def event_session_button(event_session)

    state = nil # can be :not_started, :over, :playing
    state = event_session.pinned_state if event_session.pinned_state
    if state

      music_session = fetch_last_session(event_session)

    elsif !state && (event_session.starts_at && event_session.ends_at && (event_session.user_id || event_session.band_id))

      music_session = fetch_last_session(event_session)

      if music_session
        if music_session.session_removed_at
          state = 'over'
        else
          state = 'playing'
        end
      else
        state = 'not_started'
      end
    end

    if state == 'over'
      content_tag(:a, 'SESSION ENDED', href: music_session.nil? ? '#' : music_session_detail_path(music_session.id), class: 'button-grey')
    elsif state == 'playing'
      content_tag(:a, '', href: music_session_detail_path(music_session.id), class: 'button-orange') do
        content_tag(:span, image_tag('content/icon_playbutton.png', :width => 20, height: 20, align: 'absmiddle', class:'listen-now-play') + 'LISTEN NOW')
      end
    elsif state == 'not_started'
      nil
    else
      nil
    end

  end

  def event_session_description(event_session)
    return event_session.band.biography if event_session.band
    return event_session.user.biography if event_session.user
    ''
  end
end
