class Admin < ApplicationRecord
  
  #attr_accessible :login, :email, :firstname, :name, :password, :password_confirmation, :avatar, :moderator, :as => :admin
  
  
  has_many :admin_taskables, :foreign_key => :admin_owner_id
  accepts_nested_attributes_for :admin_taskables
  
  has_secure_password
 
  attr_accessor :login
 
  validates :password, :presence => true,
                         :confirmation => true,
                         :length => {:within => 6..40},
                         :on => :create
                         
  validates :password, :confirmation => true,
                         :length => {:within => 6..40},
                         :allow_blank => true,
                         :on => :update
 validates :password_confirmation, :presence => true,
    :unless => Proc.new { |a| a.password.blank? }
 
 
  validates :email, :presence => true, :uniqueness => true
  validates :username, :presence => true, :uniqueness => true
  
#  mount_uploader :avatar, AvatarUploader

  before_create { generate_token(:remember_token) }


  has_many :admin_preferences


  after_save do
    ids_to_add = Admin.all.ids
    
    ids_to_add = ids_to_add - self.admin_taskables.map{|a| a.admin_id}
    if self.id
      ids_to_add = ids_to_add - [self.id]
    end
    puts "A"
    puts ids_to_add
    #sfdfsd = fdssfd
    
    ids_to_add.each do |id_to_add|
      self.admin_taskables.create(:admin_id => id_to_add)
      
    end
    
    
  end


  def admins_crud_taskable
    if self.super_admin or self.has_permission?("to-do-crud")
      Admin.all
    else
      Admin.where(:id => ([self.id]+self.admin_taskables.where(:crud => true).map{|a| a.admin_id}))
    end
  end
  
  def admins_read_taskable
    if self.super_admin or self.has_permission?("to-do-read")
      Admin.all
    
    else
      Admin.where(:id => ([self.id]+self.admin_taskables.where(:read => true).map{|a| a.admin_id}))
    end
  end


  def show_name
    self.firstname.to_s + " " + self.name.to_s
  end

  
  def send_password_reset
    generate_token(:reset_password_token)
    self.reset_password_sent_at = Time.now
    save!
    AdminMailer.password_reset(self).deliver
  end

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while Admin.exists?(column => self[column])
  end
  
  def self.find_by_login(login)
    
    Admin.where("username = ? OR email = ?", login,login).first
    
  end 
  has_many :admin_admin_roles
  has_many :admin_roles, :through => :admin_admin_roles
  has_many :admin_permissions, :through => :admin_roles
  
  
  def has_permission?(permission_slug)
    if self.super_admin
      true
    else
      permission = AdminPermission.where(:slug => permission_slug).first
    
      if permission
        if self.admin_permissions.include?(permission)
          true
        else
          false
        end
      else
        false
      
      end
    end
    
  end
  
  after_create do
    Admin.all.each do |a|
      a.save
    end
  end
  
end