/**
 * 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.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.Scrollable;
import org.inb.biomoby.client.gui.AbstractMobyComponent;
import org.inb.biomoby.client.gui.auxiliary.ScrollableTextPane;
import org.inb.biomoby.shared.datatypes.MobyString;
import org.inb.biomoby.shared.ontology.OMIM;
import org.inb.biomoby.shared.ontology.Text__formatted;
import org.inb.biomoby.client.gui.swing.CollapsablePanel;
import org.inb.biomoby.client.gui.swing.border.RoundedBorder;

/**
 * @author Dmitry Repchevsky
 */

public class OMIMComponent<T extends OMIM> extends AbstractMobyComponent<T>
{
    public OMIMComponent(T omim)
    {
        super(omim);

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setBorder(new RoundedBorder(4));

        JPanel main = new ScrollableJPanel();
        main.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2,2,2,2);

        int gridy = 0;

        MobyString number = omim.getNumber();
        if (number != null)
        {
            String string = number.getString();

            if (string != null && string.length() > 0)
            {
                c.gridx = 0;
                c.weightx = 0;
                c.anchor = GridBagConstraints.EAST;
                c.fill = GridBagConstraints.NONE;
                main.add(new JLabel("id:"), c);

                JTextField label = new JTextField(string);
                label.setEditable(false);
                label.setToolTipText(string);

                c.gridx = 1;
                c.weightx = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                main.add(label, c);
            }
        }

        MobyString title = omim.getTitle();
        if (title != null)
        {
            String string = title.getString();

            if (string != null && string.length() > 0)
            {
                c.gridx = 0;
                c.gridy = ++gridy;
                c.weightx = 0;
                c.anchor = GridBagConstraints.EAST;
                c.fill = GridBagConstraints.NONE;
                main.add(new JLabel("title:"), c);

                JTextField field = new JTextField(string);
                field.setEditable(false);

                c.gridx = 1;
                c.weightx = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                main.add(field, c);
            }
        }

        MobyString author = omim.getAuthor();
        if (author != null)
        {
            String string = author.getString();

            if (string != null && string.length() > 0)
            {
                c.gridx = 0;
                c.gridy = ++gridy;
                c.weightx = 0;
                c.fill = GridBagConstraints.NONE;
                main.add(new JLabel("author:"), c);

                JTextField label = new JTextField(string);
                label.setEditable(false);
                label.setToolTipText(string);

                c.gridx = 1;
                c.weightx = 1;
                c.fill = GridBagConstraints.HORIZONTAL;

                main.add(label, c);
            }
        }

        Text__formatted text = omim.getText();
        if (text != null)
        {
            CollapsablePanel panel = new CollapsablePanel(new Text__formattedComponent(text), "Text", false);
            c.gridx = 0;
            c.gridy = ++gridy;
            c.gridwidth = 2;
            c.weightx = 1;
            c.weighty = 0;
            c.fill = GridBagConstraints.HORIZONTAL;
            main.add(panel, c);
        }

        List<? extends Text__formatted> allelicVariants = omim.getAllelicvariants();
        if (allelicVariants != null && allelicVariants.size() > 0)
        {
            StringBuilder sb = new StringBuilder();

            for (Text__formatted variants : allelicVariants)
            {
                MobyString mobyString = variants.getContent();

                if (mobyString != null)
                {
                    String string = mobyString.getString();

                    if (string != null)
                    {
                        sb.append(string).append("\n");
                    }
                }
            }
            
            final int length = sb.length();
            if (length > 0)
            {
                sb.delete(length - 2, length); // remove the last "\n"

                ScrollableTextPane scroll = new ScrollableTextPane(sb.toString());

                CollapsablePanel panel = new CollapsablePanel(scroll, "Allelic Variants", false);

                c.gridx = 0;
                c.gridy = ++gridy;
                c.gridwidth = 2;
                c.weightx = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                main.add(panel, c);
            }
        }

