/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usda.weru.weps.startup;

import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import org.apache.log4j.Logger;
import org.netbeans.spi.wizard.Summary;
import org.netbeans.spi.wizard.Wizard;
import org.netbeans.spi.wizard.WizardException;
import org.netbeans.spi.wizard.WizardPage;
import usda.weru.util.About;
import usda.weru.util.ConfigData;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class WelcomeWizard implements WizardPage.WizardResultProducer {

    private static final Logger LOGGER = Logger.getLogger(WelcomeWizard.class);
    private Wizard c_wizard;
    private Map<String, String> c_changes;
    private final boolean c_cancelable;
    private boolean c_done;

    //only needs to be called once, sets the custom image in the UI manager's config
    static {
        try {
            URL url = WelcomeWizard.class.getResource("/usda/weru/resources/wizard.png");
            BufferedImage img = ImageIO.read(url);
            UIManager.put("wizard.sidebar.image", img);
        } catch (IOException e) {
            LOGGER.warn("Unable to customize wizard with wind erosion image.", e);
        }
    }

    /**
     *
     */
    public WelcomeWizard() {
        this(false);
    }

    /**
     *
     * @param cancelable
     */
    public WelcomeWizard(boolean cancelable) {
        //create the wizard
        c_cancelable = cancelable;
        c_changes = new HashMap<String, String>();

        c_wizard = WizardPage.createWizard("WEPS Welcome Wizard", createPages(), this);
    }

    private WizardPage[] createPages() {
        return new WizardPage[]{
            new IntroductionPage(this),
            new UserPage(this)};
    }

    /**
     *
     */
    public void show() {
/**
                 * Note:  Assertions are not enabled.  These will be useless items
                 * unless assertions are enabled.  Thus, they will be commented out unless
                 * the user wishes to enable specific assertions (feed the virtual machine 
                 * the -ea argument).
                 */
//        assert EventQueue.isDispatchThread() : "Wizard must be shown with the Event Dispatch Thread.";

        c_wizard.show();
    }

    /**
     *
     * @return
     */
    public ConfigData getConfigData() {
        return ConfigData.getDefault();
    }

    /**
     *
     * @return
     */
    public Map<String, String> getChanges() {
        return c_changes;
    }

    /**
     *
     * @param parameter
     * @return
     */
    public String getValue(String parameter) {
        String value = getChanges().get(parameter);
        if (value == null) {
            value = getConfigData().getData(parameter);
        }
        return value;
    }

    /**
     *
     * @param parameter
     * @param value
     */
    public void setValue(String parameter, String value) {
        getChanges().put(parameter, value);
    }

    //finish and cancel handle when the wizard's work is done
    /**
     *
     * @param wizardData
     * @return
     * @throws WizardException
     */
    @Override
    @SuppressWarnings("rawtypes")
    public Object finish(Map wizardData) throws WizardException {
        //save the user's settings
        for (Map.Entry<String, String> entry : getChanges().entrySet()) {
            getConfigData().setData(entry.getKey(), entry.getValue());
        }

        getConfigData().save();
        //return a wizard summary
        return Summary.create("User settings saved to " + About.getUserConfig().getAbsolutePath(), null);
    }

    /**
     *
     * @param settings
     * @return
     */
    @Override
    @SuppressWarnings("rawtypes")
    public boolean cancel(Map settings) {
        if (!c_cancelable) {
            JOptionPane.showMessageDialog(null, "Please complete the welcome wizard.", "Welcome Wizard", JOptionPane.INFORMATION_MESSAGE);
        }
        return c_cancelable;
    }

}
