# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  email                  :string(255)      default(""), not null
#  encrypted_password     :string(255)      default(""), not null
#  reset_password_token   :string(255)
#  reset_password_sent_at :datetime
#  remember_created_at    :datetime
#  sign_in_count          :integer          default(0), not null
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string(255)
#  last_sign_in_ip        :string(255)
#  confirmation_token     :string(255)
#  confirmed_at           :datetime
#  confirmation_sent_at   :datetime
#  unconfirmed_email      :string(255)
#  created_at             :datetime
#  updated_at             :datetime
#  name                   :string(255)
#  channel_key            :string(255)
#
# Indexes
#
#  index_users_on_confirmation_token    (confirmation_token) UNIQUE
#  index_users_on_email                 (email) UNIQUE
#  index_users_on_reset_password_token  (reset_password_token) UNIQUE
#

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable, :confirmable

  has_many :joinings, dependent: :destroy
  has_many :communities, through: :joinings
  has_many :players, dependent: :destroy
  has_many :rooms, through: :players
  has_many :illustrations, dependent: :destroy
  has_one :profile, dependent: :destroy

  validates :name, uniqueness: true, presence: true, length: { maximum: 20 }

  before_create :generate_channel_key

  def joined?(community)
    Joining.where(user_id: self.id, community_id: community).count > 0
  end

  def has_room_role?(room, name)
    role = self.players.find_by(room: room).player_role
    if role.nil?
      false
    else
      role.name == name
    end
  rescue ActiveRecord::RecordNotFound
    return false
  end

  def is_owner?(room)
    room.owner == self
  end

  private
  def generate_channel_key
    begin
      key = SecureRandom.urlsafe_base64
    end while User.where(:channel_key => key).exists?
    self.channel_key = key
  end
end
