var accountUserGroup_form = {
    settings: {
        account_user_autocomplete_url: '',
        chat_listUrl: null,
    },
    account_users: [],
    account_user_names: [],
    isGroupChat: false,
    init: function() {
        chat_form.addAccountBtn();
        chat_form.removeAccountBtn();
        chat_form.initAutocomplete();
        global.askContinue();
    },
    addAccountBtn: function() {
        jQ("#add_account_user").unbind('click').bind('click',function(e) {
            const i = jQ('.chatParticipants_account_user').length;
            let el =  '<div class="input-group">'  +
                    '<input type="text" class="chatParticipants_account_user_autocomplete" id="rxconnexionbundle_chatentity_chatParticipants_' + i + '_account_user_name" name="rxconnexionbundle_chatentity[chatParticipants][' + i + '][account_user_name]" required="required">'  +
                    '<input type="hidden" class="chatParticipants_account_user" id="rxconnexionbundle_chatentity_chatParticipants_' + i + '_account_user" name="rxconnexionbundle_chatentity[chatParticipants][' + i + '][account_user]" required="required">'  +
                    '<div class="input-group-button">'  +
                    '<button type="button" class="button warning remove_account_user" ><i class="fa fa-close"></i></button>'  +
                    '</div>'  +
                    '</div>' ;
            jQ("#account_user_container").append(el)
            chat_form.removeAccountBtn();
            chat_form.initAutocomplete();
        })
    },
    removeAccountBtn: function() {
        jQ(".remove_account_user").unbind('click').bind('click',function(e) {
            jQ(this).parent().parent().remove();
            chat_form.parseChatLabel();
            chat_form.parseChatAccountUserId();
        });
    },
    initAutocomplete: function() {
        jQ(".chatParticipants_account_user_autocomplete").each(function(i,el){
            let desc = jQ(el).attr('id');
            let id = jQ(el).siblings('.chatParticipants_account_user').attr('id');
            global.autocomplete.bind(chat_form.settings.account_user_autocomplete_url, '#'+desc, '#'+id,{
                onSelect: function(){
                    let user_id = jQ('#'+id);
                    let user_name = jQ('#'+desc);
                    if(chat_form.account_users.includes(user_id.val())) {
                        global.parseFlashMessages('#flash-messages-content-modal', 'warning', ['The account you have selected is already in the Chat Participant(s) list']);
                        // user_id.parent().remove();
                        user_id.val('');
                        jQ('#'+desc).val('');
                    } else {
                        chat_form.account_users.push(user_id.val());
                    }
                    chat_form.parseChatLabel();
                    chat_form.parseChatAccountUserId();
                }
            });

        })
    },
    parseChatLabel: function() {
        chat_form.account_user_names = [];
        jQ(".chatParticipants_account_user_autocomplete").each(function(i,el){
            if(jQ(el).val() != '')
                chat_form.account_user_names.push(jQ(el).val());
        });
        let chat_label = '';
        let chat_type = ''
        for(let i = 0; i < chat_form.account_user_names.length; i++)  chat_label += (chat_label == '' ? '' : ', ') + chat_form.account_user_names[i];
        if(chat_form.account_user_names.length > 1)
            chat_type = 'Group';
        else
            chat_type = 'Private';
        jQ("#rxconnexionbundle_chatentity_description").val(chat_label);
        jQ("#rxconnexionbundle_chatentity_chat_type").val(chat_type);
    },
    parseChatAccountUserId: function() {
        chat_form.account_users = [];
        jQ(".chatParticipants_account_user").each(function(i,el){
            if(jQ(el).val() != '')
                chat_form.account_users.push(jQ(el).val());
        });
    }
};