package net.savantly.sprout.module.content;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
import net.savantly.sprout.module.content.model.contentItem.ContentItem;
import net.savantly.sprout.module.content.model.contentItem.ContentItemRenderingChain;
import net.savantly.sprout.module.content.model.fieldType.FieldType;
import net.savantly.sprout.module.content.model.webPage.WebPage;
import net.savantly.sprout.module.content.model.webPage.WebPageRenderer;
import net.savantly.sprout.module.content.model.webPage.WebPageRepository;

@RestController
@RequestMapping("/api/content")
@Transactional
public class ContentApi {

	private final ObjectMapper mapper;
	private final ContentItemRenderingChain itemRenderer;
	private WebPageRenderer pageRenderer;
	private WebPageRepository pageRepository;
	
	
	public ContentApi(ContentItemRenderingChain itemRenderer, WebPageRenderer pageRenderer, WebPageRepository repository) {
		this.mapper = new ObjectMapper();
		this.mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
		this.itemRenderer = itemRenderer;
		this.pageRepository = repository;
		this.pageRenderer = pageRenderer;
	}
	
	@RequestMapping({"/fieldTypes"})
	public String fieldTypes() throws JsonProcessingException {
		List<JsonNode> fieldTypes = new ArrayList<>();
		Arrays.stream(FieldType.values()).forEach((ft) -> {
			fieldTypes.add(ft.toJsonNode());
		});
		return mapper.writeValueAsString(fieldTypes);
	}
	
	@RequestMapping(value="/item/{id}", method=RequestMethod.GET)
	public ResponseEntity<String> getContent(@PathVariable("id") ContentItem item) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		StringWriter writer = new StringWriter();
		itemRenderer.renderContentItem(item, writer);
		ResponseEntity<String> response = new ResponseEntity<String>(writer.toString(), HttpStatus.OK);
		return response;
	}
	
	@RequestMapping(value="/page/{id}", method=RequestMethod.GET)
	public ResponseEntity<String> getPage(@PathVariable("id") WebPage item) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Assert.notNull(item, "WebPage was not found");
		String renderedView = pageRenderer.render(item);
		ResponseEntity<String> response = new ResponseEntity<String>(renderedView, HttpStatus.OK);
		return response;
	}
	
	@RequestMapping(value="/home", method=RequestMethod.GET)
	public ResponseEntity<String> getHomePage() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		WebPage item = pageRepository.findHomePage();
		if(item == null) {
			return new ResponseEntity<String>("No Home page", HttpStatus.NOT_FOUND);
		} else {
			String renderedView = pageRenderer.render(item);
			ResponseEntity<String> response = new ResponseEntity<String>(renderedView, HttpStatus.OK);
			return response;
		}
	}

}

@@ -168,24 +168,74 @@
 util.List;%0A%0A
+import javax.persistence.EntityNotFoundException;%0A
 import javax
@@ -1223,16 +1223,99 @@
 gChain;%0A
+import net.savantly.sprout.module.content.model.contentItem.ContentItemRepository;%0A
 import n
@@ -1771,32 +1771,38 @@
 derer;%0A%09private 
+final 
 WebPageRenderer 
@@ -1824,16 +1824,22 @@
 private 
+final 
 WebPageR
@@ -1864,16 +1864,76 @@
 sitory;%0A
+%09private final ContentItemRepository contentItemRepository;%0A
 %09%0A%09%0A%09pub
@@ -2045,16 +2045,61 @@
 pository
+, ContentItemRepository contentItemRepository
 ) %7B%0A%09%09th
@@ -2310,16 +2310,70 @@
 nderer;%0A
+%09%09this.contentItemRepository = contentItemRepository;%0A
 %09%7D%0A%09%0A%09@R
@@ -2791,24 +2791,17 @@
 d%22) 
-ContentItem item
+String id
 ) th
@@ -2957,16 +2957,137 @@
 iter();%0A
+%09%09ContentItem item = this.contentItemRepository.findById(id).orElseThrow(()-%3E new EntityNotFoundException(%22id: %22 + id));%0A
 %09%09itemRe
@@ -3370,28 +3370,25 @@
 e(%22id%22) 
-WebPage item
+String id
 ) throws
@@ -3502,52 +3502,112 @@
 %7B%0A%09%09
-Assert.notNull(item, %22WebPage was not found%22
+WebPage item = this.pageRepository.findById(id).orElseThrow(()-%3Enew EntityNotFoundException(%22id: %22 + id)
 );%0A%09

fix entity resolver