ActiveAdmin.register JamRuby::EmailBatch, :as => 'Batch Emails' do

  menu :label => 'Batch Emails', :parent => 'Misc'

  config.sort_order = 'updated_at DESC'
  config.batch_actions = false
  config.clear_action_items!
  config.filters = false

  form :partial => 'form'

  action_item :only => [:show] do
    link_to('Edit Batch Email', edit_admin_batch_email_path(resource.id)) if resource.can_run_batch?
  end

  action_item :only => [:show] do
    link_to("Test Batch (#{resource.test_count})", 
            batch_test_admin_batch_email_path(resource.id),
            :confirm => "Run test batch with #{resource.test_count} emails?") if resource.can_run_test?
  end

  action_item :only => [:show] do
    link_to("Deliver Batch (#{User.email_opt_in.count})",
            batch_send_admin_batch_email_path(resource.id),
            :confirm => "Run LIVE batch with #{User.email_opt_in.count} emails?") if resource.can_run_batch?
  end

  action_item :only => [:show, :edit] do
    link_to('Clone Batch Email', batch_clone_admin_batch_email_path(resource.id))
  end

  action_item do
    link_to('New Batch Email', new_admin_batch_email_path)
  end

  index do
    column 'Subject' do |bb| bb.subject end
    column 'Created' do |bb| bb.created_at end
    column 'From' do |bb| bb.from_email end
    column 'Status' do |bb| bb.aasm_state end
    column 'Test Emails' do |bb| bb.test_emails end
    column 'Email Count' do |bb| bb.opt_in_count end
    column 'Sent Count' do |bb| bb.sent_count end
    column 'Started' do |bb| bb.started_at end
    column 'Completed' do |bb| bb.completed_at end
    column 'Send Test' do |bb| 
      bb.can_run_test? ? link_to("Test Batch (#{bb.test_count})", 
                                  batch_test_admin_batch_email_path(bb.id),
                                  :confirm => "Run test batch with #{bb.test_count} emails?") : ''
    end
    column 'Deliver Live' do |bb| 
      bb.can_run_batch? ? link_to("Deliver Batch (#{User.email_opt_in.count})",
                                  batch_send_admin_batch_email_path(bb.id),
                                  :confirm => "Run LIVE batch with #{User.email_opt_in.count} emails?") : ''
    end
    column 'Clone' do |bb| 
      link_to("Clone", batch_clone_admin_batch_email_path(bb.id))
    end

    actions
  end

  show :title => 'Batch Email' do |obj|
    panel 'Email Contents' do 
      attributes_table_for obj do
        row 'From' do |obj| obj.from_email end
        row 'Test Emails' do |obj| obj.test_emails end
        row 'Subject' do |obj| obj.subject end
        row 'Body' do |obj| obj.body end
      end
    end
    columns do 
      column do 
        panel 'Sending Parameters' do 
          attributes_table_for obj do
            row 'State' do |obj| obj.aasm_state end
            row 'Opt-in Count' do |obj| obj.opting_in_count end
            row 'Sent Count' do |obj| obj.sent_count end
            row 'Started' do |obj| obj.started_at end
            row 'Completed' do |obj| obj.completed_at end
            row 'Updated' do |obj| obj.updated_at end
          end
        end
      end
      column do 
        panel 'Send Chunks' do
          table_for(sets = obj.email_batch_sets) do
            column :started_at do |sets| sets.started_at.strftime('%b %d %Y, %H:%M') end
            column :batch_count do |sets| sets.batch_count end
          end
        end
      end
    end
  end

  controller do

    def create 
      batch = EmailBatch.create_with_params(params[:jam_ruby_email_batch])
      set_resource_ivar(batch)
      render active_admin_template('show')
    end

    def update
      resource.update_with_conflict_validation(params[:jam_ruby_email_batch])
      set_resource_ivar(resource)
      render active_admin_template('show')
    end

  end

  member_action :batch_test, :method => :get do
    resource.send_test_batch
    redirect_to admin_batch_email_path(resource.id)
  end

  member_action :batch_send, :method => :get do
    resource.deliver_batch_async
    redirect_to admin_batch_email_path(resource.id)
  end

  member_action :batch_clone, :method => :get do
    bb = resource.clone
    redirect_to edit_admin_batch_email_path(bb.id)
  end

end
