﻿var FriendshipInteractions = {
    /* excluir todos os amigos com checked */
    AllDeleteFriednship: function () {
        $('.FriendshipListRef li').each(function (i, e) {
            if ($('.FriendshipListRef li:eq(' + i + ') input').attr('checked')) {
                if ($('.FriendshipListRef li:eq(' + i + ') .itemInfo span').html() == "aguardando") { return; }
                if ($('.FriendshipListRef li:eq(' + i + ') .lkAdicionar').html() == "adicionar") { return; }
                FriendshipInteractions.DeleteFriednship($('.FriendshipListRef li:eq(' + i + ') input').val(), $('.FriendshipListRef li:eq(' + i + ') input'));
            }
        });
    },

    /* excluir amigos  */
    DeleteFriednship: function (IdFriend, object) {

        try {
            $.getJSON('/Service/Friend/FriendshipService.svc/DeleteFriendship/' + VisitedUserId + '/' + IdFriend, DeleteFriednshipResult);
        } catch (e) {
            LogFailure('', document.location, e.Message)
        }

        function DeleteFriednshipResult(results) {
            deletaLI(object);
            if (parseInt($('.countFriendship').html()) > 0) {
                var num = parseInt($('.countFriendship').html()) - 1;
                $('.countFriendship').html(num);
                if (num == 0)
                    Friendship.HideRemove();
            }
            else {
                $('.countFriendship').html('');
            }

        }


    }
    ,
    /* excluir pedidos de amizades */
    DeleteFriendRequest: function (pIdFriend, object) {

        try {
            $.getJSON('/Service/Friend/FriendshipService.svc/DeleteFriendRequest/' + VisitedUserId + '/' + pIdFriend, DeleteFriendRequestResult);
        } catch (e) {
            LogFailure('', document.location, e.Message)
        }


        function DeleteFriendRequestResult(results) { deletaLI(object); }
        if ($('#lkRequest').html() != undefined) {
            var qtdRequest = parseInt($('#lkRequest').html().replace('solicitações', '').replace('(', '').replace(')', ''));
            if (qtdRequest == 0) {
                $('#lkRequest').html('solicitações');
                $('#lkRequest').removeClass('red');
            }
            else {
                $('#lkRequest').html('solicitações (' + (qtdRequest - 1) + ')');
            }
        }



    },
    AllDeleteFriendRequest: function () {
        $('.FriendshipListRef li').each(function (i, e) {
            if ($('.FriendshipListRef li:eq(' + i + ') input').attr('checked')) {
                var IdFriend = $('.FriendshipListRef li:eq(' + i + ') input').val();
                FriendshipInteractions.DeleteFriendRequest(IdFriend, $('.FriendshipListRef li:eq(' + i + ') input'));
            }
        });
    },

    /* excluir todos os amigos com checked */
    AllDeleteFriednship: function () {
        $('.FriendshipListRef li').each(function (i, e) {
            if ($('.FriendshipListRef li:eq(' + i + ') input').attr('checked')) {
                if ($('.FriendshipListRef li:eq(' + i + ') .itemInfo span').html() == "aguardando") { return; }
                if ($('.FriendshipListRef li:eq(' + i + ') .lkAdicionar').html() == "adicionar") { return; }
                FriendshipInteractions.DeleteFriednship($('.FriendshipListRef li:eq(' + i + ') input').val(), $('.FriendshipListRef li:eq(' + i + ') input'));
            }
        });
    },

    /* Adicionar pedido de amizade */
    isFriendRequest: function () {

        try {
            $.getJSON('/Service/Friend/FriendshipService.svc/isFriendRequest/' + VisitedUserId + '/' + LoggedUserId, isFriendRequestResult);
        } catch (e) {
            LogFailure('', document.location, e.Message);
        }

        function isFriendRequestResult(results) {
            if (!results.isFriend)
                if (!results.isFriendRequest) {
                    $('#btnRequestFriend').removeClass('hide');
                    $('#btnRequestFriend').click(function (e) {
                        e.preventDefault();
                        if (LoggedUserId == '0' || LoggedUserId == '-1') {
                            //showMessage('É necessário estar logado', false);
                            ModalBox.Show({ Title: 'É necessário estar logado', Width: 270, Error: true });
                        }
                        else {
                            FriendshipInteractions.AddFriendRequest(VisitedUserId);
                            $('#btnRequestFriend').addClass('hide');
                            $('#btnRequest').removeClass('hide');
                        }
                    });
                }
                else {
                    $('#btnRequest').removeClass('hide');
                }
        }


    },

    /* Adicionar pedido de amizade */
    AddFriendship: function (pIdFriend, object) {

        try {
            $.getJSON('/Service/Friend/FriendshipService.svc/SaveFriendship/' + VisitedUserId + '/' + pIdFriend, AddFriendshipResult);
        } catch (e) {
            LogFailure('', document.location, e.Message);
        }


        function AddFriendshipResult(results) {
            if (object != null)
                deletaLI(object);

            $('.countFriendship').html(parseInt($('.countFriendship').html()) + 1);
            if ($('#lkRequest').html() != undefined && $('#lkRequest').html().indexOf("(") != -1) {
                var qtdRequest = parseInt($('#lkRequest').html().replace('solicitações', '').replace('(', '').replace(')', '')) - 1;
                if (qtdRequest == 0) {
                    $('#lkRequest').html('solicitações');
                    $('#lkRequest').removeClass('red');
                }
                else {
                    $('#lkRequest').html('solicitações (' + qtdRequest + ')');
                }
            }
        }

    },

    AllAddFriendship: function () {
        $('.FriendshipListRef li').each(function (i, e) {
            if ($('.FriendshipListRef li:eq(' + i + ') input').attr('checked')) {
                var IdFriend = $('.FriendshipListRef li:eq(' + i + ') input').val();
                FriendshipInteractions.AddFriendship(IdFriend, $('.FriendshipListRef li:eq(' + i + ') input'));
            }
        });
    },
    /* adicionar pedido de amizade */
    AddFriendRequest: function (pIdFriend) {

        try {
            if (IsOwner == 'False') {
                $.getJSON('/Service/Friend/FriendshipService.svc/SaveFriendRequest/' + LoggedUserId + '/' + pIdFriend, AddFriendRequestResult);
            }
            else {
                $.getJSON('/Service/Friend/FriendshipService.svc/SaveFriendRequest/' + VisitedUserId + '/' + pIdFriend, AddFriendRequestResult);
            }
        } catch (e) {
            LogFailure('', document.location, e.Message);
        }


        function AddFriendRequestResult(results) {
            $('.FriendshipListRef li').each(function (i, e) {
                if ($('.FriendshipListRef li:eq(' + i + ') .lkAdicionar').attr('id') == pIdFriend) {
                    $('.FriendshipListRef li:eq(' + i + ') .lkAdicionar').replaceWith('<span>aguardando</span>');
                }
            });

            $('#suggestionsList li').each(function (i, e) {
                if ($('#suggestionsList li:eq(' + i + ') .lkAdicionar').attr('id') == pIdFriend) {
                    $('#suggestionsList li:eq(' + i + ') .lkAdicionar').replaceWith('<span>aguardando</span>');
                }
            });
        }

    },

    /* adicionar todos os pedidos de amizade com checked */
    AllAddFriendRequest: function () {
        $('.FriendshipListRef li').each(function (i, e) {
            if ($('.FriendshipListRef li:eq(' + i + ') input').attr('checked')) {
                if ($('.FriendshipListRef li:eq(' + i + ') .lkAdicionar').attr('id') != null) {
                    FriendshipInteractions.AddFriendRequest($('.FriendshipListRef li:eq(' + i + ') .lkAdicionar').attr('id'));
                }
            }
        });
    },
    NotifyFriend: function (msg) {

        if (msg.lenght == 0 || msg == '') {
            ModalBox.Show({ Title: 'campo mensagem obrigatório!', Width: 300, Error: true });
            return;
        }

        var Notification = {
            "LoggedUserId": "" + AccountSettings.LoggedUserId + "",
            "Message": "" + msg + ""
        }

        var pServerMethodAddress = '/Service/Friend/FriendshipService.svc/Notify/';

        $.ajax({
            type: "POST",
            url: pServerMethodAddress,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(Notification),
            dataType: "json",
            processData: false,
            success: function (data) { NotifyResult(data) }
        });

        function NotifyResult(results) {

            if (results.Error != null) {
                ModalBox.Show({ Title: results.Error.ErrorMessage, Width: 300, Error: true });
            }
            else {
                ModalBox.Show({ Title: "Mensagem enviada com sucesso", Width: 300 });
            }

            $.fancybox.close();
        }
    }

}

function NotifyFriend() {
    $.fancybox({
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'href': '#NotifyFriend'
    });

    $('#msg').empty();
}
