Orbit::Application.routes.draw do

  mount Ckeditor::Engine => '/ckeditor'

  # get "robots.txt" => 'robots#index'

  devise_for :users, :controllers => { :registrations => "registrations", :passwords => 'passwords', :confirmations => 'confirmations' } do
    resources :passwords, :except => [:index, :show, :destroy]
    match "/users_passwd" => "desktop/registrations#update", :as => :users_passwd, :via => :put
  end

  mount Resque::Server, :at => "/admin/resque"
  mount Rack::GridFS::Endpoint.new(:db => Mongoid.database,:lookup=>:path), :at => "gridfs"

  # 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'
  get 'basic_infos' => "basic_infos#index"
  get 'basic_infos/form' => "basic_infos#form_page"
  get 'basic_infos/role_page' => "basic_infos#role_page"
  get 'basic_infos/check_availability' => "basic_infos#check_availability"
  get 'basic_infos/confirmation' => "basic_infos#confirmation"
  post 'basic_infos/role_update' => "basic_infos#role_update"
  match 'basic_infos/basic_info_update' => "basic_infos#basic_info_update"

  get "site_search" => "site_search#search"

  # routes for admin
  namespace :admin do
    match 'user_actions' => 'user_actions#index'
    match 'system_preference' => "sites#show_system_preference",:as=>"system_preference"
    mount Resque::Server.new, :at => "/resque"
    resources :assets do   
      collection do
        post 'file_upload'
        get 'delete'
        post 'delete_files'
      end
    end

    resources :mail_crons do   
      collection do
        get 'delete'
      end
    end

    resources :mail_cron_logs do   
      collection do
        get 'delete'
      end
    end
    
    resources :asset_categories
    resources :asset_tags
    resources :app_auths

    resources :object_auths do
      match 'new_interface/:ob_type/:title/new' => "object_auths_new_interface#new" ,:as => :init_ob_auth,:via => :get
      match 'new_interface' => "object_auths_new_interface#setting" ,:as => :ob_auth,:via => :get
      match 'new_interface' => "object_auths_new_interface#update_setting" ,:as => :ob_auth,:via => :post
      match 'new_interface' => "object_auths_new_interface#user_list" ,:as => :ob_auth_show,:via => :put 
    end

    match 'ad_banners/:id/preview' => 'ad_banners#realtime_preview',:as => :realtime_preview_ad_banner,:via => :put
    match 'ad_banners/:id/rename' => 'ad_banners#rename',:as => :rename_ad_banner,:via => :post

    resources :ad_images ,:except => [:show]

    resources :ad_banners #do 
    #  collection do
    #    match 'new_ad_image' => 'ad_images#new',:as => :new_ad_image,:via => :get
    #    match 'new_ad_image' => 'ad_images#create',:as => :create_ad_image,:via => :post
    #  end
    #  resources :ad_images ,:except => [:show]
    #end

    match 'authorizations/add_roles/:module(/:type(/:id))' => 'authorizations#add_roles', :as => :add_roles_authorizations
    match 'authorizations/add_users/:module(/:type(/:id))' => 'authorizations#add_users', :as => :add_users_authorizations
    match 'authorizations/modal_select/:module(/:type(/:id))' => 'authorizations#modal_select', :as => :modal_select_authorizations
    match 'authorizations/remove_users/:module(/:type(/:id))' => 'authorizations#remove_users', :as => :remove_users_authorizations, :via => :delete
    match 'authorizations(/:module(/:type(/:id)))' => 'authorizations#index', :as => :authorizations

    resources :dashboards do
      collection do
        get 'reload_all_content'
        get 'reload_most_visited'
        get 'reload_recent_update'
        get 'get_cpu_usage'
        get 'get_mem_usage'
        get 'get_disk_usage'
        get 'get_month_traffic'
      end
    end
    resources :designs do
      collection do
        get 'upload_package'
        get 'delete'
        post 'upload_package'
      end
      member do
        post 'edit_file'  => 'designs#edit_file'
        post 'update_file' => 'designs#update_file'
        post 'upload_image' => 'designs#upload_image'
      end

    end
        match 'design_list' => 'designs#design_list'

    resources :infos do
      get 'add_attribute_field'
    end

    resources :items do
      collection do
        get 'get_page_design_module_json'
        post 'update_position'
      end
    end

    resources :links do
      collection do
        get 'get_link_setting_json'
      end
      member do
        get 'delete'
      end
    end

    resources :pages do
      collection do
        get 'get_display_style'
        get 'get_page_setting_json'
      end
    end

    resources :module_apps do
      # match 'manager_auth_proc' => "module_apps_new_interface#index", :as => :manager_auth_proc, :via => :get
      match 'manager_auth_proc' => "module_apps_new_interface#setting" ,:as => :manager_auth_proc,:via => :get
      match 'manager_auth_proc' => "module_apps_new_interface#update_setting" ,:as => :manager_auth_proc,:via => :post
      match 'manager_auth_proc' => "module_apps_new_interface#user_list" ,:as => :manager_auth_show,:via => :put
      resources :app_auths do
        member do
          match 'remove/:type/:target_id' ,:action=> 'remove',:via => "delete",:as =>:remove
        end
      end
      resources :categories do
        member do
          post 'toggle'
        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
      end
    end

    resources :module_tags do
      collection do
        post 'delete_tags'
        post 'merge'
        post 'remove_default'
        post 'update_included_default'
      end
    end

    resources :page_parts do
      collection do
        get 'get_display_style'
        get 'get_page_module_json'
        get 'get_page_setting_json'
      end
      member do
        get 'reload_r_tag_options'
      end
    end

    resources :plugins
    resources :purchases do
      collection do
        get 'install_app'
      end
      member do
        get 'buy_template'
        get 'download'
      end
    end

    resources :roles do
      get 'role_field'
      get 'sub_role_field'
      get 'role_status'
      get 'role_category'
      get 'sub_role'
      get 'add_sub_role'
      get 'edit_sub_role'
      get 'add_attribute_field'
      post 'toggle'
      post 'sub_role_toggle'
    end

    resources :role_filters do
      post 'toggle'
    end

    resources :role_statuses do
      post 'toggle'
    end
    
    resources :role_categorys do
      post 'toggle'
    end

    resources :sites do
      get 'mail_setting'
      get 'site_info'
      get 'responsive_setting'
      get 'search_engine'
      get 'sitemap'
      get 'sitemap_frontend'
      get 'sitemap_toggle', :on => :member
      get 'system_info'
      get 'ui_theme'
      get 'change_design'
      get 'reset_default_locale'
      get 'preference'
      get 'update_manager'
      get 'get_update_history'
      get 'check_updates'
      get 'update_orbit'
      get 'restart_server'
    end
    match 'sites/register_site' => "sites#register_site"

    resources :tags do
      collection do
        post 'add_to_default'
        post 'delete_tags'
        post 'merge'
      end
    end
    resources :users

    match "users_new_interface/registration_approval" => "users_new_interface#registration_approval"
    match "users_new_interface/approve_selected" => "users_new_interface#approve_selected"
    match "users_new_interface/delete_selected" => "users_new_interface#delete_selected"

    resources :users_new_interface do
      member do
        get 'temp_edit'
      end
      collection do
        get 'edit_order_card'
        get 'edit_order_list'
        post 'update_order_card'
        post 'update_order_list'
      end
      match "edit_passwd" => "users_new_interface#edit_passwd" ,:as => :edit_passwd
      match "edit_user_data" => "users_new_interface#edit_user_data" ,:as => :edit_user_data
      match "setting_account" => "users_new_interface#setting_account" ,:as => :setting_account
      match "edit_privilege" => "users_new_interface#edit_privilege" ,:as => :edit_privilege
      match "approve_registration" => "users_new_interface#approve_registration", :as => :approve_registration

    end

    resources :member_selects do
      match 'member_select_search' => "member_selects#member_select_search" ,:as => :member_select_search,:via => "post"
      match 'member_select_add' => "member_selects#member_select_add" ,:as => :member_select_add,:via => "post"
      collection do
        get 'select_members'
        post 'set_roles'
        post 'set_users'
        get 'select_email_members'
        post 'set_email_roles'
        post 'set_email_users'
      end
    end

    match 'site_registration/register' => 'site_registration#register'
    resources :site_registration do
    end

   

    match 'module_store' => 'module_store#index'
    match 'module_store/show' => 'module_store#show'
    match 'module_store/download' => 'module_store#download'
    match 'module_store/remove_module' => 'module_store#remove_module'
    match 'module_store/toggle_module' => 'module_store#toggle_module'
    match 'module_store/restart_server' => 'module_store#restart_server'
    match 'template_store' => 'template_store#index'
    match 'template_store/download_theme' => "template_store#download_theme"
    match 'template_store/template/:id' => 'template_store#show', :as => :template_store_template
    # match 'template_store/cool_method' => "template_store#cool_method"
    match "import/rss2_news" => "import#rss2_news"
  end
  # end admin

  namespace :panel do
    resources :users
  end

  resources :desktop_widgets do
    collection do
      get 'upload'
      get 'delete'
      post 'upload'
    end
  end
   match "desktop_themes/settings" => "desktop_themes#settings"
  resources :desktop_themes do

    collection do
      get 'upload'
      get 'delete'
      post 'upload'
    end

  end

  resources :desktop_apps do
    collection do
      get 'upload'
      get 'delete'
      post 'upload'
    end
  end

  match "/desktop/compatibility" =>  "desktop#compatibility"
  match "desktop" =>  "desktop#index"
  match "/desktop/desktop_container" => "desktop#desktop_container"
  match '/desktop/appactivation'=>'desktop#appactivation'
  match '/desktop/app_manager'=>'desktop#app_manager'
  match '/desktop/allsections'=>'desktop#allsections'
  match '/desktop/settings'=>'desktop#settings'
  match '/desktop/get_desktop_settings/'=>'desktop#get_desktop_settings'
  match '/desktop/save_desktop_settings/'=>'desktop#save_desktop_settings'
  match '/desktop/getgroups/'=>'desktop#getgroups'
  match '/desktop/getsectionlist/'=>'desktop#getsectionlist'
  match '/desktop/get_themes/'=>'desktop#get_themes'
  match '/desktop/sections/'=>'desktop#sections'
  match '/desktop/getapplist/'=>'desktop#getapplist'
  match '/desktop/getapplistforManager/'=>'desktop#getapplistforManager'
  match '/desktop/newpositions/'=>'desktop#newpositions'
  match '/desktop/connections/'=>'desktop#connections'
  match '/desktop/widget_layout' => 'desktop#widget_layout'
  match '/desktop/temp_func/'=>'desktop#temp_func'

  # namespace :desktop_research do
  match '/twitter/'=>'otheraccounts#twitter'
  match '/forgmail/'=>'otheraccounts#gmail'
  match '/otheraccounts/getaccounts'=>'otheraccounts#getaccounts'
  match '/desktop/save_account_info/'=>'otheraccounts#saveaccountinfo'

  match '/desktop_appstore/appstore'=>'desktop_appstore#appstore'
  match '/desktop_appstore/onlinestore'=>'desktop_appstore#onlinestore'
  match '/desktop_appstore/widgets'=>'desktop_appstore#widgets'
  match '/desktop_appstore/getuserwidgets'=>'desktop_appstore#getuserwidgets'
  match '/desktop_appstore/widgets_settings'=>'desktop_appstore#widgets_settings'

  match '/desktop_orbit/orbit' => 'desktop_orbit#orbit'

  match '/desktop_orbit/eventajaxload'=> 'desktop_orbit#eventajaxload'
  match '/desktop_orbit/gettimelinespan' => 'desktop_orbit#gettimelinespan'

  match "/facebook/register_fb" => "facebook#register_fb"
 
  match "/facebook/get_friends" => "facebook#get_friends"
  match "/facebook/get_feed" => "facebook#get_feed"
  match "/facebook/disconnect" => "facebook#disconnect"
  match "/facebook/server" => "facebook#server_redirect"
  match "/facebook/profile_import" => "facebook#profile_import"
  match "/desktop/wallpaper_upload" => "desktop#wallpaper_upload"

  match "/store_confirmation/confirm" => "store_confirmation#confirm"

  #match '/desktop_orbit/eventajaxload' => 'desktop_publications#create_journal'
  #match '/desktop_orbit/eventajaxload' => 'desktop_publications#delete_journal'
  # match '/desktop_orbit/eventajaxload' => 'desktop_publications#update_journal'


  def controller_paths(controller, actions, scope=nil) 
    actions.each do |action| 
      if action.eql? 'index'
        match "#{controller}" => "#{controller}##{action}", :as => "#{controller}"
      else
        match "#{controller}/#{action}(/:id)" => "#{controller}##{action}", :as => "#{controller}_#{action}"
      end
    end 
  end

  controller_paths :front, %w[show_breadcrumb show_banner show_footer show_menu show_page_sub_menu show_site_sub_menu show_sitemap show_tag_cloud  show_terms_of_use]
  # controller_paths :mobile, %w[index announcement announcement_content dialog_contact dialog_copyright dialog_language map page page_content]

  # scope 'app' do
  #   controller_paths :mobile, %w[index announcement announcement_content dialog_contact dialog_copyright dialog_language map page page_content]
  # end


  resources :api do
    collection do
      get 'get_albums'
      get ':id/get_images', to: 'api#get_images', as: 'album_images'
      get 'get_location_categories'
      get 'get_locations'    
    end
    member do
      get 'images'
    end
  end

  scope '/mobile(/:app)' do
    match '/announcement' => 'mobile#announcement', :as => 'mobile_announcement'
    match '/announcement_content/:id' => 'mobile#announcement_content', :as => 'mobile_announcement_content'
    match '/dialog_contact' => 'mobile#dialog_contact', :as => 'mobile_dialog_contact'
    match '/dialog_copyright' => 'mobile#dialog_copyright', :as => 'mobile_dialog_copyright'
    match '/dialog_language' => 'mobile#dialog_language', :as => 'mobile_dialog_language'
    match '/map' => 'mobile#map', :as => 'mobile_map'
    match '/page' => 'mobile#page', :as => 'mobile_page'
    match '/page_content/:id' => 'mobile#page_content', :as => 'mobile_page_content'
    root :to => 'mobile#index', :as => 'mobile'
  end

  match '/panel/orbit_app/widget/:type' => 'pages#index_from_link', :constraints => lambda { |request|
    request.query_string.include?("same_page_id=")
  }
  match '/panel/orbit_app/widget/:type' => 'default_widget#default_widget'


  match '/panel/:app_name/front_end/:app_action/:id(/:controller_action)' => 'pages#show_from_link', :constraints => lambda { |request|
    !request.query_string.include?("inner=true") && !request.query_string.include?("standalone=true")
  }

  match '/panel/:app_name/front_end/:app_action' => 'pages#index_from_link', :constraints => lambda { |request|
    !request.query_string.include?("inner=true") && !request.query_string.include?("standalone=true")
  }

  match '/share/:model/:id' => 'application#render_share'

  # 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'

  match '/panel/ad_banner/front_end/inner_page/:id' => 'front#show_inner_ad_image', :as => :front_show_inner_ad_image

end
