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

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

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

  filter :genre
  filter :status, :as => :select, collection: JamRuby::JamTrack::STATUS

  form :partial => 'form'

  index do

    # default_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_flags do |jam_track| jam_track.onboard_warnings 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 :genre
    column :price
    column :reproduction_royalty
    column :public_performance_royalty
    column :reproduction_royalty_amount
    column :licensor_royalty_amount
    column :id

    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

  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