Rails.application.routes.draw do
  locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_locales
  if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
    Thread.new do
      archive_sort_order = ArchiveSortOrder.first
      if archive_sort_order.nil?
          archive_sort_order = ArchiveSortOrder.create('sort_order' => false)
      end
      need_update_sort = (archive_sort_order.need_update_sort rescue false)
      if need_update_sort
        sort_order = (archive_sort_order.sort_order rescue false) ? 1 : -1
        ArchiveFile.recalc_sort_number(sort_order)
      end
    end
  end
  scope "(:locale)", locale: Regexp.new(locales.join("|")) do
    namespace :admin do
      get 'archive_files/categories_order' => 'archive_files#categories_order'
      post 'archive_files/categories_order' => 'archive_files#save_categories_order'
      post 'archive_file/createfeed', to: 'archive_files#createfeed'
      patch 'archive_file/updatefeed', to: 'archive_files#updatefeed'
      delete 'archive_file/deletefeed', to: 'archive_files#deletefeed'
      get 'archive_file/feed', to: 'archive_files#feed'
      get 'archive_files/feedform', to: 'archive_files#feedform'
      delete 'archive_files/delete', to: 'archive_files#delete'
      resources :archive_files
   end
    get "/xhr/archive_files/feeds" => "archive_feeds#feeds"
    get "/xhr/archive/download" => "archives#download_file"
    get "/xhr/web/cmaps/:file_name.:extension" => "archives#serve_cmap"
    post "/xhr/archives/feed_add_remote/:uid" => "archive_feeds#feed_add_remote"
    post "/xhr/archives/feed_remove_remote/:uid" => "archive_feeds#feed_remove_remote"
    get "/xhr/archive_files/feed/:uid" => "archive_feeds#feed"
    get "/xhr/archive_files/rssfeed/:uid" => "archive_feeds#rssfeed"
    get "/xhr/archive_files/feeds" => "archive_feeds#feeds"
  end

end
