package eu.dnetlib.enabling.ui.server;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashSet;

import javax.imageio.ImageIO;
import javax.servlet.ServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class MetadataFormatsController {

	private static final Log log = LogFactory.getLog(MetadataFormatsController.class); // NOPMD by marko on 11/24/08 5:02 PM

	
	@RequestMapping(value = "/userinterface/mdformats_img.do")
	public void getImage(final ServletResponse response, @RequestParam(value = "status", required = true) final String status, final OutputStream out) throws IOException {
		response.setContentType("image/png");

		log.info(status);
		
		BufferedImage imgOut = null;
		for (String s : new HashSet<String>(Arrays.asList(status.split("\\.")))) {
			if (s.length() > 1) {
				imgOut = addImage(imgOut, generateSingleImage(s.substring(1), s.substring(0, 1)));
			}
		}	

		if (imgOut != null) { 
			log.info("Generating image");
			ImageIO.write(imgOut, "png", out);
		}
		out.flush();
		out.close();
	} 
	
	private BufferedImage addImage(BufferedImage img1, BufferedImage img2) {
		if (img1 == null) return img2;
		if (img2 == null) return img1;
	
		final int width = img1.getWidth() + img2.getWidth();
		final int height = img1.getHeight(); 
		
		// create a new image
        final BufferedImage newImg = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        final Graphics2D imageG = newImg.createGraphics();
        
        imageG.drawImage(img1, null, 0,               0);
        imageG.drawImage(img2, null, img1.getWidth(), 0);
	
        imageG.dispose();
        
        return newImg;
	}

	private BufferedImage generateSingleImage(final String text, final String status) throws IOException {
		
		String rs = "/eu/dnetlib/enabling/ui/server/mdFormatImages/";
		if      (status.equals("0")) { rs += "formatNull.png"; }
		else if (status.equals("1")) { rs += "formatOK.png"; }
		else                         { rs += "formatFail.png"; }
	
		InputStream in = getClass().getResourceAsStream(rs);
		BufferedImage image = ImageIO.read(in);
		
		// create a new image
        final BufferedImage waterMarked = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        final Graphics2D imageG = waterMarked.createGraphics();
        // draw original image.
        imageG.drawImage(image, null, 0, 0);
        imageG.dispose();

        final Graphics2D g  = waterMarked.createGraphics();
        g.setColor(Color.RED);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setFont(new Font("Arial", Font.BOLD, 9));
        final FontMetrics fontMetrics = g.getFontMetrics();
        final Rectangle2D rect = fontMetrics.getStringBounds(text, g);
        final int centerX = (image.getWidth() - (int) rect.getWidth()) /2;
        
        g.drawString(text, centerX, image.getHeight() - 2);
        g.dispose();

        log.info("Metadata " + text + ",status " + status);
        return waterMarked;
    }
}
