/*
 * 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.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(Exception     e){
            LOGGER.warn("Unable to customize wizard with wind erosion image.", e);
        }
    }

    public WelcomeWizard(){
        this(false);
    }

    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(){
        assert EventQueue.isDispatchThread() : "Wizard must be shown with the Event Dispatch Thread.";
        
        c_wizard.show();
    }

    public ConfigData getConfigData(){
        return ConfigData.getDefault();
    }

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

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

    public void setValue(String parameter, String value){
        getChanges().put(parameter, value);
    }

    //finish and cancel handle when the wizard's work is done
    @Override
    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);
    }

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


}
