ActiveAdmin.register JamRuby::Connection, :as => 'Connection' do

  menu :parent => 'Operations'

  actions :index, :show

  action_item :only => [:index] do
    link_to('Reload All Clients', reload_all_admin_connections_path, class: 'confirm')
  end
  collection_action :reload_all, :method => :get do
    Notification.send_reload(MessageFactory::ALL_NATIVE_CLIENTS)
    redirect_to({:action => :index}, {:notice => "All Clients Reloaded!"})
  end

  action_item :only => [:index] do
    link_to('Restart All Clients', restart_all_admin_connections_path, class: 'confirm')
  end
  collection_action :restart_all, :method => :get do
    Notification.send_restart_application(MessageFactory::ALL_NATIVE_CLIENTS)
    redirect_to({:action => :index}, {:notice => "All Clients Restarted!"})
  end

  action_item :only => [:index] do
    link_to('Shutdown All Clients', stop_all_admin_connections_path, class: 'confirm')
  end
  collection_action :stop_all, :method => :get do
    Notification.send_stop_application(MessageFactory::ALL_NATIVE_CLIENTS)
    redirect_to({:action => :index}, {:notice => "All Clients Shutdown!"})
  end

  action_item :only => [:show] do
    link_to('Reload', reload_admin_connection_path(resource.id), class: 'confirm')
  end
  member_action :reload, :method => :get do
    connection = Connection.find(params[:id])
    Notification.send_reload(connection.client_id)
    redirect_to({:action => :show}, {:notice => "Reloaded!"})
  end

  action_item :only => [:show] do
    link_to('Restart Client', restart_admin_connection_path(resource.id), class: 'confirm') if resource.client_type == 'client'
  end
  member_action :restart, :method => :get do
    connection = Connection.find(params[:id])
    Notification.send_restart_application(connection.client_id)
    redirect_to({:action => :show}, {:notice => "Restarted!"})
  end

  action_item :only => [:show] do
    link_to('Shutdown Client', stop_admin_connection_path(resource.id), class: 'confirm') if resource.client_type == 'client'
  end
  member_action :stop, :method => :get do
    connection = Connection.find(params[:id])
    Notification.send_stop_application(connection.client_id)
    redirect_to({:action => :show}, {:notice => "Shutdown!"})
  end

  index do
    actions
    column :user_id do |c|
      c.user ? c.user.name : ''
    end
    column :ip_address
    column :client_type
    column :music_session_id do |c|
      c.music_session ? c.music_session.name : ''
    end
    column :client_id
    column :locidispid
    column :aasm_state
    column :udp_reachable
    column :scoring_failures
    column :scoring_timeout_occurrences
    column :scoring_failures_offset
    column :scoring_timeout do |c|
      Time.now > c.scoring_timeout ? '' : "#{((c.scoring_timeout - Time.now) / 60).round} minutes left"
    end
  end

  show do
    attributes_table do
      row :user_id do |c|
        c.user ? c.user.name : ''
      end
      row :ip_address
      row :client_type
      row :music_session_id do |c|
        c.music_session ? c.music_session.name : ''
      end
      row :client_id
      row :locidispid
      row :aasm_state
      row :udp_reachable
      row :is_network_testing
      row :scoring_failures
      row :scoring_timeout_occurrences
      row :scoring_failures_offset
      row :scoring_timeout do |c|
        Time.now > c.scoring_timeout ? '' : "#{((c.scoring_timeout - Time.now) / 60).round} minutes left"
      end
    end
  end

end

