package org.imdea.watson;

import org.protege.editor.owl.model.hierarchy.OWLObjectHierarchyProvider;
import org.protege.editor.owl.ui.view.AbstractOWLClassViewComponent;
import org.semanticweb.owl.model.OWLClass;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

import java.rmi.RemoteException;

import uk.ac.open.kmi.watson.clientapi.OntologySearch;
import uk.ac.open.kmi.watson.clientapi.OntologySearchServiceLocator;
import uk.ac.open.kmi.watson.clientapi.WatsonService;


/*
 * Copyright (C) 2009, IMDEA Software
 *
 * Modifications to the initial code base are copyright of their
 * respective authors, or their employers as appropriate.  Authorship
 * of the modifications may be determined from the ChangeLog placed at
 * the end of this file.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * Author: Ivan Perez<br>
 * <p/>
 * IMDEA Software
 * Date: Jul 14, 2009<br><br>
 * <p/>
 *
 * Shows all the ancestors of the selected class in a text window for cut and paste
 */
public class WatsonSearchView extends AbstractOWLClassViewComponent
{
    public class MainThread extends Thread
    {
        public JTextPane namesPane;
        public SpinnerNumberModel snm;
        public OWLClass selectedClass;
        public OntologySearch os;

        public MainThread (JTextPane n,
                           SpinnerNumberModel nm,
                           OWLClass sc)
        {
		     OntologySearchServiceLocator locator =
                 new OntologySearchServiceLocator();
		     try{
		     	os = locator.getUrnOntologySearch();			
		     }
		     catch (Exception e) {
		     	e.printStackTrace();
		     	System.out.println(e.getCause());
		     }
            namesPane = n;
            snm = nm;
            selectedClass = sc;
        }

        public void run()
        {
             // Object o = this.spinner.getModel().getValue();
             int max = snm.getNumber().intValue();
             if (os != null)
             {
                 String text = "<html><body>";
                 String className = selectedClass.getURI().getFragment();
                 String[] params = {className};
                 try {
                     String[] res = os.getSemanticContentByKeywords(params);
                     if (res.length > 0) {
                         text += "<ul>";
                         for (int i = 0; i < res.length && i<max; i++)
                         {
                             String s = res[i];
                             text += "<li><a href=\"" + s + "\">" + s + "</a><br />";
                             String[] res2 = os.getComments(s);
                             for (String s2 : res2) 
                                 text += s2;
                             text += "</li>";
                         }
                         text += "</ul>";
                     }
                 }
                 catch (Exception e) {
                     e.printStackTrace();
                     System.out.println(e.getCause());
                 }
                 text += "</body></html>";
                 namesPane.setText(text);
             }
        }
    }

    private MainThread m = null;

    private JTextPane namesPane;
    private SpinnerNumberModel numberModel;

    public void initialiseClassView() throws Exception
    {
        setLayout(new BorderLayout(6, 6));
        namesPane = new JTextPane();    
        namesPane.setEditable(false);
        namesPane.addHyperlinkListener(new Hyperactive());
        namesPane.setContentType ("text/html");
        numberModel = new SpinnerNumberModel (3,1,10,1);
        JSpinner spinner = new JSpinner (numberModel);
        JPanel northPanel = new JPanel();
        northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.LINE_AXIS));
        JLabel label = new JLabel ("Max number of results:");
        northPanel.add (label);
        northPanel.add (spinner);
        add(northPanel, BorderLayout.NORTH);
        add(new JScrollPane(namesPane), BorderLayout.CENTER);
    }

    protected OWLClass updateView(OWLClass selectedClass)
    {
             if (this.m != null)
             {
                 m.interrupt();
             }
             if (selectedClass != null)
             {
                 String className = selectedClass.getURI().getFragment();
                 if (!className.equals ("Thing") &&
                     !className.equals ("Nothing"))
                 {
                     this.namesPane.setText("<html><body><center>Loading " + className
                                            + "</center></body></html>");
                     this.m = new MainThread (this.namesPane,
                                              this.numberModel,
                                              selectedClass);
                     m.start();
                 }
             }
             return selectedClass;
    }

    public void disposeView()
    {
        if (this.m != null)
        {
            m.interrupt();
        }
    }

    class Hyperactive implements HyperlinkListener
    {
        public void hyperlinkUpdate(HyperlinkEvent e)
        {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
            {
                JEditorPane pane = (JEditorPane) e.getSource();
                try
                {
                    java.awt.Desktop.getDesktop().browse(e.getURL().toURI());
                }
                catch (Throwable t)
                {
                    t.printStackTrace();
                }
            }
        }
     }

}
