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

package kiyut.alkitab.bookviewer;

import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.event.HyperlinkListener;
import kiyut.alkitab.api.BookViewer;
import kiyut.alkitab.api.History;
import kiyut.alkitab.api.HistoryManager;
import kiyut.alkitab.api.SwordURI;
import kiyut.alkitab.api.ViewerHints;
import kiyut.alkitab.api.event.BookChangeEvent;
import kiyut.alkitab.bookviewer.history.BookViewerHistory;
import kiyut.alkitab.bookviewer.history.BookViewerHistoryManager;
import kiyut.alkitab.options.ViewerHintsOptions;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.passage.Key;

/**
 * Implementation of {@link kiyut.alkitab.api.BookViewer BookViewer} which display single book
 *
 */
public class SingleBookViewerPane extends AbstractBookViewerPane {

    protected ResourceBundle bundle = ResourceBundle.getBundle(SingleBookViewerPane.class.getName());
    protected BookTextPane bookTextPane;
    protected HistoryManager historyManager;
    
    /** Creates new SingleBookViewerPane */
    public SingleBookViewerPane() {
        //this.viewerHints = new ViewerHints<ViewerHints.Key, Object>(ViewerHintsOptions.getInstance().getViewerHints());
        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() {

        bookScrollPane = new javax.swing.JScrollPane();

        setLayout(new java.awt.BorderLayout());
        add(bookScrollPane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane bookScrollPane;
    // End of variables declaration//GEN-END:variables

    protected void initCustom() {
        ViewerHints<ViewerHints.Key,Object> viewerHints = new ViewerHints<ViewerHints.Key,Object>(ViewerHintsOptions.getInstance().getViewerHints());
        bookTextPane = new BookTextPane(viewerHints);
        bookScrollPane.setViewportView(bookTextPane);
        historyManager = new BookViewerHistoryManager();

        //getActionMap().setParent(bookTextPane.getActionMap());
    }

    @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();
    }
    
    @Override
    public JComponent getViewerComponent() {
        return bookTextPane;
    }

    @Override
    public ViewerHints<ViewerHints.Key,Object> getViewerHints() {
        return bookTextPane.getViewerHints();
    }

    @Override
    public void addHyperlinkListener(HyperlinkListener listener) {
        bookTextPane.addHyperlinkListener(listener);
    }

    @Override
    public void removeHyperlinkListener(HyperlinkListener listener) {
        bookTextPane.removeHyperlinkListener(listener);
    }

    @Override
    public List<Book> getBooks() {
        List<Book> srcBooks = bookTextPane.getBooks();
        return Collections.unmodifiableList(srcBooks);
    }

    @Override
    public int getBookCount() {
        return bookTextPane.getBooks().size();
    }
    
    @Override
    public void viewSource() {
        try {
            SourceViewerPane sourcePane = new SourceViewerPane();
            sourcePane.initSource(bookTextPane);
            //sourcePane.initSource(bookTextPane.getBooks(), bookTextPane.getKey(), bookTextPane.getConverter(), bookTextPane.getViewerHints(), bookTextPane.isCompareView());
            sourcePane.showDialog(this,true);
        } catch (Exception ex) {
            Logger logger = Logger.getLogger(this.getClass().getName());
            logger.log(Level.WARNING, ex.getMessage(), ex);
        }
    }

    @Override
    public void openURI(SwordURI uri) {
        openURI(uri, null);
    }

    @Override
    public void openURI(SwordURI uri, String info) {
        Book book = Books.installed().getBook(uri.getPath());
        setBook(book);
        reload();
    }

    public void setBook(String bookName) {
        Book book = Books.installed().getBook(bookName);
        setBook(book);
    }
    
    public void setBook(Book book) {
        List<Book> books = bookTextPane.getBooks();

        if (books.isEmpty()) {
            books.add(book);
            firePropertyChange(BookViewer.VIEWER_NAME, null, getName());
            fireBookChange(new BookChangeEvent(this));
        } else {
            books.set(0, book);
        }
        
        if (book == null) {
            return;
        }
        
        Key tKey = book.getGlobalKeyList();

        if (tKey == null) {
            return;
        }
        if (tKey.getCardinality() > 0) {
            History hist = new BookViewerHistory(tKey,null);
            historyManager.add(hist);
            bookTextPane.setKey(hist.current());
        }
    }
    
    @Override
    public void setKey(Key key) {
        // find the key in history
        History hist = historyManager.current();
        if (hist == null) { return; }
        
        Key keyHist = hist.first();
        if (keyHist == null) {
            return;
        }
        
        while (!keyHist.equals(key)) {
            keyHist = hist.next();
            if (keyHist == null) {
                break;
            }
        }
        
        if (keyHist == null) {
            return;
        }
        
        bookTextPane.setKey(keyHist);
    }

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

    @Override
    public void reload() {
        bookTextPane.reload(true);
    }

    public HistoryManager getHistoryManager() {
        return historyManager;
    }

    public void goPrevious() {
        History hist = historyManager.current();
        if (hist == null) {
            return;
        }
        
        if (!hist.hasPrevious()) {
            return;
        }
        
        bookTextPane.setKey(hist.previous());
        reload();
    }

    public void goNext() {
        History hist = historyManager.current();
        if (hist == null) {
            return;
        }
        
        if (!hist.hasNext()) {
            return;
        }
        
        bookTextPane.setKey(hist.next());
        reload();
    }
    
    /** For single book viewer, this method do nothing 
     * {@inheritDoc}
     */
    @Override
    public void compareView(boolean compare) {
        // do nothing
    }
}
