ActiveAdmin.register  JamRuby::JamTrack, :as => 'JamTracks' do

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

  config.sort_order = 'name_asc'
  config.batch_actions = false

  filter :name
  filter :original_artist
  filter :genres
  filter :status, :as => :select, collection: JamRuby::JamTrack::STATUS

  scope("Default", default: true) { |scope| scope }
  scope("Onboarding TODO") { |scope| scope.where('onboarding_exceptions is not null') }
  scope("Tency Only") { |scope| scope.joins('INNER JOIN jam_track_licensors as licensors ON jam_tracks.licensor_id = licensors.id').where("licensors.name = 'Tency Music'") }
  scope("TimTracks Only") { |scope| scope.joins('INNER JOIN jam_track_licensors as licensors ON jam_tracks.licensor_id = licensors.id').where("licensors.name = 'Tim Waurick'") }
  scope("Drumma Boy Only") { |scope| scope.joins('INNER JOIN jam_track_licensors as licensors ON jam_tracks.licensor_id = licensors.id').where("licensors.name = 'Drumma Boy'") }
  # scope("Onboarding TODO w/ Tency Only") { |scope| scope.joins('INNER JOIN jam_track_licensors as licensors ON jam_tracks.licensor_id = licensors.id').where("licensors.name = 'Tency Music'").where('onboarding_exceptions is not null') }

  form :partial => 'form'

  index do

    # actions # use this for all view/edit/delete links
    column "Actions" do |jam_track|
      links = ''.html_safe
      clz = "member_link view_link show_tracks"
      clz += ' expand' if params[:focus_track]==jam_track.id
      links << link_to("Show Tracks", '#', :class => clz)
      links << link_to("Update", edit_resource_path(jam_track), :class => "member_link edit_link")
      links
    end

    column :original_artist
    column :name
    column :onboarding_exceptions do |jam_track|
      if jam_track.onboarding_exceptions
        exceptions = jam_track.onboarding_exceptions
        exceptions.keys.join(',')
      else
      ''
      end

    end
    column :status
    column :master_track do |jam_track| jam_track.master_track.nil? ? 'None' : (link_to "Download", jam_track.master_track.url_by_sample_rate(44)) end
    column :licensor
    column :genres do |jam_track|
      jam_track.genres.map(&:description).join(',')
    end
    column :price
    column :reproduction_royalty
    column :public_performance_royalty
    column :reproduction_royalty_amount
    column :licensor_royalty_amount
    column :id

=begin
    column :jam_track_tracks do |jam_track|
      table_for jam_track.jam_track_tracks.order('position ASC') do
        column :id
        column :track_type
        column :instrument
        column :part

        column "" do |track|
          if track.position > 1
            link_to 'Move Up', "jam_tracks/#{track.id}/move_up"
          end
        end
        column "" do |track|
          if track.position < jam_track.jam_track_tracks.count
            link_to 'Move Down', "jam_tracks/#{track.id}/move_down"
          end
        end
        column "" do |track|
          link_to 'Play', '#'
        end
      end
    end
=end


    
  end

  member_action :move_up, :method => :get do
    track = JamTrackTrack.where("id=?",params[:id]).first
    track.move_up
    redirect_to("/admin/jam_tracks?focus_track=#{track.jam_track_id}", {:notice => "Moved Up."})
  end

  member_action :move_down, :method => :get do
    track = JamTrackTrack.where("id=?",params[:id]).first
    track.move_down
    redirect_to("/admin/jam_tracks?focus_track=#{track.jam_track_id}", {:notice => "Moved Down."})    
  end
end