/*
 * Copyright
 *   2005 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;

import fr.gouv.finances.cp.utils.ui.IhmFactory;
import fr.gouv.finances.cp.utils.ui.UiUtils;
import fr.gouv.finances.cp.xemelios.common.Constants;
import fr.gouv.finances.cp.xemelios.common.listeners.StartListener;
import fr.gouv.finances.cp.xemelios.ui.ImageResources;
import fr.gouv.finances.cp.xemelios.ui.MainWindow;

/**
 * @author chm
 */
public class Splash extends JWindow implements StartListener {
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 3257570598480654904L;
    private static final int MAX_PROGRESS=46;
    public static Splash instance = null;
    private JProgressBar progress;
    private int requiredProgress=0;
    
    static {
        if("true".equals(System.getProperty(Constants.SYS_PROP_DEBUG_UI))) {
            //RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());
        }
    }
    
    public Splash() {
        super();
        initialize();
        instance = this;
        /**
         * Si activé, empêche d'avoir le focus dans le champs password sous windows.
         */
//		setAlwaysOnTop(true);
    }
    
    protected void initialize() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        IhmFactory iF = IhmFactory.newInstance();
        ImageIcon ii = iF.getIconFromResource(ImageResources.SPLASH);
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(new JLabel(ii),BorderLayout.CENTER);
        pane.add(progress=new JProgressBar(JProgressBar.HORIZONTAL,0,MAX_PROGRESS),BorderLayout.SOUTH);
        getContentPane().add(pane);
        pack();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((int)(screen.getWidth()-getWidth())/2, (int)(screen.getHeight()-getHeight())/2);
        addFocusListener(new java.awt.event.FocusListener() {
            public void focusGained(java.awt.event.FocusEvent e) {
                toFront();
            }
            public void focusLost(java.awt.event.FocusEvent e) {
                toFront();
            }
        });
    }
    public void pushProgress() {
        final Thread runner = new Thread() {
            @Override
            public void run() {
                int p = requiredProgress;
                p++;
                if(p<MAX_PROGRESS) {
                    progress.setValue(p);
                    
                } else if(p==MAX_PROGRESS) {
                    progress.setValue(MAX_PROGRESS);
                    
                } else {
                    System.out.println("MAX_PROGRESS trop petit: "+p);
                }
                requiredProgress = p;
            }
        };
        UiUtils.invokeInEDT(runner,true);
    }
    public void completeProgress() {
        final Thread runner = new Thread() {
            @Override
            public void run() {
                progress.setValue(MAX_PROGRESS);
            }
        };
        UiUtils.invokeInEDT(runner,true);
    }
    /**
     * @see fr.gouv.finances.cp.xemelios.common.listeners.StartListener#notifyMessage(java.lang.String)
     */
    public void notifyMessage(String msg) {
        // les messages ne sont pas gérés pour le moment
    }
    /**
     * @see fr.gouv.finances.cp.xemelios.common.listeners.StartListener#notifyProgress(int)
     */
    public void notifyProgress(int progress) {
        if(progress>=0 && progress<=100) this.progress.setValue(progress);
        
    }
    public static void main(final String[] args) {
        //for(String arg:args) System.out.println(arg);
        Splash splash = null;
        final Displayer displayer = new Displayer();
        UiUtils.invokeInEDT(displayer,true);
        splash = displayer.getSplash();
        Starter starter = new Starter(args,splash);
        starter.run();
    }
    private static final class Displayer extends Thread {
        private Splash splash = null;
        @Override
        public void run() {
            splash = new Splash();
            splash.setVisible(true);
        }
        public Splash getSplash() { return splash; }
    }
    private static final class Starter implements Runnable {
        Splash sl;
        String[] args = null;
        public Starter(String[] args,Splash sl) {
            super();
            this.args = args;
            this.sl=sl;
        }
        
        public void run(){
            try {
                Thread.sleep(100);
            } catch(Throwable t) {}
            if(sl!=null) sl.pushProgress();
//		  for(String arg:args) System.out.println(arg);
            final Thread runner = new Thread() {
                @Override
                public void run() {
                    new MainWindow(Constants.NOM_APP,sl,args);
                }
            };
//		  if(SwingUtilities.isEventDispatchThread()) {
//			  runner.run();
//		  } else {
//			  try {
//					SwingUtilities.invokeLater(runner);
//			  } catch(Throwable t) {};
//		  }
            UiUtils.invokeInEDT(runner,true);
            try {
                Thread.sleep(1000);
            } catch(Throwable t) {}
            if(sl!=null) {
                sl.setVisible(false);
                sl.dispose();
            }
        }
    }
}
