ActiveAdmin.register JamRuby::Mix, :as => 'Mixes' do

  config.filters = true
  config.per_page = 50
  config.clear_action_items!
  config.sort_order = "created_at_desc"
  menu :parent => 'Sessions'

  controller do

    def mix_again
      @mix = Mix.find(params[:id])
      @mix.enqueue
      render :json =>  {}
    end
  end


  index :as => :block do |mix|
    div :for => mix do
      h3 "Mix (Users: #{mix.recording.users.map { |u| u.name }.join ','}) (When: #{mix.created_at.strftime('%b %d %Y, %H:%M')})"
      columns do
        column do
          panel 'Mix Details' do
            attributes_table_for(mix) do
              row :recording do |mix| auto_link(mix.recording, mix.recording.id) end
              row :created_at do |mix| mix.created_at.strftime('%b %d %Y, %H:%M') end
              row :s3_url do |mix| mix.sign_url end
              row :manifest do |mix| mix.manifest end
              row :local_manifest do |mix|
                div class: 'local-manifest' do
                  mix.local_manifest.to_json
                end
              end
              row :download_script do |mix|
                div class: 'download-script' do
                  mix.download_script
                end
              end
              row :completed do |mix| "#{mix.completed ? "finished" : "not finished"}" end
              if mix.completed
                row :completed_at do |mix| mix.completed_at.strftime('%b %d %Y, %H:%M') end
              elsif mix.error_count > 0
                     row :error_count do |mix| "#{mix.error_count} times failed" end
                     row :error_reason do |mix| "last reason failed: #{mix.error_reason}" end
                     row :error_detail do |mix| "last error detail: #{mix.error_detail}" end
                     row :mix_again do |mix| div :class => 'mix-again' do
                              span do link_to "Mix Again", '#', :class => 'mix-again', :'data-mix-id' => mix.id end
                              span do div :class => 'mix-again-dialog' do end end
                     end
                end
              end

            end
          end
        end
      end
    end
  end
end
