package com.remonsinnema.awe.worktype;

import org.json.simple.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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;


@RestController
@RequestMapping(path = "/work-types")
public class WorkTypesController {

  private static final String NL = System.getProperty("line.separator");

  private WorkTypesService service;

  @Autowired
  void setService(WorkTypesService service) {
    this.service = service;
  }

  @RequestMapping(method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
  public String getWorkTypes() {
    StringBuilder result = new StringBuilder(512);
    result.append("<table>").append(NL).append("<tr><td>").append(labelFor("type", "Type"))
        .append("</td><td><select id='type' onchange='typeChanged()'>");
    for (String type : service.getTypes()) {
      result.append("<option value='").append(type).append("'>").append(type).append("</option>");
    }
    result.append("</select></td></tr>").append(NL).append("<tr><td>").append(labelFor("category", "Category"))
        .append("</td><td><select id='category' onchange='categoryChanged()'></select></td></tr>").append(NL)
        .append("<tr><td>").append(labelFor("subcategory", "Sub-category"))
        .append("</td><td><select id='subcategory'></select></td></tr>").append(NL).append("</table>").append(NL);
    return result.toString();
  }

  private String labelFor(String id, String label) {
    String key = label.substring(0, 1);
    return String.format("<label for='%s' accesskey='%s'><u>%s</u>%s:</label>", id, key, key, label.substring(1));
  }

  @RequestMapping(path = "/{type}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public String getCategoriesFor(@PathVariable("type") String type) {
    return toJsonArrayAsString(service.getCategoriesFor(type));
  }

  @SuppressWarnings("unchecked")
  private String toJsonArrayAsString(Iterable<String> items) {
    JSONArray result = new JSONArray();
    items.forEach(item -> result.add(item));
    return result.toJSONString();
  }

  @RequestMapping(path = "/{type}/{category}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public String getSubcategoriesFor(@PathVariable("type") String type, @PathVariable("category") String category) {
    return toJsonArrayAsString(service.getSubcategoriesFor(type, category));
  }

}

@@ -586,16 +586,180 @@
 rator%22);
+%0A  private static final String START_ROW = %22%3Ctr%3E%3Ctd%3E%22;%0A  private static final String START_CELL = %22%3C/td%3E%3Ctd%3E%22;%0A  private static final String END_ROW = %22%3C/td%3E%3C/tr%3E%22;
 %0A%0A  priv
@@ -1092,34 +1092,33 @@
 (NL).append(
-%22%3Ctr%3E%3Ctd%3E%22
+START_ROW
 ).append(lab
@@ -1152,34 +1152,44 @@
     .append(
-%22%3C/td%3E%3Ctd%3E
+START_CELL).append(%22
 %3Cselect id='
@@ -1397,35 +1397,41 @@
 d(%22%3C/select%3E
-%3C/td%3E%3C/tr%3E%22
+%22).append(END_ROW
 ).append(NL)
@@ -1434,34 +1434,33 @@
 (NL).append(
-%22%3Ctr%3E%3Ctd%3E%22
+START_ROW
 ).append(lab
@@ -1502,34 +1502,44 @@
     .append(
-%22%3C/td%3E%3Ctd%3E
+START_CELL).append(%22
 %3Cselect id='
@@ -1582,34 +1582,24 @@
 )'%3E%3C/select%3E
-%3C/td%3E%3C/tr%3E
 %22).append(NL
@@ -1596,18 +1596,23 @@
 .append(
-NL
+END_ROW
 )%0A      
@@ -1621,26 +1621,36 @@
 .append(
-%22%3Ctr%3E%3Ctd%3E%22
+NL).append(START_ROW
 ).append
@@ -1707,26 +1707,36 @@
 .append(
-%22%3C/td%3E%3Ctd%3E
+START_CELL).append(%22
 %3Cselect 
@@ -1765,20 +1765,35 @@
 ect%3E
-%3C/td%3E%3C/tr%3E%22)
+%22).append(END_ROW)%0A        
 .app
@@ -2485,32 +2485,19 @@
 ach(
-item -%3E 
 result
-.
+::
 add
-(item)
 );%0A 

Fix code style issues