ActiveAdmin.register JamRuby::DownloadTracker, :as => 'DownloadTrackers' do

  menu :label => 'Download Trackers', :parent => 'JamTracks'

  config.batch_actions = false
  config.filters = true
  config.per_page = 50

  filter :remote_ip

  index do
    column 'User' do |oo| oo.user ? link_to(oo.user.email, oo.user.admin_url, {:title => oo.user.email}) : '' end
    column 'Created' do |oo| oo.created_at end
    column 'JamTrack' do |oo| oo.jam_track end
    column 'Paid' do |oo| oo.paid end
    column 'Blacklisted?' do |oo| IpBlacklist.listed(oo.remote_ip) ? 'Yes' : 'No' end
    column 'Remote IP' do |oo| oo.remote_ip end
    column "" do |oo|
      link_to 'Blacklist This IP', "download_trackers/#{oo.id}/blacklist_by_ip"
    end
  end

  member_action :blacklist_by_ip, :method => :get do
    tracker = DownloadTracker.find(params[:id])

    if !IpBlacklist.listed(tracker.remote_ip)
      ip = IpBlacklist.new
      ip.remote_ip = tracker.remote_ip
      ip.save!
    end

    redirect_to admin_download_trackers_path, :notice => "IP address #{tracker.remote_ip} blacklisted."
  end

end
