class WsRosterController < WsController
    require 'xmpp4r/roster'
    require 'xmpp4r/vcard'
    require 'RMagick'
 
    def initialize
        super
 
        @storages_arr = [:clients, :rosters]
        @storages_hash = [:link_roster_client, :my_presences, :presences]
    end
 
    def initialize_storage
        @storages_arr.each do |storage|
            connection_store[storage] = []
        end
 
        @storages_hash.each do |storage|
            connection_store[storage] = {}
        end
    end
 
    ##
    # Pripoj sa na jabber ucty.
    def connect
        initialize_storage()
 
        # TODO: Pouzit najprv:
        # clients = Token.fing_user_accounts_having_to_token(session[:token])
        # ale toto, az ked budem mat dokonceny multiaccount (settings a popup)
        # TODO: skusit zrychlit
        cookies = env['rack.request.cookie_hash'] # TODO: nahlasit bug na websocket-rails, lebo sa neda pristupit ku `cookies'
        cipher_key = cookies['key']
        cipher_iv = cookies['iv']
 
        clients = session[:users].map do |jid, encrypted_pass|
            decrypted_pass = Security::decrypt(encrypted_pass, cipher_key, cipher_iv)
            {jid: jid, pass: decrypted_pass}
        end
 
        clients.each do |client|
            Thread.new do
                begin
                    client = Signin.try_login(client[:jid], client[:pass])
                    connection_store[:clients] << client
                    send_message 'app.client.connected', client.jid.strip.to_s
                    #Rails.logger.debug '!!!!! finished for ' + client_id.to_s
                rescue Signin::LoginError
                    #Rails.logger.debug '!!!!! finished FAILED for ' + client_id.to_s
                    send_message 'app.client.cannot_connect', client.jid.strip.to_s
                end
            end
        end
    end
 
    ##
    # Inicializuj roster so zoznamom ludi v nom.
    # Vrat zoznam ludi (ich JID).
    def init_roster
        all_jids = []
 
        connection_store[:clients].each do |client|
            roster = Jabber::Roster::Helper.new(client)
 
            connection_store[:rosters] << roster
            connection_store[:link_roster_client][roster] = client
 
            roster.get_roster()
            roster.wait_for_roster()
 
            roster.items.each do |jid, contact|
                all_jids << {
                    jid: jid.to_s,
                    belongsTo: client.jid.strip.to_s
                }
            end
        end
 
        trigger_success contacts: all_jids
    end
 
    ##
    # Stiahni vcard ludi v rosteri
    def start_fetching_vcards
        connection_store[:rosters].each do |roster|
            client = connection_store[:link_roster_client][roster]
 
            roster.items.each do |jid, contact|
                Thread.new do
                    vcard = get_vcard_info(client, jid.to_s)
                    send_message 'app.roster.vcard', jid: jid.to_s, vcard: vcard
                end
            end
        end
    end
 
    ##
    # Zacni pocuvat zmeny stavov v rosteri
    def start_polling_contacts_state
        start_polling_control_answer()
 
        connection_store[:rosters].each do |roster|
            start_polling_subscription(roster)