  module JamWebsockets
  class ClientContext
    
    attr_accessor :user, :client, :msg_count, :session, :client_type, :sent_bad_state_previously, :active, :updated_at

    def initialize(user, client, client_type)
      @user = user
      @client = client

      @client_type = client_type
      @msg_count = 0
      @session = nil
      @sent_bad_state_previously = false
      @active = true
      @updated_at = Time.now
      client.context = self
    end

    def stale?(stale_time)
      return Time.now - @updated_at > stale_time
    end

    def to_s
      return "Client[user:#{@user} client:#{@client.client_id} msgs:#{@msg_count} session:#{@session} client_type:#{@client_type} channel_id: #{@client.channel_id}]"
    end

    def to_json
      {user_id: @user.id, client_id: @client.client_id, msg_count: @msg_count, client_type: @client_type, socket_id: @client.channel_id}.to_json
    end

    def hash
      @client.hash
    end

    def ==(o)
      o.class == self.class && o.client == @client
    end
    alias_method :eql?, :==

  end
end
