package kiyut.alkitab.core;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.filechooser.FileSystemView;
import kiyut.alkitab.Application;
import kiyut.alkitab.windows.BookshelfTopComponent;
import org.crosswire.jsword.book.sword.SwordBookPath;
import org.openide.modules.ModuleInstall;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Core Module Install for Netbeans Platform
 * 
 */
public class CoreModuleInstall extends ModuleInstall {
    @Override
    public  void restored() {
        super.restored();
        
        //System.setProperty("netbeans.buildnumber", Application.VERSION);
        System.setProperty("alkitab.buildnumber", Application.getBuildNumber());
        System.setProperty("alkitab.version", Application.getVersion());
        
        // override user.dir variable to the 
        String def =  FileSystemView.getFileSystemView().getDefaultDirectory().getPath();
        System.setProperty("user.dir", def);
        
        //TODO set this when on windows [HOME]/.sword
        //SwordBookPath.setAugmentPath(arg0)
        //setAugmentPath(File[] theNewDirs) throws BookException
        
        //System.out.println(System.getProperty ("user.home"));
        //System.out.println(System.getProperty ("netbeans.user"));
        
        StringBuilder sb = new StringBuilder();
        
        File[] files;
        File file;
        
        sb.append("AugmentPath:\n");
        files = SwordBookPath.getAugmentPath();
        for (int i=0; i<files.length; i++) {
            sb.append("\t" + files[i].toString() + "\n");
        }
        
        sb.append("SwordPath:\n");
        files = SwordBookPath.getSwordPath();
        for (int i=0; i<files.length; i++) {
            sb.append("\t" + files[i].toString() + "\n");
        }
        
        sb.append("DownloadDir:\n");
        file = SwordBookPath.getDownloadDir();
        sb.append("\t" + (file != null ? file.toString() : "") + "\n");
        
        sb.append("SwordDownloadDir:\n");
        file = SwordBookPath.getSwordDownloadDir();
        sb.append("\t" + (file != null ? file.toString() : "") + "\n");
        
        sb.append("-----------------------------------------------------------------\n");
        
        Logger logger = Logger.getLogger(this.getClass().getName());
        logger.log(Level.INFO,"Sword Path");
        logger.log(Level.INFO,sb.toString());
        
        // XXX why this is not working
        //ToolbarPool.getDefault().setConfiguration("CoreToolbar"); 
        
        
        // XXX hide all toolbar, because no icon yet
        /*Toolbar[] toolbars = ToolbarPool.getDefault().getToolbars();
        for (int i=0; i<toolbars.length; i++) {
            toolbars[i].setVisible(false);
        }*/
        
        
        WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
            public void run() {
                final Frame mainWindow = WindowManager.getDefault().getMainWindow();
                mainWindow.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowOpened(WindowEvent evt) {
                        // set active BookshelfTopComponent if opened
                        Set set = WindowManager.getDefault().getRegistry().getOpened();
                        TopComponent tc = null;
                        Iterator it = set.iterator();
                        while (it.hasNext()) {
                            tc = (TopComponent) it.next();
                            if (tc instanceof BookshelfTopComponent) {
                                break;
                            }
                        }
                        if (tc != null) {
                            tc.requestActive();
                        }
                    }
                });
            }
        });

    }
}
