/**
 * 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.auxiliary;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.List;
import org.inb.biomoby.client.gui.auxiliary.AbstractAnnotationListPane.AnnotationListListener;
import org.inb.biomoby.shared.datatypes.MobyInteger;
import org.inb.biomoby.shared.datatypes.MobyString;
import org.inb.biomoby.shared.ontology.BLOCKAnnotation;
import org.inb.biomoby.shared.ontology.FamilyBLOCKAnnotation;

/**
 * @author Dmitry Repchevsky
 */

public class FamilyBLOCKAnnotationPane<T extends BLOCKAnnotation> extends LabeledAnnotationListPane<T, AnnotationListListener>
{
    private FamilyBLOCKAnnotation annotations;
    
    private final static Color DESCRIPTION_BG = 
            new Color(BACKGROUND.getRed(), BACKGROUND.getGreen(), BACKGROUND.getBlue(), 0xF0);
    
    private boolean highlighted;
    
    private int height;
    private Font font;

    public FamilyBLOCKAnnotationPane()
    {
        super(true);
    }

    public FamilyBLOCKAnnotationPane(FamilyBLOCKAnnotation annotations)
    {
        super((List<T>)annotations.getBlocks(), true);
        
        this.annotations = annotations;
    }

    @Override
    public String getLabel(T annotation)
    {
        StringBuilder sb = new StringBuilder();
                
        MobyString frameMobyString = annotation.getFrame();

        if (frameMobyString != null)
        {
            String frame = frameMobyString.getString();
            
            if (frame != null && frame.length() > 0)
            {
                sb.append("frame: ").append(frame);
            }
        }

        MobyString acMobyString = annotation.getAC();
        if (acMobyString != null)
        {
            String ac = acMobyString.getString();
            
            if (ac != null && ac.length() > 0)
            {
                if (sb.length() > 0)
                {
                    sb.append(", ");
                }
                sb.append("ac: ").append(ac);
            }
        }
        
        MobyString evalueMobyString = annotation.getBLOCK_Evalue();
        if (evalueMobyString != null)
        {
            String evalue = evalueMobyString.getString();
            
            if (evalue != null && evalue.length() > 0)
            {
                if (sb.length() > 0)
                {
                    sb.append(", ");
                }
                sb.append("evalue: ").append(evalue);
            }
        }
        
        return sb.length() == 0 ? null : sb.toString();
    }
    
    @Override
    public void paintComponent(Graphics g)
    {
        if (highlighted)
        { // paint a description over annotations
            super.paintComponent(g);
            paintDescription(g);
        }
        else
        {
            paintDescription(g);
            super.paintComponent(g);
        }
    }
    
    @Override public void mouseEntered(MouseEvent e)
    {
        highlighted = true;
        
        super.mouseEntered(e);
        
        repaint();
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
        highlighted = false;
        
        super.mouseExited(e);
        
        repaint();
    }
    
    private void paintDescription(Graphics g)
    {
        StringBuilder sb = new StringBuilder();
        
        MobyString acMobyString = annotations.getAC();

        if (acMobyString != null)
        {
            String ac = acMobyString.getString();
            
            if (ac != null && ac.length() > 0)
            {
                sb.append(ac);
            }
        }

        MobyString descriptionMobyString = annotations.getDescription();
        
        if (descriptionMobyString != null)
        {
            String description = descriptionMobyString.getString();
            
            if (description != null && description.length() > 0)
            {
                if (sb.length() > 0)
                {
                    sb.append(", ");
                }
                
                sb.append(description);
            }
        }
        
        MobyString strandMobyString = annotations.getStrand();
        
        if (strandMobyString != null)
        {
            String strand = strandMobyString.getString();
            
            if (strand != null && strand.length() > 0)
            {
                if (sb.length() > 0)
                {
                    sb.append(", strand: ");
                }
                
                sb.append(strand);
            }
        }

        MobyString evalueMobyString = annotations.getCombined_Evalue();
        
        if (evalueMobyString != null)
        {
            String evalue = evalueMobyString.getString();
            
            if (evalue != null && evalue.length() > 0)
            {
                if (sb.length() > 0)
                {
                    sb.append(", e-value: ");
                }
                
                sb.append(evalue);
            }
        }
        
        MobyInteger nBlocksMobyInteger = annotations.getNum_blocks();
        
        if (nBlocksMobyInteger != null)
        {
            int nBlocks = nBlocksMobyInteger.getInteger();
            
            if (sb.length() > 0)
            {
                sb.append(", ");
            }
            sb.append("blocks: ").append(Integer.valueOf(nBlocks));
        }
        
        if (sb.length() > 0)
        {
            final int h = (int)getHeight()/2;

            if (h != height || font == null)
            {
                font = new Font("Monospaced", Font.PLAIN, h);
                height = h;
            }

            g.setColor(DESCRIPTION_BG);
            g.fillRoundRect(0, h, getWidth(), h, 4, 4);

            String label = sb.toString();
            
            g.setColor(Color.LIGHT_GRAY);
            g.drawString(label, 2, getHeight() - (int)font.getLineMetrics(label, g.getFontMetrics().getFontRenderContext()).getDescent());
        }        
    }
}
