// 名前空間
var CHAT_MAKE_ROOM = {};

$(function() {
    // メンバー検索
    $('#chatMakeRoom .search_form input[type="search"]').click(function(e) {
        let contactListTitle = getLocalizedString("userSearch");
        $('#makeRoomTitle').text(contactListTitle);
    });
    $('#chatMakeRoom .search_form input[type="search"]').keyup(function(e) {
        //画面タイトル設定
        var keyword = $('#chatMakeRoom .search_form input[type="search"]').val();
        if (e.KeyCode == 13 || e.key == "Enter") {
            if (keyword != '' && keyword.length != 0) {
                 $('#chatMakeRoom .search_form input[type="search"]').blur();
                return false;
            }
        } else if (keyword == '' || keyword.length < 2) {
            $('.overlay_src_msg').empty();
            return false;
        }
        CHAT_MAKE_ROOM.searchUser(keyword);
        if (e.key == "Enter" || e.KeyCode == 13) {
            $('#chatMakeRoom .search_form input[type="search"]').blur();
            return;
        }
    });
    // iOSキーボード変換検知用
    $('#chatMakeRoom .search_form input[type="search"]').on('compositionend', function()　{
        if (CHAT_UTIL.isIOS()) {
            var keyword = $('#chatMakeRoom .search_form input[type="search"]').val();
            CHAT_MAKE_ROOM.searchUser(keyword);
        }
    });  
});

// メンバー検索
CHAT_MAKE_ROOM.searchUser = function(keyword) {
    var isAllGroup = $('#tabAllGroupOnMakeRoom').is(':checked');
    $('.overlay_src_msg').empty();

    //全グループ検索画面
    if (isAllGroup) {
        //グループデータ検索
        var groupList = CHAT_DB.getGroupByName(keyword);
        var groupTemplate;
        $.get({ url: "./template/template_make_room_group_list.html", async: false }
            , function(text) {
                groupTemplate = text;
        });

        groupList.forEach(function(group) {
            let html = Mustache.render(groupTemplate, {
                name: group.groupName,
                id: group.groupId,
            });

            let obj = jQuery.parseHTML(html);
            $('.overlay_src_msg').append(obj);
        })

        //ユーザデータ検索
        var userList = CHAT_DB.getAllGroupShopMemberByName(keyword);
        var userTemplate;
        $.get({ url: "./template/template_make_room_user_list.html", async: false }
            , function(text) {
                userTemplate = text;
        });
        userList.forEach(function(user) {
            user.profileUrl = CHAT.getProfileImgUrl(user.profileUrl);
            let findObj = CHAT.globalSelectedUserList.find(function(shopMemberId) {
                return shopMemberId == user.shopMemberId;
            })
            if (findObj) {
                user.checked = 'checked';
            }
        });
        let html = Mustache.render(userTemplate, {
            userList: userList
        });
        let obj = jQuery.parseHTML(html);
        $('.overlay_src_msg').append(obj);
        if (groupList.length == 0 && userList.length == 0) {
            const noResultMsg = $('<div/>',{width:'auto', style:'text-align: center'});
            noResultMsg.append(getLocalizedString("noResult"))
            $('.overlay_src_msg').append(noResultMsg);
        }
　　//連絡先画面
    } else {
        var userList = CHAT_DB.getMyGroupShopMemberByName(keyword);
        var userTemplate;
        $.get({ url: "./template/template_make_room_user_list.html", async: false }
            , function(text) {
                userTemplate = text;
        });
        userList.forEach(function(user) {
            user.profileUrl = CHAT.getProfileImgUrl(user.profileUrl);
            let findObj = CHAT.globalSelectedUserList.find(function(shopMemberId) {
                return shopMemberId == user.shopMemberId;
            })
            if (findObj) {
                user.checked = 'checked';
            }
        });
        let html = Mustache.render(userTemplate, {
            userList: userList
        });
        let obj = jQuery.parseHTML(html);
        $('.overlay_src_msg').html(obj);
        if (userList.length == 0) {
            const noResultMsg = $('<div/>',{width:'auto', style:'text-align: center'});
            noResultMsg.append(getLocalizedString("noResult"))
            $('.overlay_src_msg').append(noResultMsg);
        }
    }
}
