package kiyut.alkitab.swing;

import java.util.List;
import javax.swing.ListSelectionModel;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import kiyut.alkitab.api.TransformerHints;
import kiyut.alkitab.options.TransformerHintsOptions;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.passage.Key;
import org.crosswire.jsword.passage.PreferredKey;

/**
 * Panel which display single book with list of key on the right side
 * 
 */
public class DefinitionPane extends javax.swing.JPanel {
    
    //protected Book book;
    
    protected BookTextPane bookTextPane;
    protected TransformerHints<TransformerHints.Key,Object> transformerHints;
    
    /** Creates new DefinitionPane */
    public DefinitionPane() {
        this(null);
    }
    
    public DefinitionPane(Book book) {
        this.transformerHints = new TransformerHints<TransformerHints.Key,Object>(TransformerHintsOptions.getInstance().getTransformerHints());
        initComponents();
        initCustom();
        setBook(book);
    }
    
    /** 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() {

        jSplitPane1 = new javax.swing.JSplitPane();
        bookScrollPane = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        indexScrollPane = new javax.swing.JScrollPane();
        keyList = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setResizeWeight(1.0);
        jSplitPane1.setLeftComponent(bookScrollPane);

        jPanel1.setLayout(new java.awt.BorderLayout(0, 3));

        indexScrollPane.setViewportView(keyList);

        jPanel1.add(indexScrollPane, java.awt.BorderLayout.CENTER);

        jSplitPane1.setRightComponent(jPanel1);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane bookScrollPane;
    private javax.swing.JScrollPane indexScrollPane;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JList keyList;
    // End of variables declaration//GEN-END:variables
    
    protected void initCustom() {
        bookTextPane = new BookTextPane(transformerHints);
        bookScrollPane.setViewportView(bookTextPane);
        
        //getActionMap().setParent(bookTextPane.getActionMap());
        
        keyList.setPrototypeCellValue("DICTIONARY INDEX TEXT");
        keyList.setModel(new KeyListModel());
        keyList.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        keyList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent evt) {
                keyValueChanged(evt);
            }
        });
    }
    
    public void setBook(Book book) {
        if (book != null) {
            List<Book> books = bookTextPane.getBooks();
            if (books.isEmpty()) {
                books.add(book);
            } else {
                books.set(0, book);
            }
            
            Key key = book.getGlobalKeyList();
            ((KeyListModel)keyList.getModel()).setKey(key);
            
            Key prefKey = null;
            if (book instanceof PreferredKey) {
                prefKey = ((PreferredKey)book).getPreferred();
            } 
            
            int index = 0;
            if (prefKey != null) {
                 index = key.indexOf(prefKey);
                 if (index < 0) { index = 0; }
            } 
            
            if (keyList.getModel().getSize() > index) {
                keyList.setSelectedIndex(index);
            }
        } else {
            bookTextPane.getBooks().clear();
        }
    }
    
    public Book getBook() {
        List<Book> books = bookTextPane.getBooks();
        if (books.isEmpty()) {
            return null;
        }
        return books.get(0);
    }
    
    public TransformerHints<TransformerHints.Key,Object> getTransformerHints() {
        if (transformerHints == null) {
            transformerHints = new TransformerHints<TransformerHints.Key,Object>();
        }
        return transformerHints;
    }
    
    public void addHyperlinkListener(HyperlinkListener listener) {
        bookTextPane.addHyperlinkListener(listener);
    }
    
    public void removeHyperlinkListener(HyperlinkListener listener) {
        bookTextPane.removeHyperlinkListener(listener);
    }
    
    protected void keyValueChanged(ListSelectionEvent evt) {
        if (evt.getValueIsAdjusting()) { return; }
        int index = keyList.getSelectedIndex();
        if (index == -1) { return; }
        Key key = (Key)keyList.getModel().getElementAt(index);
        // XXX bug on JSword ?, if using string it will not find the key
        // eg: Daily Devotional
        /*System.out.println("key: " + key.toString());
        System.out.println("   : " + key.getName());
        System.out.println("   : " + key.getOsisID());
        System.out.println("   : " + key.getOsisRef());
         */
        //bookTextPane.setViewKey(key.toString());
        bookTextPane.setKey(key);
        bookTextPane.refresh(true);
        //System.out.println("DefinitionPane.keyValueChanged" + key);
    }
}