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

$(function () {
    // メンバー検索
    $('#contact .search_form input[type="search"]').keyup(function(e) {
        var keyword = $('#contact .search_form input[type="search"]').val();
        if (e.key == "Enter" || e.KeyCode == 13) {
            if (keyword != '' && keyword.length != 0) {
                $('#contact .search_form input[type="search"]').blur();
                return;
            }
        } else if (keyword == '' || keyword.length < 2) {
            $('.overlay_src_msg').empty();
            return;
        }
        CONTACT.searchUser(keyword);
        if (e.key == "Enter" || e.KeyCode == 13) {
            $('#contact .search_form input[type="search"]').blur();
            return;
        }
    });
    // iOSキーボード変換検知用
    $('#contact .search_form input[type="search"]').on('compositionend', function()　{
        if (CHAT_UTIL.isIOS()) {
            var keyword = $('#contact .search_form input[type="search"]').val();
            CONTACT.searchUser(keyword);
        }
    }); 
});

// ユーザー検索
CONTACT.searchUser = function(keyword) {
    var groupList;
    $('.overlay_src_msg').empty();
    var isAllGroup = $('#tabAllGroup').is(':checked');

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

        groupList.forEach(function(group) {
            let html = Mustache.render(groupTemplate, {
                name: group.groupName,
                id: group.groupId,
                isFavorite: group.isFavorite
            });
            let obj = jQuery.parseHTML(html);
            $('.overlay_src_msg').append(obj);
        })

        //ユーザデータ検索
        var userList = CHAT_DB.getAllGroupShopMemberByName(keyword);
        var userTemplate;
        $.get({ url: "./template/template_user_list.html", async: false }
            , function(text) {
                userTemplate = text;
        });
        userList.forEach(function(user) {
            user.profileUrl = CHAT.getProfileImgUrl(user.profileUrl);
        });
        let html = Mustache.render(userTemplate, {
            userList: userList
        });
        let obj = jQuery.parseHTML(html);
        $('.overlay_src_msg').append(obj);
        if (userList.length == 0 && groupList.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_user_list.html", async: false }
            , function(text) {
                userTemplate = text;
        });
        userList.forEach(function(user) {
            user.profileUrl = CHAT.getProfileImgUrl(user.profileUrl);
        });
        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);
        }
    }
};
