/**
 * 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.util.List;
import javax.swing.BoxLayout;
import org.inb.biomoby.client.gui.AbstractMobyComponent;
import org.inb.biomoby.client.gui.auxiliary.AbstractAnnotationListPane.AnnotationListListener;
import org.inb.biomoby.client.gui.auxiliary.EquicktandemAnnotationListPane;
import org.inb.biomoby.client.gui.IDynamicComponent;
import org.inb.biomoby.client.gui.ISequenceCoordinateTransformer;
import org.inb.biomoby.client.gui.ISequenceDependent;
import org.inb.biomoby.client.gui.ITransformable;
import org.inb.biomoby.client.gui.ontology.CommentedNASequenceComponent.CommentedNASequenceListener;
import org.inb.biomoby.client.gui.ontology.EquicktandemAnnotatedNASequenceComponent.EquicktandemAnnotatedNASequenceListener;
import org.inb.biomoby.shared.datatypes.MobyInteger;
import org.inb.biomoby.shared.ontology.Annotation;
import org.inb.biomoby.shared.ontology.EquicktandemAnnotatedNASequence;

/**
 * @author Dmitry Repchevsky
 */

public class EquicktandemAnnotatedNASequenceComponent extends AbstractMobyComponent
        implements IDynamicComponent<EquicktandemAnnotatedNASequenceListener>,
                   ISequenceDependent, 
                   ITransformable,
                   CommentedNASequenceListener,
                   AnnotationListListener
{
    private AnnotatedNASequenceComponent annotatedNASequencePanel;
    private EquicktandemAnnotationListPane annotations;
    
    private EquicktandemAnnotatedNASequenceComponent(AnnotatedNASequenceComponent annotatedNASequencePanel)
    {   
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        
        add(annotatedNASequencePanel);
        
        annotatedNASequencePanel.addListener(this); // will proxy events
        
        annotations = new EquicktandemAnnotationListPane();
        annotations.setTransformer(annotatedNASequencePanel.getTransformer());
        
        annotations.addListener(this);
        
        add(annotations);
        
        this.annotatedNASequencePanel = annotatedNASequencePanel;
    }

    public EquicktandemAnnotatedNASequenceComponent()
    {
        this(new AnnotatedNASequenceComponent());
    }
    
    public EquicktandemAnnotatedNASequenceComponent(EquicktandemAnnotatedNASequence sequence)
    {
        this(new AnnotatedNASequenceComponent(sequence));
        
        annotations.setAnnotationList(sequence.getAnnotations());
    }

    public EquicktandemAnnotatedNASequence getData()
    {
        return (EquicktandemAnnotatedNASequence)annotatedNASequencePanel.getData();
    }

    @Override
    public void setPosition(int position)
    {
        annotatedNASequencePanel.setPosition(position);
        
        repaint();
    }

    @Override
    public void setSequenceVisible(boolean isVisible)
    {
        annotatedNASequencePanel.setSequenceVisible(isVisible);
        
        // if sequence is unvisible we can not transform annotations based on it...
        annotations.setTransformer(isVisible ? annotatedNASequencePanel.getTransformer() : null);
    }

    @Override
    public void setTransformer(ISequenceCoordinateTransformer transformer)
    {
        annotations.setTransformer(transformer);
    }

    @Override
    public ISequenceCoordinateTransformer getTransformer()
    {
        return annotations.getTransformer();
    }
    
    @Override
    public void positionChanged(int pos)
    {
        annotations.repaint();
    }

    @Override
    public void annotationsSelected(List<? extends Annotation> annotations)
    {
        if (!annotations.isEmpty())
        { // set position to the first annotation
            Annotation annotation = annotations.get(0);
            
            MobyInteger mobyInteger = annotation.getStart();

            if (mobyInteger != null)
            {
                final int position = mobyInteger.getInteger();
                setPosition(position);
            }
        }
    }

    @Override
    public void addListener(MobyComponentListener listener) 
    {
        annotatedNASequencePanel.addListener(listener);
        annotations.addListener(listener);
    }

    @Override
    public void removeListener(MobyComponentListener listener)
    {
        annotatedNASequencePanel.removeListener(listener);
        annotations.removeListener(listener);
    }

    public static interface EquicktandemAnnotatedNASequenceListener 
            extends CommentedNASequenceListener, AnnotationListListener
    {
    }
}