/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2009 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.editor.wizard.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.pietschy.wizard.InvalidStateException;
import org.pietschy.wizard.PanelWizardStep;

import com.approximatrix.draco.editor.wizard.PluginCreatorController;
import com.approximatrix.draco.editor.wizard.PluginWizardData;
import com.approximatrix.draco.general.ui.GeneralWorker;
import com.approximatrix.draco.main.ui.MainWindow;

public class PluginFinalizeStep extends PanelWizardStep {

	private JTextField visibleName = null;
	private JTextField className = null;
	private JTextField moduleName = null;
	private PluginWizardData pwd = null;
	private MainWindow mw = null;
	
	public PluginFinalizeStep(PluginWizardData pwd, MainWindow mw) {
		super("Finalize Plugin", "Enter some final information and Draco will generate a plugin skeleton for editing.");
		this.mw = mw;
		this.pwd = pwd;
		
		this.setLayout(new BorderLayout());
		
		JPanel holding = new JPanel();
		holding.setLayout(new GridLayout(3,2));
		
		holding.add(this.wrapAdd(new JLabel("Visible Name:")));
		visibleName = new JTextField("New Plugin");
		visibleName.setToolTipText("The text that will appear in the menu...");
		
		Dimension tbs = visibleName.getPreferredSize();
		tbs.width = 175;
		visibleName.setPreferredSize(tbs);
		
		holding.add(this.wrapAdd(visibleName));
		
		holding.add(this.wrapAdd(new JLabel("Class Name:")));
		className = new JTextField("NewPlugin");
		className.setToolTipText("The name of the plugin's class (no spaces)...");
		className.setPreferredSize(tbs);
		holding.add(this.wrapAdd(className));
		
		holding.add(this.wrapAdd(new JLabel("Module Name (optional):")));
		moduleName = new JTextField("");
		moduleName.setToolTipText("The module containing this plugin class (optional)...");
		moduleName.setPreferredSize(tbs);
		holding.add(this.wrapAdd(moduleName));
		
		this.add(holding,BorderLayout.NORTH);
		
		this.setComplete(true);
	}
	
	private JPanel wrapAdd(JComponent value) {
		JPanel ret = new JPanel();
		FlowLayout fl = new FlowLayout();
		fl.setAlignment(FlowLayout.LEFT);
		ret.setLayout(fl);
		ret.add(value);		
		return ret;
	}

	@Override
	public void applyState() throws InvalidStateException {
		this.setBusy(true);
		
		pwd.setClassName(className.getText());
		pwd.setModuleName(moduleName.getText());
		pwd.setVisibleName(visibleName.getText());
		
		try {
			CreatePlugin cp = new CreatePlugin(pwd);
			
			new GeneralWorker(this.mw,
							  "Plugin Wizard",
							  "Generating Plugin - Standby...",
							  cp);
			
			pwd.setPluginText(cp.getPlugin());
		} finally {
			this.setBusy(false);
		}
	}
	
	private class CreatePlugin implements GeneralWorker.WorkRoutine {
		
		private PluginWizardData pwd = null;
		private String results = null;
		private PluginCreatorController pcc = null;
		
		public CreatePlugin(PluginWizardData data) {
			pwd = data;
		}

		public void doWork() {
			this.pcc = new PluginCreatorController(this.pwd);
			this.pcc.runCreator();
			results = this.pcc.getPlugin();
		}
		
		public String getPlugin() {
			return results;
		}
	}
	
}
