/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.general.ui;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import java.awt.BorderLayout;
import java.awt.Color;

import com.approximatrix.draco.application.ApplicationManager;
import com.approximatrix.draco.application.InitializeWorker;
import com.sun.java.SwingWorker;

public class SplashFrame extends JFrame {
	
	private static final String PATH_TO_SPLASH = "/com/approximatrix/draco/main/ui/images/draco.svg";
	private static final int SPLASH_WIDTH = 400;
	private static final int SPLASH_HEIGHT = 275;
	
	private static final String SPLASH_TITLE = ApplicationManager.APP_NAME+" Initializing...";
	
	private VectorGraphicsPanel image_panel = null;
	private JLabel version_label = null;
	private JLabel status_label = null;
	private JProgressBar pbar = null;
	
	public SplashFrame() {
		super();
		initialize();
	}
	
	private void initialize() {
		this.setSize(SPLASH_WIDTH, SPLASH_HEIGHT);
		this.setLayout(new BorderLayout());
		this.setBackground(Color.WHITE);
		this.add(getImage_panel(),BorderLayout.CENTER);
		
		this.setTitle(SPLASH_TITLE);
		
		JPanel south_panel = new JPanel();
		south_panel.setLayout(new BorderLayout());
		south_panel.setBackground(Color.WHITE);
		
		
		south_panel.add(getVersionLabel(),BorderLayout.CENTER);
		south_panel.add(getPBar(),BorderLayout.SOUTH);
		
		this.add(south_panel,BorderLayout.SOUTH);
		this.setUndecorated(true);
		this.setResizable(true);
		this.setAlwaysOnTop(true);
	}
	
	private VectorGraphicsPanel getImage_panel() {
		if(image_panel == null) {
			image_panel = new VectorGraphicsPanel(PATH_TO_SPLASH);
			image_panel.setBackground(Color.WHITE);
		}
		return image_panel;
	}
	
	private JLabel getVersionLabel() {
		if(version_label == null) {
			version_label = new JLabel();
			version_label.setText(ApplicationManager.APP_NAME+"    "+ApplicationManager.APP_VERSION);
			version_label.setHorizontalAlignment(SwingConstants.CENTER);
			version_label.setBackground(Color.WHITE);
		}
		return version_label;
	}

	private JProgressBar getPBar() {
		if(pbar == null) {
			pbar = new JProgressBar();
			pbar.setMinimum(0);
			pbar.setMaximum(InitializeWorker.GetTotalSteps()-1);
			pbar.setStringPainted(true);
			pbar.setString("Initializing...");
			pbar.setBackground(Color.WHITE);
		}
		return pbar;
	}
	
	private void setStatus(final String text, final int step) {
		Runnable doSetText = new Runnable() {
            public void run() {
            	SplashFrame.this.getPBar().setValue(step);
            	SplashFrame.this.getPBar().setString(text);
            }
        };
        SwingUtilities.invokeLater(doSetText);
	}
	
	public void startup() {
		SwingWorker workerthread = new SwingWorker() {
			public Object construct() {
				return doStartupIterations();
			}
			public void finished() {
				Runnable doCloseSplash = new Runnable() {
		            public void run() {
		            	SplashFrame.this.dispose();
		            }
		        };
		        SwingUtilities.invokeLater(doCloseSplash);
			}
		};
		workerthread.start();
	}
	
	/** This routine represents the meat of the SwingWorker thread that performs
	 * all iteration control
	 * 
	 * @return for now, a String
	 */
	private Object doStartupIterations() {
		InitializeWorker iwork = new InitializeWorker();

		for(int i=0;i<iwork.getTotalSteps();i++) {
			SplashFrame.this.setStatus(iwork.getStepDescription(i),i);
			iwork.doStep(i);
		}
		
		return "All Done"; 
	}
}
