/* This work has been placed into the public domain. */

package kiyut.alkitab.windows;

import kiyut.alkitab.windows.BookViewerTopComponent;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import kiyut.alkitab.swing.BookNavigatorPane;
import kiyut.alkitab.api.BookViewer;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.Utilities;

/**
 * Top component which displays {@link kiyut.alkitab.swing.BookNavigatorPane BookNavigatorPane}.
 */
public final class BookNavigatorTopComponent extends TopComponent {

    private static BookNavigatorTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";

    private static final String PREFERRED_ID = "BookNavigatorTopComponent";
    
    private PropertyChangeListener tcPropertyChangeListener;
    
    private Lookup.Result result = null;
    private LookupListener bookViewerLookupListener = null;
    
    private Map<BookViewer,BookNavigatorPane> navigatorMap;
    
    private BookViewer bookViewer;

    private BookNavigatorTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(BookNavigatorTopComponent.class, "CTL_BookNavigatorTopComponent"));
        setToolTipText(NbBundle.getMessage(BookNavigatorTopComponent.class, "HINT_BookNavigatorTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
        
        initCustom();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized BookNavigatorTopComponent getDefault() {
        if (instance == null) {
            instance = new BookNavigatorTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the BookNavigatorTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized BookNavigatorTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(BookNavigatorTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof BookNavigatorTopComponent) {
            return (BookNavigatorTopComponent) win;
        }
        Logger.getLogger(BookNavigatorTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
        TopComponent.getRegistry().addPropertyChangeListener(tcPropertyChangeListener);
        displayBookNavigator(bookViewer);
    }

    @Override
    public void componentClosed() {
        TopComponent.getRegistry().removePropertyChangeListener(tcPropertyChangeListener);
    }

    /** replaces this in object stream */
    @Override
    public Object writeReplace() {
        return new ResolvableHelper();
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    final static class ResolvableHelper implements Serializable {

        private static final long serialVersionUID = 1L;

        public Object readResolve() {
            return BookNavigatorTopComponent.getDefault();
        }
    }
    
    private void initCustom() {
        navigatorMap = new HashMap<BookViewer,BookNavigatorPane>();
        
        tcPropertyChangeListener = new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                Object obj = evt.getNewValue();
                if (!(obj instanceof BookViewerTopComponent)) {
                    return;
                }
                
                String propName = evt.getPropertyName();
                if (!propName.equals(TopComponent.Registry.PROP_TC_CLOSED)) {
                    return;
                }
                
                BookViewerTopComponent tc = (BookViewerTopComponent)obj;
                bookViewer = tc.getBookViewer();
                unregisterBookViewer(bookViewer);
                bookViewer = null;
            }
        };
        
        bookViewerLookupListener = new LookupListener() {
            public void resultChanged(LookupEvent lookupEvent) {
                bookViewerLookupListenerResultChanged(lookupEvent);
            }
        };
        
        result = Utilities.actionsGlobalContext().lookupResult(BookViewer.class);
        result.addLookupListener(bookViewerLookupListener);
        //result.allInstances(); // needed to tell Nb that it is processed
        bookViewerLookupListenerResultChanged(new LookupEvent(result));
    }
    
    private void bookViewerLookupListenerResultChanged(LookupEvent lookupEvent) {
        Lookup.Result r = (Lookup.Result)lookupEvent.getSource();
        Collection c = r.allInstances();
        if (!c.isEmpty()) {
            bookViewer = (BookViewer)c.iterator().next();
            BookNavigatorPane navPane = navigatorMap.get(bookViewer);
            if (navPane == null) {
                registerBookViewer(bookViewer);
            }
            displayBookNavigator(bookViewer);
        } 
    }
    
    
    private void registerBookViewer(BookViewer bookViewer) {
        if (bookViewer == null) { return; }
        BookNavigatorPane navPane = new BookNavigatorPane();
        navPane.setBookViewer(bookViewer);
        navigatorMap.put(bookViewer,navPane);
        
        //System.out.println("BookNavTC.registerBookViewer()");
    }
    
    private void unregisterBookViewer(BookViewer bookViewer) {
        BookNavigatorPane navPane = navigatorMap.remove(bookViewer);
        if (navPane != null) {
            navPane.setBookViewer(null);
        }
    }
    
    private void displayBookNavigator(BookViewer bookViewer) {
        BookNavigatorPane navPane = navigatorMap.get(bookViewer);
        if (navPane == null) {
            return;
        }
        
        this.removeAll();
        navigatorMap.put(bookViewer,navPane);
        this.add(BorderLayout.CENTER,navPane);
        this.revalidate();
        this.repaint();
        
        //System.out.println("BookNavTC.displayBookNavigator()");
    }
}
