package org.marsik.elshelves.backend.entities.converters;

import gnu.trove.set.hash.THashSet;
import org.marsik.elshelves.api.entities.NumericPropertyApiModel;
import org.marsik.elshelves.api.entities.PartGroupApiModel;
import org.marsik.elshelves.backend.entities.Group;
import org.marsik.elshelves.backend.entities.NumericProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

@Service
public class EmberToGroup implements CachingConverter<PartGroupApiModel, Group, UUID> {
	@Autowired
	EmberToNamedObject emberToNamedObject;

    @Autowired
    EmberToNumericProperty emberToNumericProperty;

	@Override
	public Group convert(PartGroupApiModel object, int nested, Map<UUID, Object> cache) {
		if (object == null) {
			return null;
		}

		if (cache.containsKey(object.getId())) {
			return (Group)cache.get(object.getId());
		}

		Group model = new Group();
		if (nested > 0
				&& object.getId() != null) {
			cache.put(object.getId(), model);
		}

		return convert(object, model, nested, cache);
	}

	@Override
	public Group convert(PartGroupApiModel object, Group model, int nested, Map<UUID, Object> cache) {
		emberToNamedObject.convert(object, model, nested, cache);
		model.setParent(convert(object.getParent(), 1, cache));

        if (object.getShowProperties() != null) {
            model.setShowProperties(new THashSet<NumericProperty>());
            for (NumericPropertyApiModel p: object.getShowProperties()) {
                model.getShowProperties().add(emberToNumericProperty.convert(p, 1, cache));
            }
        }

		return model;
	}
}

@@ -206,32 +206,91 @@
 tGroupApiModel;%0A
+import org.marsik.elshelves.api.entities.PartTypeApiModel;%0A
 import org.marsi
@@ -387,16 +387,67 @@
 operty;%0A
+import org.marsik.elshelves.backend.entities.Type;%0A
 import o
@@ -812,16 +812,61 @@
 perty;%0A%0A
+    @Autowired%0A    EmberToType emberToType;%0A%0A
 %09@Overri
@@ -1806,16 +1806,267 @@
      %7D%0A%0A
+        if (object.getTypes() != null) %7B%0A            model.setTypes(new THashSet%3CType%3E());%0A            for (PartTypeApiModel t: object.getTypes()) %7B%0A                model.getTypes().add(emberToType.convert(t, nested, cache));%0A            %7D%0A        %7D%0A%0A
 %09%09return

Save types when updating part group