class CustomDomain
  def matches?(request)
    return false if request.subdomain.length <= 0 || request.subdomain == 'www'
    true
  end
end

Rails.application.routes.draw do

  root to: "default#index"

  resources :lti_installs

  devise_for :users, controllers: {
    sessions: "sessions",
    registrations: "registrations",
    omniauth_callbacks: "omniauth_callbacks"
  }

  resources :users do
    resources :assessments, except: [:update, :edit, :show], controller: "assessments"
  end

  as :user do
    get    '/auth/failure'         => 'sessions#new'
    get     'users/auth/:provider'  => 'users/omniauth_callbacks#passthru'
    get     'sign_in'               => 'sessions#new'
    post    'sign_in'               => 'sessions#create'
    get     'sign_up'               => 'devise/registrations#new'
    delete  'sign_out'              => 'sessions#destroy'
  end

  resources :canvas_authentications

  resources :admin, only: [:index]

  namespace :api do
    resources :accounts do
      resources :users
    end
  end

  post 'assessments/lti', to: 'assessments#lti', as: 'lti'
  resources :assessments do
    member do
      post :show
      post :edit
    end
  end

  resources :assessment_loaders

  get 'saml', to: 'saml#index'
  get 'saml/metadata', to: 'saml#metadata'
  post 'saml/consume', to: 'saml#consume'

  # oembed
  match 'oembed' => 'oembed#endpoint', :via => [:get, :post]

  namespace :api do
    resources :user_assessments do
      put '/update_attempts', to: "user_assessments#update_attempts"
    end
    resources :assessments do
      get '/review', to: 'assessments#review_show'
      get '/student_review', to: 'assessments#student_review_show'
      get '/student_review_xml', to: 'assessments#student_review_show_xml'
      get 'results/:result_id', to: 'assessment_results#show'
      put '/edit', to: 'assessments#json_update'
      post '/copy', to: 'assessments#copy', as: 'assessments_copy'
      post '/remove-questions-for-guid', to: 'assessments#remove_questions_for_guid'
      post '/move-questions-for-guid', to: 'assessments#move_questions_for_guid'
    end
    resources :assessment_results do
      post 'send', to: 'assessment_results#send_result_to_analytics'
      post 'lti_outcome', to: 'assessment_results#send_lti_outcome'
    end
    post 'progress', to: 'assessment_results#log_progress'
    resources :item_results
    resources :grades
    resources :assessment_settings
    resources :lti_credentials, only: [:create, :index, :show, :update]
  end

  match '/proxy' => 'default#proxy', via: [:get, :post]
  match '/contact' => 'default#contact', via: [:get, :post]
  match '/about' => 'default#about', via: [:get]
  match '/take' => 'default#take', via: [:get]

  mount MailPreview => 'mail_view' if Rails.env.development?

end
