PrototypeR4::Application.routes.draw do

  devise_for :users
  
  # routes for sinatra app
  match '/site/set_registered', :to => CentralServerExchangeApp  
  match '/site/public_key', :to => CentralServerExchangeApp 
  match '/site/update', :to => GithubApp
  match '/purchase/:type', :to => CentralServerExchangeApp

  match 'load_orbit_bar' => 'pages#load_orbit_bar'

  # routes for admin
  namespace :admin do
    resources :assets
    resources :app_auths 
    resources :object_auths  do
      collection do
        match 'new/:type/:obj_id',:action => 'new',:via => "get",:as => :init
      end
      member do
        match ':id/create_role',:action => 'create_role',:via => "post",:as => :create_role
        match 'remove/:type/:target_id' ,:action=> 'remove_role',:via => "delete",:as =>:remove
      end
    end

    resources :ad_banners
    resources :dashboards
    resources :designs do
      collection do
        get 'upload_package'
        post 'upload_package'
      end
      member do
        post 'edit_file'  => 'designs#edit_file'
      end
    end
    resources :infos do
      get 'add_attribute_field'
    end
    resources :items
    resources :links do
      member do
        get 'delete'
      end
    end
    resources :pages do
      member do
        get 'delete'
        get 'reload_themes'
      end
    end
    resources :module_apps do
      resources :app_auths do
        member do
          match 'remove/:type/:target_id' ,:action=> 'remove',:via => "delete",:as =>:remove
        end
      end
      
      member do
        match 'assign_manager' ,:action=> 'assign_manager',:via => "post",:as =>:assign_manager
        match 'assign_sub_manager' ,:action=> 'assign_sub_manager',:via => "post",:as =>:assign_sub_manager

        match 'remove_manager/:app_manager_id' ,:action=> 'remove_manager',:via => "delete",:as =>:remove_manager
        match 'remove_sub_manager/:app_sub_manager_id' ,:action=> 'remove_sub_manager',:via => "delete",:as =>:remove_sub_manager
        get 'reload_frontend_pages'
      end
    end
    resources :page_parts
    resources :purchases do
      collection do
        get 'install_app'
      end
      member do
        get 'download'
      end
    end
    resources :roles do
      get 'add_sub_role'
      get 'add_attribute_field'
    end
    resources :sites
    resources :snippets
    resources :tags
    resources :translations
    resources :users
  end
  # end admin
  
  namespace :panel do
    resources :users
  end
  
  match '/panel/:app_name/front_end/:app_action/:id' => 'pages#show_from_link', :constraints => lambda { |request|
    !request.query_string.include?("inner=true")
  }
  
  match '/panel/:app_name/front_end/:app_action' => 'pages#index_from_link', :constraints => lambda { |request|
    !request.query_string.include?("inner=true")
  }
  
  # routes for gridfs files
  match "/gridfs/*path" => "gridfs#serve"
  # match "/images/*path" => "gridfs#serve_images"
  # match "/admin/pages/images/*path" => "gridfs#serve_images"
  
  # routes for pages to generate
  match '*page_name' => 'pages#show', :as => :page, :constraints => lambda{|request|
    !request.path.starts_with?("/panel")
  }
  
  root :to => 'pages#index'
  
end
