/* Copyright (C) 2007-2017 Patrick G. Durand
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  You may obtain a copy of the License at
 *
 *     https://www.gnu.org/licenses/agpl-3.0.txt
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 */
package bzh.plealog.dbmirror.main;

import java.awt.BorderLayout;
import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.swing.JPanel;

import com.plealog.genericapp.api.EZApplicationBranding;
import com.plealog.genericapp.api.EZEnvironment;
import com.plealog.genericapp.api.EZGenericApplication;
import com.plealog.genericapp.api.EZUIStarterListener;

import bzh.plealog.dbmirror.ui.DBMSPanel;
import bzh.plealog.dbmirror.ui.DBMSUserInterface;
import bzh.plealog.dbmirror.ui.resources.DBMSMessages;
import bzh.plealog.dbmirror.util.conf.Configuration;
import bzh.plealog.dbmirror.util.conf.DBMSAbstractConfig;

public class UiInstaller {

  public static void main(String[] args) {
    // This has to be done at the very beginning, i.e. first method call within
    // main().
    EZGenericApplication.initialize("DB-Manager");
    // Add application branding
    Properties props = StarterUtils.getVersionProperties();
    EZApplicationBranding.setAppName(props.getProperty("prg.app.name"));
    EZApplicationBranding.setAppVersion(props.getProperty("prg.version"));
    EZApplicationBranding.setCopyRight(props.getProperty("prg.copyright"));
    EZApplicationBranding.setProviderName(props.getProperty("prg.provider"));

    EZEnvironment.addResourceLocator(DBMSMessages.class);

    // ...before any attempt to load a resource
    EZApplicationBranding.setAppIcon(EZEnvironment.getImageIcon("appicon.png"));

    // we load our ResourceBundle containing the main menu declarations
    ResourceBundle rb = ResourceBundle.getBundle(DBMSMessages.class
        .getPackage().getName() + ".menu");
    EZEnvironment.setUserDefinedActionsResourceBundle(rb);

    // we load our message resource bundle
    rb = ResourceBundle.getBundle(DBMSMessages.class.getPackage().getName()
        + ".messages");
    EZEnvironment.setUserDefinedMessagesResourceBundle(rb);

    StarterUtils.configureApplication(null, "kdmsUI", true, true, true);
    DBMSAbstractConfig.setStandalone(true);

    // install the action manager listener to easily handle actions
    EZEnvironment.getActionsManager().addActionMenuListener(
        new MyActionManager());
    // Add a listener to application startup cycle (see below)
    EZEnvironment.setUIStarterListener(new MyStarterListener());

    // We setup the Preferences Dialogue Box
    String confPath = DBMSAbstractConfig.getInstallAppConfPath(Configuration.SYSTEM);
    confPath += "prefEditor.config";
    EZEnvironment.setPreferencesConfigurationFile(confPath);

    // Start the application
    EZGenericApplication.startApplication(args);
  }

  private static class MyStarterListener implements EZUIStarterListener {
    private JPanel mainPanel = null;

    @Override
    public Component getApplicationComponent() {
      if (mainPanel != null)
        return mainPanel;

      mainPanel = new JPanel(new BorderLayout());
      DBMSPanel pnl = DBMSUserInterface.getUserInterface(
          DBMSAbstractConfig.getInstallAppConfPath(Configuration.DESCRIPTOR), true);
      mainPanel.add(pnl, BorderLayout.CENTER);
      return mainPanel;
    }

    @Override
    public boolean isAboutToQuit() {
      // You can add some code to figure out if application can exit.

      // Return false to prevent application from exiting (e.g. a background
      // task is still running).
      // Return true otherwise.

      // Do not add a Quit dialogue box to ask user confirmation: the framework
      // already does that for you.
      return true;
    }

    @Override
    public void postStart() {
      // This method is called by the framework just before displaying UI
      // (main frame).
    }

    @Override
    public void preStart() {
      // This method is called by the framework at the very beginning of
      // application startup.
    	URI rootURI = null;
		try {
			rootURI = new URI("file:///");
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Path rootPath = Paths.get(rootURI);
        Path dirPath = rootPath.resolve("/home1/homedir4/perso/pgdurand/devel/BeeDeeM");
    	//FileSystem fs = FileSystems.getFileSystem(dirPath.toUri());//.getDefault();
    	FileSystem fs = FileSystems.getDefault();
    	for (FileStore store : fs.getFileStores()) {
    	      printDetails(store);
    	}
    }
  }

  public static void printDetails(FileStore store) {
	    try {
	      String desc = store.name();
	      String type = store.type();
	      long totalSpace = store.getTotalSpace();
	      long unallocatedSpace = store.getUnallocatedSpace();
	      long availableSpace = store.getUsableSpace();
	      
	      System.out.println(desc + " (" + type + "), Total: " + totalSpace + ",  Unallocated: "
	          + unallocatedSpace + ",  Available: " + availableSpace);
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	  }
  /*
   * Show how to work with generic action (see ui.properties for the definition
   * of action FileOpen)
   */
  private static class MyActionManager implements PropertyChangeListener {

    @Override
    public void propertyChange(PropertyChangeEvent event) {
    }
  }
}

@@ -845,268 +845,8 @@
 er;%0A
-import java.io.File;%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.nio.file.FileStore;%0Aimport java.nio.file.FileSystem;%0Aimport java.nio.file.FileSystems;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A
 impo
@@ -4590,1070 +4590,14 @@
     
-%09URI rootURI = null;%0A%09%09try %7B%0A%09%09%09rootURI = new URI(%22file:///%22);%0A%09%09%7D catch (URISyntaxException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A        Path rootPath = Paths.get(rootURI);%0A        Path dirPath = rootPath.resolve(%22/home1/homedir4/perso/pgdurand/devel/BeeDeeM%22);%0A    %09//FileSystem fs = FileSystems.getFileSystem(dirPath.toUri());//.getDefault();%0A    %09FileSystem fs = FileSystems.getDefault();%0A    %09for (FileStore store : fs.getFileStores()) %7B%0A    %09      printDetails(store);%0A    %09%7D%0A    %7D%0A  %7D%0A%0A  public static void printDetails(FileStore store) %7B%0A%09    try %7B%0A%09      String desc = store.name();%0A%09      String type = store.type();%0A%09      long totalSpace = store.getTotalSpace();%0A%09      long unallocatedSpace = store.getUnallocatedSpace();%0A%09      long availableSpace = store.getUsableSpace();%0A%09      %0A%09      System.out.println(desc + %22 (%22 + type + %22), Total: %22 + totalSpace + %22,  Unallocated: %22%0A%09          + unallocatedSpace + %22,  Available: %22 + availableSpace);%0A%09    %7D catch (IOException e) %7B%0A%09      e.printStackTrace();%0A%09    %7D%0A%09  %7D
+%7D%0A  %7D%0A
 %0A  /

remove file system testing code