package email.service.template.rs;

import com.codahale.metrics.annotation.Timed;
import email.service.email.EmailService;
import email.service.template.api.Template;
import email.service.template.db.TemplateDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import org.antlr.stringtemplate.*;

@Path("/templates")
@Produces(MediaType.APPLICATION_JSON)
@Api("Email Templates")
@Singleton
public class TemplateResource {

  private final TemplateDao dao;
  private final EmailService emailService;

  @Inject
  public TemplateResource(TemplateDao dao, EmailService emailService) {
    this.dao = dao;
    this.emailService = emailService;
  }

  @Timed
  @GET
  @ApiOperation("Get All Templates")
  public List<Template> findAll(
          @DefaultValue("EN") @QueryParam("language") String language) {
    return dao.findAll(language);
  }

  @Timed
  @GET
  @ApiOperation("Get Template by ID")
  @Path("{id}")
  public Template find(
          @PathParam("id") Long id,
          @DefaultValue("EN") @QueryParam("language") String language) {
    return dao.find(id, language);
  }

  @Timed
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @ApiOperation("Create Template")
  public Template create(
          @Valid @ApiParam(required = true) Template template,
          @DefaultValue("EN") @QueryParam("language") String language) {
    final Long id = dao.create(template);
    template.setId(id);
    dao.updateTranslation(template, language);
    return dao.find(id, language);
  }

  @Timed
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @ApiOperation("Modify Template")
  @Path("{id}")
  public Template update(
          @PathParam("id") Long id,
          @Valid @ApiParam(required = true) Template template,
          @DefaultValue("EN") @QueryParam("language") String language) {
    dao.update(id, template);
    dao.updateTranslation(template, language);
    return dao.find(id, language);
  }

  @Timed
  @DELETE
  @ApiOperation("Delete Template")
  @Path("{id}")
  public Response update(
          @PathParam("id") Long id) {
    dao.deleteTranslation(id);
    dao.delete(id);
    return Response.status(Response.Status.NO_CONTENT).build();
  }

  @Timed
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @ApiOperation("Test Template")
  @Path("{id}/test")
  public Response test(
          @PathParam("id") Long id,
          @DefaultValue("EN") @QueryParam("language") String language) {
    Template template = dao.find(id, language);
    //StringTemplate hello = new StringTemplate("Hello, $name$");
    //hello.setAttribute("name", "World");
    emailService.sendEmail("gert.vesterberg@gmail.com", template.getTitle(), template.getBodyText(), template.getBodyHtml());
    return Response.status(Response.Status.OK).build();
  }
}
@@ -324,16 +324,64 @@
 piParam;
+%0Aimport org.antlr.stringtemplate.StringTemplate;
 %0A%0Aimport
@@ -572,20 +572,23 @@
 va.util.
-List
+HashMap
 ;%0Aimport
@@ -592,34 +592,44 @@
 ort 
-org.antlr.stringtemplate.*
+java.util.List;%0Aimport java.util.Map
 ;%0A%0A@
@@ -2739,24 +2739,64 @@
 language
+,%0A          HashMap%3CString, String%3E tags
 ) %7B%0A    
 Template
@@ -2787,16 +2787,22 @@
 ) %7B%0A    
+final 
 Template
@@ -2841,18 +2841,16 @@
 e);%0A    
-//
 StringTe
@@ -2860,95 +2860,316 @@
 ate 
-hello = new StringTemplate(%22Hello, $name$%22);%0A    //hello.setAttribute(%22name%22, %22World%22);
+text = new StringTemplate(template.getBodyText());%0A    StringTemplate html = new StringTemplate(template.getBodyHtml());%0A%0A    for (Map.Entry%3CString, String%3E entry : tags.entrySet()) %7B%0A      text.setAttribute(entry.getKey(), entry.getValue());%0A      html.setAttribute(entry.getKey(), entry.getValue());%0A    %7D%0A
 %0A   
@@ -3248,50 +3248,36 @@
 , te
-mplate.getBodyText(), template.getBodyHtml
+xt.toString(), html.toString
 ());

Add ability to send tags to test email for further replacement