package ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.service;

import com.microsoft.azure.storage.StorageException;
import ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.data.Repository;
import ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.data.RepositoryFactory;
import ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.model.ObjectEntity;
import ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.proxy.ProxyFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Path("/houses/{house_id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ObjectService {

    public Repository getRepository() throws StorageException {
        return RepositoryFactory.create();
    }

    public ProxyFactory getProxyFactory() {
        return ProxyFactory.getProxyFactory();
    }

    @GET
    @Path("objects")
    public List<ObjectDo> stub(@PathParam("house_id") String houseId) throws StorageException {

        if (!houseExists(houseId))
            throw new NotFoundException();
        List<ObjectDo> result = new ArrayList<>();
        for (ObjectEntity obj : getRepository().getAll()) {
            result.add(obj.toDo());
        }
        return result;
    }

    @POST
    @Path("objects")
    public ObjectDo addObject(@PathParam("house_id") String houseId, ObjectDo obj) throws StorageException {
        if (!houseExists(houseId) || obj.getType() == null)
            throw new NotFoundException();
        obj.setId(UUID.randomUUID().toString());
        try {
            getRepository().insertOrUpdate(ObjectEntity.fromDo(obj));
        } catch (StorageException e) {
            throw new InternalServerErrorException();
        }
        return obj;
    }

    private boolean houseExists(String id) {
        return getProxyFactory()
                .getHouseRegistryService()
                .get(id);
    }
}

@@ -645,19 +645,27 @@
 ouse_id%7D
+/objects
 %22)%0A
-
 @Consume
@@ -983,37 +983,16 @@
    @GET%0A
-    @Path(%22objects%22)%0A
     publ
@@ -1359,29 +1359,8 @@
 OST%0A
-    @Path(%22objects%22)%0A
     
@@ -1795,32 +1795,32 @@
 on();%0A        %7D%0A
-
         return o
@@ -1826,24 +1826,427 @@
 obj;%0A    %7D%0A%0A
+    @GET%0A    @Path(%22%7Bobject_id%7D%22)%0A    public ObjectDo getObjectById(@PathParam(%22house_id%22) String houseId,%0A                                  @PathParam(%22object_id%22) String objectId) throws StorageException %7B%0A        ObjectEntity entity = getRepository().find(objectId);%0A        if (!houseExists(houseId) %7C%7C entity == null)%0A            throw new NotFoundException();%0A        return entity.toDo();%0A    %7D%0A%0A
     private 

Add get object by id impl.