class SiteServer
  include Mongoid::Document
  include Mongoid::Timestamps
  include OrbitCategory::Categorizable
  include OrbitTag::Taggable
  include OrbitModel::Status
  field :server_name , type: String ,default: ''
  field :enable_redirect_default_domain, type: Boolean, default: false
  field :default_domain_names, type: Array, default: []
  field :domain_name , type: String ,default: ''
  field :domain_names, type: Array, default: []
  field :ip , type: String , default: ''
  field :port, type: String, default: '22'
  field :account , type: String ,default: ''
  field :password , type: String ,default: ''
  field :active , type: Boolean ,default: true
  field :has_certbot, type: Boolean , default: false
  field :need_update_site_ids, type: Array, default: []
  field :super_user_changed, type: Boolean, type: false
  field :removed_super_users, type: Array, default: []
  field :need_rewrite_backup_setting, type: Boolean, default: false
  has_many :site_server_file_backups, :autosave => true, :dependent => :destroy
  has_many :site_server_db_backups, :autosave => true, :dependent => :destroy
  has_many :site_super_users, :autosave => true, :dependent => :destroy
  accepts_nested_attributes_for :site_server_file_backups, :allow_destroy => true
  accepts_nested_attributes_for :site_server_db_backups, :allow_destroy => true
  accepts_nested_attributes_for :site_super_users, :allow_destroy => true
  def check_super_user_changed
    self.update(:super_user_changed => (self.site_super_users.where(:is_changed=>true).count != 0 || self.removed_super_users.count != 0))
  end
  def domain_names
    if self.domain_name != ''
      [self.domain_name]
    else
      super
    end
  end
  def site_constructs
    SiteConstruct.where(server_type: self.server_name)
  end
  before_save do
    if self.domain_name != '' && self.domain_names.length !=0
      self.domain_name = ''
    end
    if !self.new_record? && self.account_changed?
      SiteServerFileBackup.where(:backup_dir=>"/home/#{self.account_was}").update_all(:backup_dir=>"/home/{{user_name}}")
    end
    tmp_site_constructs = site_constructs
    need_change = false
    if self.enable_redirect_default_domain_changed?
      tmp_site_constructs = tmp_site_constructs.where(:enable_redirect_default_domain.in=>[nil,0])
      need_change = true
    end
    if (need_change || (!need_change && self.enable_redirect_default_domain))
      if self.default_domain_names_changed?
        check_domains = []
        removed_default_domains = self.default_domain_names_was.to_a - self.default_domain_names
        add_default_domains = self.default_domain_names - self.default_domain_names_was.to_a
        check_domains += removed_default_domains
        check_domains += add_default_domains
        check_domains = check_domains.map{|domain| ::Regexp.new("(\\A|\\s)"+domain.gsub(".","\\.").gsub("*","[^.]*") + "($|\\s)")}
        tmp_site_constructs = tmp_site_constructs.where(:domain_name.in=>check_domains)
        need_change = true
      elsif need_change
        check_domains = self.default_domain_names
        check_domains = check_domains.map{|domain| ::Regexp.new("(\\A|\\s)"+domain.gsub(".","\\.").gsub("*","[^.]*") + "($|\\s)")}
        tmp_site_constructs = tmp_site_constructs.where(:domain_name.in=>check_domains)
      end
    end
    if need_change
      self.need_update_site_ids += tmp_site_constructs.pluck(:id)
      self.need_update_site_ids = self.need_update_site_ids.uniq
    end
  end
end