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

package kiyut.alkitab.navigator;

import java.util.ResourceBundle;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import kiyut.alkitab.bookviewer.BookViewer;
import kiyut.alkitab.util.ComponentOrientationSupport;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.passage.Key;
import org.crosswire.jsword.passage.KeyUtil;

/**
 * Panel which display book Key aka Table of Content. It display the content using
 * {@link kiyut.alkitab.navigator.KeyTree KeyTree}
 * 
 * @author Tonny Kohar <tonny.kohar@gmail.com>
 */
public class BookNavigatorPane extends javax.swing.JPanel {
    protected ResourceBundle bundle = ResourceBundle.getBundle(this.getClass().getName());

    protected KeyTree keyTree;
    protected BookViewer bookViewer;
    protected TreeSelectionListener treeSelectionListener;
    //protected Book book;

    /** Creates new BookNavigatorPane */
    public BookNavigatorPane() {
        initComponents();
        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() {
        java.awt.GridBagConstraints gridBagConstraints;

        scrollPane = new javax.swing.JScrollPane();

        setLayout(new java.awt.BorderLayout());
        add(scrollPane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration//GEN-END:variables
    
    protected void initCustom() {
        treeSelectionListener = new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent evt) {
                keyValueChanged(evt);
            }
        };
    }

    public void setBookViewer(BookViewer bookViewer) {
        this.bookViewer = bookViewer;
    }

    public void setDisplayMode(Book book) {
        BookCategory bookCategory = book.getBookCategory();
        if (bookCategory.equals(BookCategory.BIBLE) || bookCategory.equals(BookCategory.COMMENTARY)) {
            keyTree = new KeyTree(new BibleKeyTreeModel(BibleKeyTreeModel.LEVEL_VERSE));
        } else {
            Key key = book.getGlobalKeyList();
            keyTree = new KeyTree(new DefaultKeyTreeModel(key));
        }

        scrollPane.setViewportView(keyTree);
        keyTree.addTreeSelectionListener(treeSelectionListener);

        // XXX workaround for Sun Java Bug 4845945
        // ComponentOrientation must be applied after the component visible
        ComponentOrientationSupport.applyComponentOrientation(this);

        this.revalidate();
    }
    
    protected void keyValueChanged(TreeSelectionEvent evt) {
        if (bookViewer == null) { return; }

        TreePath treePath = evt.getPath();
        
        KeyTreeNode node = (KeyTreeNode)treePath.getLastPathComponent();
        Key key = node.getKey();
        if (key == null) {
            return;
        }
        
        if (keyTree.getModel() instanceof BibleKeyTreeModel) {
            BibleKeyTreeNode bibleNode = (BibleKeyTreeNode) node;
            if (bibleNode.getCategory() == BibleKeyTreeNode.BIBLE ||
                    bibleNode.getCategory() == BibleKeyTreeNode.BOOK) {
                // this is bible or book level, too big to be displayed
                return;
            }
            // convert the key into Passage
            bookViewer.setKey(KeyUtil.getPassage(key));
            bookViewer.reload();
        } else {
            //System.out.println(key.toString());
            bookViewer.setKey(key);
            bookViewer.reload();
        }
    }
}