        List<? extends Text__formatted> clinicalSynopsis = omim.getClinicalSynopsis();
        if (clinicalSynopsis != null && clinicalSynopsis.size() > 0)
        {
            StringBuilder sb = new StringBuilder();

            for (Text__formatted synopsis : clinicalSynopsis)
            {
                MobyString mobyString = synopsis.getContent();

                if (mobyString != null)
                {
                    String string = mobyString.getString();

                    if (string != null)
                    {
                        sb.append(string).append("\n");
                    }
                }
            }

            final int length = sb.length();
            if (length > 0)
            {
                sb.delete(length - 2, length); // remove the last "\n"

                ScrollableTextPane scroll = new ScrollableTextPane(sb.toString());

                CollapsablePanel panel = new CollapsablePanel(scroll, "Clinical Synopsis", false);

                c.gridx = 0;
                c.gridy = ++gridy;
                c.gridwidth = 2;
                c.weightx = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                main.add(panel, c);
            }
        }

        List<? extends MobyString> seeAlso = omim.getSeealso();
        if (seeAlso != null && seeAlso.size() > 0)
        {
            StringBuilder sb = new StringBuilder();

            for (MobyString also : seeAlso)
            {
                String string = also.getString();

                if (string != null)
                {
                    sb.append(string).append("\n");
                }
            }

            final int length = sb.length();
            if (length > 0)
            {
                sb.delete(length - 2, length); // remove the last "\n"

                ScrollableTextPane scroll = new ScrollableTextPane(sb.toString());

                CollapsablePanel panel = new CollapsablePanel(scroll, "See Also", false);

                c.gridx = 0;
                c.gridy = ++gridy;
                c.gridwidth = 2;
                c.weightx = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                main.add(panel, c);
            }
        }

        List<? extends MobyString> journals = omim.getJournal();
        if (journals != null && journals.size() > 0)
        {
            StringBuilder sb = new StringBuilder();

            for (MobyString journal : journals)
            {
                String string = journal.getString();

                if (string != null)
                {
                    sb.append(string).append("\n");
                }
            }

            final int length = sb.length();
            if (length > 0)
            {
                sb.delete(length - 2, length); // remove the last "\n"

                ScrollableTextPane scroll = new ScrollableTextPane(sb.toString());

                CollapsablePanel panel = new CollapsablePanel(scroll, "References", false);

                c.gridx = 0;
                c.gridy = ++gridy;
                c.gridwidth = 2;
                c.weightx = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                main.add(panel, c);
            }
        }

        List<? extends MobyString> contributors = omim.getContributor();
        if (contributors != null)
        {
            StringBuilder sb = new StringBuilder();

            for (MobyString contributor : contributors)
            {
                String string = contributor.getString();
                if (string != null)
                {
                    sb.append(string).append("\n");
                }
            }

            final int length = sb.length();
            if (length > 0)
            {
                sb.delete(length - 2, length); // remove the last "\n"

                ScrollableTextPane scroll = new ScrollableTextPane(sb.toString());
                CollapsablePanel panel = new CollapsablePanel(scroll, "Contributors", false);

                c.gridx = 0;
                c.gridy = ++gridy;
                c.gridwidth = 2;
                c.weightx = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                main.add(panel, c);
            }
        }

        List<? extends MobyString> editorHistory = omim.getEditorhistory();
        if (editorHistory != null && editorHistory.size() > 0)
        {
            StringBuilder sb = new StringBuilder();

            for (MobyString history : editorHistory)
            {
                String string = history.getString();

                if (string != null)
                {
                    sb.append(string).append("\n");
                }
            }

            final int length = sb.length();
            if (length > 0)
            {
                sb.delete(length - 2, length); // remove the last "\n"

                ScrollableTextPane scroll = new ScrollableTextPane(sb.toString());

                CollapsablePanel panel = new CollapsablePanel(scroll, "Edit History", false);

                c.gridx = 0;
                c.gridy = ++gridy;
                c.gridwidth = 2;
                c.weightx = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                main.add(panel, c);
            }
        }

        c.gridx = 0;
        c.gridy = ++gridy;
        c.gridwidth = 2;
        c.weightx = 1;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;

        main.add(Box.createGlue(), c);

        JScrollPane scroll = new JScrollPane(main, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        add(scroll);
    }

    private static class ScrollableJPanel extends JPanel implements Scrollable
    {
	public Dimension getPreferredScrollableViewportSize()
	{
            return getPreferredSize();
	}

	public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
	{
            return 20;
	}

	public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
	{
            return 60;
	}

        /**
         * Force the width of this Scrollable to match the width of the viewport.
         * @return true.
         */
	public boolean getScrollableTracksViewportWidth()
	{
            return true;
	}

	public boolean getScrollableTracksViewportHeight()
	{
            return false;
	}
    }
}
