require 'digest/sha1'

class User < ActiveRecord::Base
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken
  include Authorization::AasmRoles
  
  # Validations
 validates_presence_of :login
 validates_length_of :login, :within => 3..40
 validates_uniqueness_of :login, :case_sensitive => false
 validates_format_of :login, :with => RE_LOGIN_OK, :message => MSG_LOGIN_BAD
 validates_format_of :name, :with => RE_NAME_OK, :message => MSG_NAME_BAD, :allow_nil => true
 validates_length_of :name, :maximum => 100
 validates_presence_of :email
 validates_length_of :email, :within => 6..100
 validates_uniqueness_of :email, :case_sensitive => false
 validates_format_of :email, :with => RE_EMAIL_OK, :message => MSG_EMAIL_BAD
  
  # Observers
  before_create :set_created_at # a bug in aasm does not set created_at when using STI
  before_update :set_updated_at # a bug in aasm does not set created_at when using STI
  
  
  # Relations
  has_and_belongs_to_many :roles
  
  # HACK HACK HACK -- how to do attr_accessible from here?
  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :login, :email, :name, :password, :password_confirmation, :real_name, :location, :contact_no
  
  # has_role? simply needs to return true or false whether a user has a role or not.  
  # It may be a good idea to have "admin" roles return true always
  def has_role?(role)
    list ||= self.roles.collect(&:name)
    list.include?(role.to_s) || list.include?('admin')
  end

  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  def self.authenticate(login, password)
   u = find(:first, :conditions => {:login => login}) # need to get the salt
   u && u.authenticated?(password) ? u : nil
  end
  
  def password_required?
    new_record? ? (crypted_password.blank? || !password.blank?) : !password.blank?
  end
  
  # Creates a new password for the user, and notifies him with an email
  def reset_password!
    password = PasswordGenerator.random_pronouncable_password(3)
    self.password = password
    self.password_confirmation = password
    self.password_reset_code = nil
    save
    
    UserMailer.deliver_reset_password(self)
  end
  
  def forgot_password
    self.make_password_reset_code
    save
    UserMailer.deliver_forgot_password(self)
  end
  
  def self.find_by_login_or_email(login_or_email)
    find(:first, :conditions => ['login = ? OR email = ?', login_or_email, login_or_email])
  rescue
    nil
  end
  
  #filter out the profile attributes
  def profile_attributes
    ['real_name', 'location', 'contact_no']
  end
    
protected

  def make_activation_code
    self.deleted_at = nil
    self.activation_code = self.class.make_token
  end

  def make_password_reset_code
    self.password_reset_code = self.class.make_token
  end
  
  def set_created_at
    self.created_at = Time.now
    self.updated_at = Time.now    
  end
  
  def set_updated_at
    self.updated_at = Time.now
  end  
  
  
  
  
# --------

  # Virtual attribute for the unencrypted password
  # attr_accessor :password
  
  # Per page pagination
  # TODO: Replace with searchgasm
  # cattr_reader :per_page
  # @@per_page = 25




  # Encrypts some data with the salt.
  # def self.encrypt(password, salt)
  #   Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  # end


  

  
    # before filter 
    # def encrypt_password
    #   return if password.blank?
    #   self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
    #   self.crypted_password = encrypt(password)
    # end
    

    
    # def do_delete
    #   self.deleted_at = Time.now.utc
    # end
    # 
    # def do_activate
    #   self.activated_at = Time.now.utc
    #   self.deleted_at = self.activation_code = nil
    # end
    

end
