package org.dainst.gazetteer.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
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;

@Controller
public class ImageController {

	private Font fontBig = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
	private Font fontMedium = new Font(Font.SANS_SERIF, Font.PLAIN, 11);
	private Font fontSmall = new Font(Font.SANS_SERIF, Font.PLAIN, 8);
	private ClassLoader classloader = Thread.currentThread().getContextClassLoader();
	
	@ResponseBody
	@RequestMapping(value = "/markerImage/{color}/{number}", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
	public byte[] numberedMarkerImage(@PathVariable String color, @PathVariable int number) throws Exception {
		String fileName;
		switch (color) {
		case "blue":
			fileName = "marker_blue.png";
			break;
		case "lightRed":
			fileName = "marker_light_red.png";
			break;
		default:
			fileName = "marker_red.png";
			break;
		}

		InputStream inputStream = classloader.getResourceAsStream("images/" + fileName);
		BufferedImage image = null;
		image = ImageIO.read(inputStream);
		
		if (image != null) {			
			if (number < 1000) {
				int xPos, yPos;
				Font font;
				if (number < 10) {
					xPos = 12;
					yPos = 14;
					font = fontBig;
				} else if (number < 100) {
					xPos = 9;
					yPos = 13;
					font = fontMedium;
				} else {
					xPos = 8;
					yPos = 13;
					font = fontSmall;
				}
			
				Graphics graphics = image.getGraphics();
				graphics.setFont(font);
				graphics.setColor(Color.BLACK);
				graphics.drawString(String.valueOf(number), xPos, yPos);
				graphics.dispose();
			}
			
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();			
			ImageIO.write(image, "png", outputStream);			
			return outputStream.toByteArray();
		}
		return null;
	}

}
@@ -104,16 +104,50 @@
 Graphics
+2D;%0Aimport java.awt.RenderingHints
 ;%0Aimport
@@ -1675,16 +1675,99 @@
 ber 
+== 4) %7B%0A%09%09%09%09%09xPos = 11;%0A%09%09%09%09%09yPos = 14;%0A%09%09%09%09%09font = fontBig;%0A%09%09%09%09%7D else if (number 
 %3C 10) %7B%0A
 %09%09%09%09
@@ -1758,24 +1758,24 @@
 ber %3C 10) %7B%0A
-
 %09%09%09%09%09xPos = 
@@ -1990,16 +1990,18 @@
 Graphics
+2D
  graphic
@@ -2003,16 +2003,29 @@
 aphics =
+ (Graphics2D)
  image.g
@@ -2038,16 +2038,126 @@
 hics();%0A
+%09%09%09%09graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );%0A
 %09%09%09%09grap

use antialiased font for numbered markers