/*
 * ApiController.java
 *
 * Copyright (C) 2017 [ A Legge Up ]
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

package com.aleggeup.confagrid.controller;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

import com.aleggeup.confagrid.filter.JwtFilter;

import io.jsonwebtoken.Claims;

@RestController
@RequestMapping("/api")
public class ApiController {

    @RequestMapping(value = "role/{role}", method = RequestMethod.GET)
    public Boolean login(@PathVariable final String role,
            final HttpServletRequest request) throws ServletException {

        final Claims claims = (Claims) request.getAttribute(JwtFilter.ATTRIBUTE_CLAIMS);

        if (claims == null || claims.isEmpty()) {
            return false;
        }

        return getRoles(claims).stream().anyMatch(map -> map.containsValue(role));
    }

    @SuppressWarnings("unchecked")
    private List<LinkedHashMap<String, String>> getRoles(final Claims claims) {
        final Object roles = claims.get("roles");
        if (roles != null) {
            return (List<LinkedHashMap<String, String>>)roles;
        } else {
            return Collections.EMPTY_LIST;
        }
    }

    @RequestMapping(value = "test", method = RequestMethod.GET, produces = "image/svg+xml")
    public @ResponseBody String test() throws SVGGraphics2DIOException, UnsupportedEncodingException {

        final DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
        final String svgNS = "http://www.w3.org/2000/svg";
        final Document document = domImpl.createDocument(svgNS, "svg", null);

        final SVGGeneratorContext context = SVGGeneratorContext.createDefault(document);
        context.setEmbeddedFontsOn(true);
        context.setComment("Generated by Confagrid [ A Legge Up ] with Batik SVG Generator");

        // Create an instance of the SVG Generator.
        final boolean textAsShapes = true;
        final SVGGraphics2D svgGenerator = new SVGGraphics2D(context, textAsShapes);

        // Ask the test to render into the SVG Graphics2D implementation.
        paint(svgGenerator);

        // Finally, stream out SVG to the standard output using
        // UTF-8 encoding.
        final boolean useCSS = true; // we want to use CSS style attributes
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final Writer out = new OutputStreamWriter(baos, "UTF-8");
        svgGenerator.stream(out, useCSS);

        return baos.toString("UTF-8");
    }

    public void paint(final Graphics2D g2d) {
        g2d.setPaint(new Color(0, 0, 0, 0.1f));
        g2d.fill(new Rectangle(0, 0, 1000, 1000));
        g2d.setStroke(new BasicStroke());
        g2d.setPaint(new Color(0, 0, 0, 1.0f));
        g2d.drawRect(10, 10, 980, 980);
        g2d.drawLine(100, 10, 100, 990);
      }
}

@@ -3659,16 +3659,14 @@
  0, 
-10
+8
 00, 
-10
+6
 00))
@@ -3705,16 +3705,20 @@
 cStroke(
+4.0f
 ));%0A    
@@ -3757,24 +3757,25 @@
  0, 1.0f));%0A
+%0A
         g2d.
@@ -3787,23 +3787,21 @@
 ect(
-1
 0, 
-1
 0, 
-9
 8
+0
 0, 
-98
+60
 0);%0A
@@ -3812,40 +3812,206 @@
     
-g2d.drawLine(100, 10, 100, 990);
+for (int i = 0; i %3C 16 + 1; ++i) %7B%0A            g2d.drawLine(i * 50, 0, i * 50, 600);%0A        %7D%0A        for (int i = 0; i %3C 12 + 1; ++i) %7B%0A            g2d.drawLine(0, i * 50, 800, i * 50);%0A        %7D%0A
 %0A   

Test drawing a full grid