package eu.dnetlib.is.resources;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import eu.dnetlib.data.is.resource.model.ResourceType;
import eu.dnetlib.data.is.resource.repository.ResourceTypeRepository;

@RestController
@RequestMapping("/api/resources")
public class ResourceApiController extends AbstractResourceController {

	@Autowired
	private ResourceTypeRepository resourceTypeRepository;

	@GetMapping("/")
	private List<SimpleResourceType> types() {
		return StreamSupport.stream(resourceTypeRepository.findAll().spliterator(), false)
			.filter(rt -> rt.isSimple())
			.map(rt -> new SimpleResourceType(rt))
			.collect(Collectors.toList());
	}

	class SimpleResourceType {

		private final String typeId;

		private final String typeName;

		private final String contentType;

		private final long count;

		public SimpleResourceType(final ResourceType rt) {
			this.typeId = rt.getId();
			this.typeName = rt.getName();
			this.contentType = rt.getContentType();
			this.count = rt.getCount();
		}

		public String getTypeId() {
			return typeId;
		}

		public String getTypeName() {
			return typeName;
		}

		public String getContentType() {
			return contentType;
		}

		public long getCount() {
			return count;
		}

	}

}
