/*
 * Copyright (C) 2013 The enviroCar project
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.envirocar.server.rest.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response.Status;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import org.envirocar.server.core.entities.User;
import org.envirocar.server.core.entities.Users;

import org.envirocar.server.core.exception.UserNotFoundException;
import org.envirocar.server.rest.MediaTypes;
import org.envirocar.server.rest.Schemas;
import org.envirocar.server.rest.UserReference;
import org.envirocar.server.rest.auth.Authenticated;

import org.envirocar.server.rest.validation.Schema;

/**
 * TODO JavaDoc
 *
 * @author Christian Autermann <autermann@uni-muenster.de>
 */
public class FriendsResource extends AbstractResource {
    public static final String FRIEND = "{friend}";
    private final User user;

    @Inject
    public FriendsResource(@Assisted User user) {
        this.user = user;
    }

    @GET
    @Schema(response = Schemas.USERS)
    @Produces({ MediaTypes.USERS,
                MediaTypes.XML_RDF,
                MediaTypes.TURTLE,
                MediaTypes.TURTLE_ALT })
    public Users get() {
        checkRights(getRights().canSeeFriendsOf(user));
        return getFriendService().getFriends(user);
    }

    @POST
    @Authenticated
    @Schema(request = Schemas.USER_REF)
    @Consumes({ MediaTypes.USER_REF })
    public void add(UserReference friend) throws UserNotFoundException {
        if (friend.getName() == null ||
            friend.getName().equals(getCurrentUser().getName())) {
            throw new WebApplicationException(Status.BAD_REQUEST);
        }
        User f = getUserService().getUser(friend.getName());
        checkRights(getRights().canFriend(user, f));
        getFriendService().addFriend(user, f);
    }

    @Path(FRIEND)
    public UserResource friend(@PathParam("friend") String friendName) throws
            UserNotFoundException {
        checkRights(getRights().canSeeFriendsOf(user));
        return getResourceFactory()
                .createFriendResource(user, getFriendService()
                .getFriend(user, friendName));
    }
}

@@ -1189,17 +1189,16 @@
 .Users;%0A
-%0A
 import o
@@ -1439,25 +1439,24 @@
 henticated;%0A
-%0A
 import org.e
@@ -1690,16 +1690,169 @@
 iend%7D%22;%0A
+    public static final String INCOMING_FRIEND_REQUESTS = %22incomingRequests%22;%0A%09public static final String OUTGOING_FRIEND_REQUESTS = %22outgoingRequests%22;%0A
     priv
@@ -3121,16 +3121,16 @@
 rvice()%0A
-
         
@@ -3174,10 +3174,698 @@
 ;%0A    %7D%0A
+    %0A    @GET%0A    @Path(INCOMING_FRIEND_REQUESTS)%0A    @Produces(%7B MediaTypes.USERS,%0A        MediaTypes.XML_RDF,%0A        MediaTypes.TURTLE,%0A        MediaTypes.TURTLE_ALT %7D)%0A    public Users pendingIncomingFriendRequests() %7B%0A    %09checkRights(getRights().canSeeFriendsOf(user));%0A    %09return getFriendService().pendingIncomingRequests(user);%0A    %7D%0A    %0A    @GET%0A    @Path(OUTGOING_FRIEND_REQUESTS)%0A    @Produces(%7B MediaTypes.USERS,%0A        MediaTypes.XML_RDF,%0A        MediaTypes.TURTLE,%0A        MediaTypes.TURTLE_ALT %7D)%0A    public Users pendingOutgoingFriendRequests() %7B%0A    %09checkRights(getRights().canSeeFriendsOf(user));%0A    %09return getFriendService().pendingOutgoingRequests(user);%0A    %7D%0A
 %7D%0A

set up paths in FriendsResource to get list of pending friend requests