package ua.be.wee.controller.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import ua.be.wee.model.nodes.Node;
import ua.be.wee.model.repository.FusekiWrapper;
import ua.be.wee.model.repository.NodeRespository;

@RestController
public class NodeController {

	@Autowired
	private NodeRespository nodesRepo;
	
	@Autowired
	public NodeController(@Value("${endpoint}")final String endpoint) throws Exception {
		if (FusekiWrapper.getInstance().getServiceURI() == null) {
			FusekiWrapper.getInstance().setServiceURI(endpoint);
		}
	} 
	
	@CrossOrigin
	@GetMapping("/nodes/{pmIRI}")
	public List<Node> all(@PathVariable String pmIRI) throws Exception {
		return nodesRepo.getNodes(pmIRI);
	}

	@CrossOrigin
	@GetMapping("/nodes/{pmIRI}/{nodeIRI}")
	public Node getNode(@PathVariable String pmIRI, @PathVariable String nodeIRI) throws Exception {
		List<Node> nodes = nodesRepo.getNodes(pmIRI);
		for (Node node : nodes) {
			if (node.getIri().equals(nodeIRI)) {
				return node;
			}
		}
		return null;
	}
	
}
