/**
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * Copyright (C)
 * <a href="http://www.inab.org">Spanish National Institute of Bioinformatics (INB)</a>
 * <a href="http://www.bsc.es">Barcelona Supercomputing Center (BSC)</a>
 * <a href="http://inb.bsc.es">Computational Node 6</a>
 */

package org.inb.biomoby.client.gui.ontology;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import org.inb.biomoby.client.gui.DynamicMobyComponent;
import org.inb.biomoby.shared.datatypes.MobyInteger;
import org.inb.biomoby.shared.ontology.AnnotationHits;

/**
 * @author Dmitry Repchevsky
 */

public class AnnotationHitsComponent extends DynamicMobyComponent
{
    private AnnotationHits hits;
    
    private BufferedImage image;
    
    private int width;
    private int height;

    public AnnotationHitsComponent()
    {
    }
     
    public AnnotationHitsComponent(AnnotationHits hits)
    {
         this.hits = hits;
    }
    
    public void setAnnotationHits(AnnotationHits hits)
    {
        this.hits = hits;
        
        repaint();
    }

    public AnnotationHits getData()
    {
        return hits;
    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(120, 12);
    }
    
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        if (hits != null)
        {
            final int w = getWidth();
            final int h = getHeight();
            
            if (image == null || width != w || height != h)
            {
                image = generateImage(hits, w, h);
            }
            
            g.drawImage(image, 0, 0, null);
        }
    }
    
    private static BufferedImage generateImage(AnnotationHits hits, int width, int height)
    {
        StringBuilder sb = new StringBuilder();
        
        MobyInteger hitcount = hits.getHitcount();
        MobyInteger from = hits.getFrom();
        MobyInteger to = hits.getTo();
        
        sb.append("hit counts: ");
        sb.append(hitcount != null ? hitcount.getInteger() : '?');
        sb.append(" from ");
        sb.append(from != null ? from.getInteger() : '?');
        sb.append(" to ");
        sb.append(to != null ? to.getInteger() : '?');
        
        String label = sb.toString();
        
        FontRenderContext context = new FontRenderContext(null, true, false);
        
        Font font;
        Rectangle2D bounds;
        int fSize = 36;
        
        do
        {
            font = new Font(Font.SANS_SERIF, Font.PLAIN, fSize--);
            bounds = font.getStringBounds(label, context);
        }
        while (bounds.getWidth() > width || bounds.getHeight() > height);
        
        GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = environment.getDefaultScreenDevice();
        GraphicsConfiguration config = device.getDefaultConfiguration();
        BufferedImage image = config.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
         
        Graphics2D g2d = image.createGraphics();
        
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

        g2d.setFont(font);
        g2d.setColor(Color.BLUE);
        g2d.drawString(label, 0, height);
        
        g2d.dispose();
        
        return image;
    }
}
