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

  menu :label => 'Recording', :parent => 'Recordings'

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

  form :partial => 'form'

  controller do

    def initialize_client_tokens(params)

      recording = params[:jam_ruby_recording]
      return params unless recording

      recorded_tracks = recording[:recorded_tracks_attributes]
      return params unless recorded_tracks

      recorded_tracks.each do |key, recorded_track|
        recorded_track[:client_id]        = nil if recorded_track[:client_id] == ""
        recorded_track[:client_track_id]  = nil if recorded_track[:client_track_id] == ""
        recorded_track[:track_id]         = nil if recorded_track[:track_id] == ""

        recorded_track[:client_id]        ||= recorded_track[:user_id] if recorded_track[:user_id]
        recorded_track[:client_track_id]  ||= SecureRandom.uuid
        recorded_track[:track_id]         ||= SecureRandom.uuid
      end

      params
    end

    def new
      @recording = JamRuby::Recording.new

      super
    end

    def create
      params.merge! initialize_client_tokens(params)
      create!
    end

    def edit
      @recording = resource
      super
    end

    def update
      params.merge! initialize_client_tokens(params)

      update! do |format|
            format.html { redirect_to edit_admin_recording_url(params[:jam_ruby_recording]) }
      end
    end
  end


end
