package kiyut.alkitab.swing;

import kiyut.alkitab.api.BookViewer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.event.HyperlinkListener;
import kiyut.alkitab.api.SwordURI;
import kiyut.alkitab.api.TransformerHints;
import kiyut.alkitab.api.event.BookChangeEvent;
import kiyut.alkitab.options.TransformerHintsOptions;
import kiyut.alkitab.util.SwordUtilities;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.passage.Key;

/**
 * Panel which display single book
 *
 */
public class SingleBookViewerPane extends AbstractBookViewerPane {
    
    protected ResourceBundle bundle = ResourceBundle.getBundle(this.getClass().getName());    
    
    protected BookTextPane bookTextPane;
    
    /** Creates new SingleBookViewerPane */
    public SingleBookViewerPane() {
        this.transformerHints = new TransformerHints<TransformerHints.Key,Object>(TransformerHintsOptions.getInstance().getTransformerHints());
        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() {

        jToolBar1 = new javax.swing.JToolBar();
        prevButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        bookScrollPane = new javax.swing.JScrollPane();

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

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        prevButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kiyut/alkitab/swing/left.png"))); // NOI18N
        prevButton.setToolTipText(bundle.getString("HINT_Previous.Text")); // NOI18N
        prevButton.setFocusable(false);
        prevButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        prevButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(prevButton);

        nextButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kiyut/alkitab/swing/right.png"))); // NOI18N
        nextButton.setToolTipText(bundle.getString("HINT_Next.Text")); // NOI18N
        nextButton.setFocusable(false);
        nextButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        nextButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(nextButton);

        add(jToolBar1, java.awt.BorderLayout.NORTH);
        add(bookScrollPane, 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.JToolBar jToolBar1;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton prevButton;
    // End of variables declaration//GEN-END:variables
    
    protected void initCustom() {
        bookTextPane = new BookTextPane(transformerHints);
        bookScrollPane.setViewportView(bookTextPane);
     
        getActionMap().setParent(bookTextPane.getActionMap());
        
        prevButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                Key key = SwordUtilities.getPreviousKey(getKey());
                if (key != null) {
                    setKey(key);
                }
            }
        });
        
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                Key key = SwordUtilities.getNextKey(getKey());
                if (key != null) {
                    setKey(key);
                }
            }
        });
    }
    
    @Override
    public String getName() {
        if (bookTextPane == null) {
            return "";
        }
        
        List<Book> books = bookTextPane.getBooks();
        
        if (books.isEmpty()) {
            return "";
        }
        
        Book book = books.get(0);
        if (book == null) {
            return "";
        }
        
        return book.getInitials();
    }
    
    public void addHyperlinkListener(HyperlinkListener listener) {
        bookTextPane.addHyperlinkListener(listener);
    }
    
    public void removeHyperlinkListener(HyperlinkListener listener) {
        bookTextPane.removeHyperlinkListener(listener);
    }
    
    public List<Book> getBooks() {
        List<Book> srcBooks = bookTextPane.getBooks();
        return Collections.unmodifiableList(srcBooks);
    }


    public int getBookCount() {
        return bookTextPane.getBooks().size();
    }
    
    public void openURI(SwordURI uri) {
        List<Book> books = bookTextPane.getBooks();
        Book book =  Books.installed().getBook(uri.getPath());
        
        if (books.isEmpty()) {
            books.add(book);
            firePropertyChange(BookViewer.VIEWER_NAME, null, getName());
            fireBookChange(new BookChangeEvent(this));
        } else {
            books.set(1,book);
        }
        
        if (book == null) { 
            return; 
        }
        
        Key tKey = book.getGlobalKeyList();
        
        if (tKey == null) { return; }
        if (tKey.getCardinality() > 0) {
            setKey(tKey.get(0));
        }
    }
    
    public void setKey(Key key) {
        bookTextPane.setKey(key);
        bookTextPane.refresh();
        updateActions();
    }

    public Key getKey() {
        return bookTextPane.getKey();
    }

    public void refresh() {
        bookTextPane.refresh();
    }
    
    protected void updateActions() {
        boolean enabled = false;
        
        if (SwordUtilities.getPreviousKey(getKey()) != null) {
            enabled = true;
        }
        prevButton.setEnabled(enabled);
        
        enabled = false;
        if (SwordUtilities.getNextKey(getKey()) != null) {
            enabled = true;
        }
        nextButton.setEnabled(enabled);
    }
    
    /** For single book viewer, this method do nothing 
     * {@inheritDoc}
     */
    public void compareView(boolean compare) {
        // do nothing
    }
}
