class Site
  include Mongoid::Document
  include Mongoid::Timestamps
  include Slug

  DEBUG = false
  has_many :page_sets, :autosave => true, :dependent => :destroy
  accepts_nested_attributes_for :page_sets, :allow_destroy => true
  field :update_flag, type: Boolean, default: true
  field :title, as: :slug_title, type: String, localize: true
  field :school, type: String
  field :department, type: String
  field :address, type: String
  field :footer, localize: true
  field :social_share, type: String
  field :sub_menu, localize: true
  field :site_type
  field :site_map_link, type: String, :default => "/sitemap"
  field :terms_of_use_link, type: String
  field :google_analytics, type: String
  field :in_use_locales, :type => Array
  field :valid_locales, :type => Array
  field :site_active, :type => Boolean
  field :frontend_open, :type => Boolean, :default => true
  field :backend_openness_on, :type => Boolean, :default => false
  field :mobile_api_openness_on, :type => Boolean, :default => false
  field :desktop_closed, :type => Boolean, :default => false
  field :enable_language_detection, :type => Boolean, :default => false
  field :enable_redirect_index, :type => Boolean, :default => true
  field :enable_zh_cn, :type => Boolean, :default => true
  field :enable_language_options, :type => Boolean, :default => true
  field :default_locale, :default => "zh_tw"
  field :mobile_on, :type => Boolean, :default => false
  field :announcement_category, :type => Array, :default=>[]
  field :mobile_bar_color, :type => Array, :default=>[]
  field :phone_number, :type => Array,:default=>[]
  field :title_always_on, :type => Boolean, :default => false
  field :display_title_in_frontend, :type => Boolean, :default => true
  field :sitemap_menu_in_header, :type => Boolean, :default => false
  field :enable_terms_of_use, :type => Boolean, :default => false
  field :search,:type => Hash
  field :site_settings
  field :template, type: String
  field :store_token
  field :enable_last_update, type: Boolean, :default => false
  field :personal_plugins_sort, type: Array, :default => []
  field :sign_up_roles, type: Array, :default => []
  field :required_sign_up_attributes , :type => Hash , :default => {}
  field :mobile_orbit_bar_title, localize: true
  field :google_oauth_enabled, :type => Boolean, :default => false
  field :google_client_id
  field :google_client_secret
  
  field :gmail_oauth2_enabled, :type => Boolean, :default => false
  field :gmail_login_flag, :type => Boolean, :default => false
  field :gmail_client_id
  field :gmail_client_secret
  field :gmail_access_token
  field :gmail_refresh_token
  field :gmail

  field :meta_tags
  field :every_page_sharer, type: Array, :default => ["facebook","line","twitter","google","printer"]

  field :sync_enabled, type: Boolean, :default => false
  field :sync_rake_task

  field :month_traffic_cache
  field :home_link, :type => String, :default=>"/"
  field :home_link_1, :type => String, :default=>"/"
  field :site_title, :type => Hash, :default=>{:zh_tw=>"主頁",:en=>"home_page"}
  field :site_title_1, :type => Hash, :default=>{:zh_tw=>"主頁",:en=>"home_page"}
  field :orbitbar_language_login_btn_mobile_configuration , type: String, :default => ""
  field :is_hidden_orbit_bar , type: Boolean, :default => false
  field :orbit_bar_background_color , type: String, :default => ""
  field :orbit_bar_background_hover_color
  field :orbit_bar_text_color , type: String, :default => "#ffffff"
  field :orbit_bar_text_hover_color
  field :orbit_bar_submenu_background_color , type: String, :default => ""
  field :orbit_bar_submenu_text_color , type: String, :default => "#ffffff"
  field :orbit_bar_animation_time , type: String, :default => "0.3s"
  field :login_text , type: String, :default => "", localize: true
  
  mount_uploader :default_image, ImageUploader
  mount_uploader :site_logo, ImageUploader
  mount_uploader :site_logo_1, ImageUploader
  mount_uploader :favicon, ImageUploader
  mount_uploader :mobile_icon, ImageUploader
  after_initialize do |record|
    if !record.new_record?
      if record.is_hidden_orbit_bar.nil?
        record.is_hidden_orbit_bar = false
        record.save
      end
      if record.orbit_bar_background_color.nil?
        record.orbit_bar_background_color = ""
        record.save
      end
      if record.orbit_bar_text_color.nil?
        record.orbit_bar_text_color = ""
        record.save
      end
      if record.orbit_bar_submenu_background_color.nil?
        record.orbit_bar_submenu_background_color = ""
        record.save
      end
      if record.orbit_bar_submenu_text_color.nil?
        record.orbit_bar_submenu_text_color = ""
        record.save
      end
      if record.orbit_bar_animation_time.nil?
        record.orbit_bar_animation_time = "0.3s"
        record.save
      end
    end
    if record.orbit_bar_background_color == '#000000' && record.orbit_bar_text_color == '#000000'
      record.update(:orbit_bar_text_color => '#ffffff')
    end
    if record.orbit_bar_submenu_background_color == '#000000' && record.orbit_bar_submenu_text_color == '#000000'
      record.update(:orbit_bar_submenu_text_color => '#ffffff')
    end
  end
  def register_site(url,university,department,email,country)
    api_key = STORE_CONFIG[:store_settings]["api_key"]
    self.generate_site_token
    store = Store.new(self.id.to_s,self.site_token,api_key)
    store.post_client(self.id.to_s,self.site_token,self.title,url,university,department,email,country)
  end
end
