package lv.ctco.cukesrest.gadgets;

import lv.ctco.cukesrest.common.InMemoryStorage;
import lv.ctco.cukesrest.gadgets.dto.GadgetDto;
import lv.ctco.cukesrest.gadgets.dto.GadgetType;

import javax.inject.Inject;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Set;

public class DummyGadgetService {

    @Inject
    InMemoryStorage storage;

    public Collection<GadgetDto> searchGadgets() {
        return storage.getGadgets().values();
    }

    public GadgetDto getGadget(Integer id) {
        return storage.getGadgets().get(id);
    }

    public Integer addGadget(GadgetDto gadget) {
        if (gadget == null || gadget.getType() == GadgetType.BOOK_READER) {
            return null;
        }

        Set<Integer> gadgetIds = storage.getGadgets().keySet();
        Integer newId = Collections.max(gadgetIds) + 1;
        gadget.setId(newId);
        gadget.setUpdatedDate(null);
        gadget.setCreatedDate(new Date());

        storage.getGadgets().put(newId, gadget);
        return newId;
    }

    public boolean updateGadget(Integer id, GadgetDto updated) {
        GadgetDto gadget = storage.getGadgets().get(id);
        if (gadget == null) {
            return false;
        }
        gadget.setName(updated.getName());
        gadget.setType(updated.getType());
        gadget.setOwner(updated.getOwner());
        gadget.setUpdatedDate(new Date());
        return true;
    }

    public boolean removeGadget(Integer id) {
        return storage.getGadgets().remove(id) == null; //TODO check
    }
}

@@ -225,93 +225,9 @@
 til.
-Collection;%0Aimport java.util.Collections;%0Aimport java.util.Date;%0Aimport java.util.Set
+*
 ;%0A%0Ap
@@ -1420,68 +1420,247 @@
     
-return storage.getGadgets().remove(id) == null; //TODO check
+Map%3CInteger, GadgetDto%3E gadgets = storage.getGadgets();%0A        GadgetDto gadget = gadgets.get(id);%0A        if (gadget != null) %7B%0A            gadgets.remove(id);%0A            return true;%0A        %7D else %7B%0A            return false;%0A        %7D
 %0A   

DELETE /gadget added