/*
 * Created on 2005-05-25
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2005
 */
package pl.zalewski.mikolaj.symbdev.wizards;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.templates.ISubstitutionOptionsWizardPage;
import pl.zalewski.mikolaj.symbdev.templates.InputStreamFromReader;
import pl.zalewski.mikolaj.symbdev.templates.SubstitutionStreamReader;


/**
 * A wizard which asks for the file location, like the new file wizard
 * and then for some more data needed by the templates
 * 
 * @author mikolaj
 */
public class NewTemplatedFileWizard extends BasicNewResourceWizard {

    public NewTemplatedFileWizard(InputStream template) {
        super();
        fTemplate=template;
    }
    
    public void setDescrption(String desc) {
        fDescription=desc;
    }
    
    public void setWizardContentProvider(ISubstitutionOptionsWizardPage provider) {
        fProvider=provider;
    }
    
    public void addPages() {
        super.addPages();
    	fMainPage = new WizardNewFileCreationPage("newFilePage1",  getSelection());
    	fMainPage.setTitle("Files");
    	fMainPage.setDescription(fDescription);
    	addPage(fMainPage);

        if (fProvider!=null) {
            TemplateOptionsPage page=new TemplateOptionsPage("Template options");
            page.setWizardContentProvider(fProvider);
            addPage(page);            
        }
    }
    
    /* (non-Javadoc)
     * Method declared on IWizard.
     */
    public boolean performFinish() {
    	fResultFile = fMainPage.createNewFile();
    	if (fResultFile == null)
    		return false;
    	
    	HashMap substMap=new HashMap();
    	if (fProvider!=null)
    	    fProvider.contributeToTranslationMap(substMap);
    	
        InputStreamReader templateReader=new InputStreamReader(fTemplate,
                Charset.forName("UTF-8"));
        BufferedReader templateBreader=new BufferedReader(templateReader);
        SubstitutionStreamReader subst=new SubstitutionStreamReader(substMap, templateReader);
        try {
	        InputStreamFromReader translated=new InputStreamFromReader(subst, Charset.forName(fResultFile.getCharset()));
	        fResultFile.setContents(translated, true, false, null);
        }
        catch (CoreException e) {
            SymbdevPlugin.showError(e, "Error setting file content", getShell());
        }

    	return true;
    }
    
    
    public void dispose() {
        super.dispose();
        try {
	        fTemplate.close();
        }
        catch (IOException e) {
            SymbdevPlugin.getDefault().getLog().log(
                    SymbdevPlugin.createErrorStatus(
                            SymbdevPlugin.ERROR_IO_ACCESS,
                            "Error closing template input stream", e));
        }
    }
    public IFile getResultFile() {
        return fResultFile;
    }
    
    IFile fResultFile;

    String fDescription="Create a new file";
    InputStream fTemplate;
    ISubstitutionOptionsWizardPage fProvider;
	protected WizardNewFileCreationPage fMainPage;    
}
