class ApiLinksController < ApiController

  respond_to :json

  before_filter :api_any_user
  before_filter :affiliate_partner

  def log
    @log || Logging.logger[ApiLinksController]
  end

  def all
    links = AffiliateLink.all.order(:created_at)
    results = []
    links.each do |link|
      url = nil
      if link.link.index('?')
        url = link.link + '&' + affiliate_params.to_query
      else
        url = link.link + '?' + affiliate_params.to_query
      end

      results << {url: url,
                  target: link.name }
    end
    render json: results, status: 200
  end
=begin

  def jamtrack_song_index
    @affiliate_params = affiliate_params()

    @affiliate_params = affiliate_params('jamtrack-song')

    results = []

    @jamtracks, @next = JamTrack.index({offset: 0, limit:1000}, any_user)

    @jamtracks.each do |jamtrack|
      results << {url: individual_jamtrack_url(jamtrack.short_plan_code, @affiliate_params),
                  target: "#{jamtrack.original_artist} - #{jamtrack.name}" }
    end

    render json: results, status: 200
  end

  def jamtrack_band_index
    @affiliate_params = affiliate_params('jamtrack-band')

    results = []

    @jamtracks, @next = JamTrack.index({offset: 0, limit:1000}, any_user)
    @jamtracks.each do |jamtrack|
        results << {url: individual_jamtrack_band_url(jamtrack.short_plan_code, @affiliate_params),
                    target: "#{jamtrack.original_artist} - #{jamtrack.name}" }
    end

    render json: results, status: 200
  end

  def jamtrack_general_index
    @affiliate_params = affiliate_params('jamtrack-general')
    @affiliate_params[:generic] = 1

    results = []

    @jamtracks, @next = JamTrack.index({offset: 0, limit:1000}, any_user)

    @jamtracks.each do |jamtrack|
      results << {url: individual_jamtrack_url(jamtrack.short_plan_code, @affiliate_params),
                  target: "#{jamtrack.original_artist} - #{jamtrack.name}" }
    end

    render json: results, status: 200
  end

  def jamkazam_general_index

    results = []

    @affiliate_params = affiliate_params('home')
    results << {url: root_url(@affiliate_params), target: 'JamKazam Home'}

    @affiliate_params = affiliate_params('products-jamblaster')
    results << {url: product_jamblaster_url(@affiliate_params), target: 'JamBlaster'}

    @affiliate_params = affiliate_params('products-jamtracks')
    results << {url: product_jamtracks_url(@affiliate_params), target: 'JamTracks'}


    render json: results, status: 200
  end

  def session_index

    @affiliate_params = affiliate_params('session')

    results = []

    ActiveRecord::Base.transaction do
      options = {offset:0, limit:50}
      @music_sessions = MusicSession.scheduled(@partner.partner_user, true)

      @music_sessions.each do |session|
        results << {url: music_scheduled_session_detail_url(session.id, @affiliate_params), target: session.name}
      end
    end

    render json: results, status: 200
  end

  def recording_index

    @affiliate_params = affiliate_params('recording')

    results = []

    feeds, next_page = Feed.index(@partner.partner_user, :type => 'recording', time_range: 'all', limit:100, user: @partner.partner_user.id, )

    feeds.each do |feed|
      claim = feed.recording.candidate_claimed_recording
      if claim
        results << {url: share_token_url(claim.share_token.token, @affiliate_params), target: claim.name}
      end
    end

    render json: results, status: 200
  end
=end

  private
  def affiliate_params()
    #{utm_source:'affiliate', utm_medium: 'affiliate', utm_campaign: "#{Date.today.year}-affiliate-#{campaign}", affiliate: @partner.id}#
    # the above was deemed too noisy
    {affiliate: @partner.id}
  end
end
