class WsChatController < WsController
 
    require 'message_builder'
 
    def start_polling_messages
        connection_store[:clients].each do |client|
            client.add_message_callback do |message|
                #noinspection RubyAssignmentExpressionInConditionalInspection
                if message.body
                    process_incoming_message(message.from, message.to, message.body, message.attribute('chat_id').to_s)
                elsif request = message.first_element('sync_contacts_request')
                    # toto mozem prijat len ako admin multichatu
                    send_contacts(message.from, message.to, request.attribute('chat_id').to_s)
                elsif answer = message.first_element('synced_contacts')
                    # toto mozem prijat len ako ucastnik multichatu (nie admin)
                    contacts = xml_contacts_to_array(answer)
                    set_contacts_in_multichat(find_client(message.to.strip.to_s), answer.attribute('chat_id').to_s, message.from.to_s, contacts)
                    sync_contacts_frontend(message.from, message.to, answer.attribute('chat_id').to_s, contacts)
                elsif answer = message.first_element('exported_chat')
                    contacts = xml_contacts_to_array(message.first_element('exported_chat'))
                    import_people_in_chat(message.from, message.to, answer.attribute('chat_id').to_s, contacts)
                elsif message.attribute('destroy_multichat')
                    destroy_multichat(message.to, message.attribute('chat_id').to_s)
                elsif message.attribute('req_update_contacts')
                    added   = xml_contacts_to_array(message.first_element('added'))
                    removed = xml_contacts_to_array(message.first_element('removed'))
                    update_attendants_in_multichat(message.from, message.to, message.attribute('chat_id').to_s, removed, added)
                elsif message.attribute('new_owner')
                    make_me_new_owner(message.to, message.attribute('chat_id').to_s)
                end
                #TODO: upozornit na pisanie spravy
                #TODO: odoslat informaciu o tom, ze pisem spravu
                #else
                #    send_message 'app.chat.messageState',
                #                 state: message.chat_state,
                #                 from: message.from.strip.to_s,
                #                 to: message.to.strip.to_s,
                #                 message: message.body,
                #                 chat_id: if message.attribute(:is_simulating) then message.attribute(:chat_id) end
            end
        end
    end
 
    def make_me_new_owner(me, chat_id)
        client = find_client(me.strip.to_s)
 
        connection_store[:opened_chats][client][chat_id][:attendants] -= [me.to_s]
        connection_store[:opened_chats][client][chat_id][:attendants] += [connection_store[:opened_chats][client][chat_id][:owner]]
 
        connection_store[:opened_chats][client][chat_id][:owner] = me.to_s
 
        contacts = connection_store[:opened_chats][client][chat_id][:attendants]
        contacts.each do |contact|
            client.send(MessageBuilder::send_multichat_contacts(client.jid.to_s, contact, chat_id, contacts))
        end
 
        send_message 'app.chat.makeMeNewOwner',
                     me: me.strip.to_s,
                     chat_id: chat_id
    end
 
    def import_people_in_chat(owner, me, chat_id, contacts)
        #Rails.logger.debug ['imported chat arrived', message.to_s, chat_id]
        client = find_client(me.strip.to_s)
        set_contacts_in_multichat(client, chat_id, owner.to_s, contacts)
 
        send_message 'app.chat.importChat',
                     owner:    owner.strip.to_s,
                     chat_id:  chat_id,
                     contacts: strip_all(contacts)
    end
 
    # Owner vytvori najprv u seba novy multichat
    def new_multichat
        me = message[:chatOwner]
        hash = Digest::SHA2.hexdigest(me)
        chat_id = hash + Time.now.to_f.to_s
        client = find_client(me)
 
        set_contacts_in_multichat(client, chat_id, client.jid.to_s)
 
        trigger_success id: chat_id
    end
 
    # Owner posle novemu cloveku informaciu o chat_id a kontaktoch
    def add_to_multichat
        client = find_client(message[:chatOwner])
 
        chat_id  = message[:chatId]
        add      = message[:jid]
        add_resource = find_multichat_supported(add)
 
        if add_resource
            connection_store[:opened_chats][client][chat_id][:attendants] << add_resource
            #Rails.logger.debug ['adding to multichat', add_resource]
 
            contacts = connection_store[:opened_chats][client][chat_id][:attendants]
 
            contacts.each do |contact|
                client.send(MessageBuilder::export_multichat(client.jid.to_s, contact, chat_id, contacts))
            end
 
            trigger_success