class WsChatController < WsController
 
    def start_polling_messages
        connection_store[:clients].each do |client|
            client.add_message_callback do |message|
                from = message.from.strip.to_s
                to   = message.to.strip.to_s
                chat_id = message.attribute('chat_id').to_s
 
                #noinspection RubyAssignmentExpressionInConditionalInspection
                if message.body
                    process_incoming_message(from, to, message.body, chat_id)
                elsif request = message.first_element('sync_contacts_request')
                    # toto mozem prijat len ako admin multichatu
                    send_contacts(from, 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)
                    sync_contacts_frontend(from, 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(from, to, answer.attribute('chat_id').to_s, contacts)
                elsif message.attribute('destroy_multichat')
                    destroy_multichat(to, chat_id)
                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(from, to, chat_id, removed, added)
                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 import_people_in_chat(from, to, chat_id, contacts)
        #Rails.logger.debug ['imported chat arrived', message.to_s, chat_id]
        client = find_client(to)
        create_opened_chat(client, chat_id, from, contacts)
 
        send_message 'app.chat.importChat',
                     owner:    from,
                     chat_id:  chat_id,
                     contacts: 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)
 
        create_opened_chat(client, chat_id, me)
 
        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]
 
        connection_store[:opened_chats][client][chat_id][:attendants] << add
 
        contacts = connection_store[:opened_chats][client][chat_id][:attendants]
 
        contacts.each do |contact|
            client.send(MessageBuilder::export_multichat(client.jid.strip.to_s, contact, chat_id, contacts))
        end
 
        trigger_success
    end
 
    def send_chat_message
        me = message[:from]
        client = find_client(me)
 
        if client
            chat_id = message[:chatId]
            if chat_id
                me = client.jid.strip.to_s
                attendants = connection_store[:opened_chats][client][chat_id][:attendants] + [connection_store[:opened_chats][client][chat_id][:owner]]
                attendants -= [me]
 
                messages = MessageBuilder::build_multi_messages(message[:message], me, attendants, chat_id)
                #Rails.logger.debug messages
            else
                messages = [MessageBuilder::build_message(message[:message], me, message[:to])]
            end
 
            # Xmpp4r doesn't support XEP-0033 (multicast messages)
            messages.each do |message|
                client.send(message)