import com.install4j.api.*;

import java.io.*;
import java.util.Properties;

// This is an example of a typical install action that writes some data
// to a file. This action is registered as a post-install action since it operates
// on a file that is first installed by the installer. If you create a new file,
// make sure to delete it in the uninstall action.
public class HelloInstallAction extends InstallAction {

    // holds the information whether the installation was canceled or not.
    private boolean canceled;

    // Implementation of abstract method. This method is called when the action should be performed.
    // You can use the progress interface to tell the user how the installation is proceeding.
    // The InstallerWizardContext provides access to all important information and to installation-
    // related services.
    public boolean performAction(InstallerWizardContext context, ProgressInterface progressInterface) {

        progressInterface.setStatusMessage("Initializing the huge hello name database ...");

        String greetingName = "world";
        HelloScreen helloScreen = (HelloScreen)context.getCustomScreenOfClass(HelloScreen.class);
        if (helloScreen != null) {
            greetingName = helloScreen.getGreetingName();
        }

        try {
            writeGreetingName(greetingName, context);
        } catch (IOException ex) {
        }

        for (int i = 0; i < 10; i++) {
            if (canceled) {
                return true;
            }
            progressInterface.setDetailMessage("phase " + i);
            progressInterface.setPercentCompleted(i * 10);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        return true;
    }

    // Implementation of abstract method. This method is called when the action has been fully
    // or partially performed and the installation has been cancelled. You can undo any changes
    // in this method.
    public void rollback(InstallerWizardContext context, ProgressInterface progressInterface) {
        for (int i = 0; i < 5; i++) {
            progressInterface.setPercentCompleted(i * 20);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    }

    // Implementation of abstract method. This method is called if the user has pressed the
    // "Cancel" button while this action is being performed. If your implementation of
    // performAction might take longer than a few seconds, it is recommended to react to calls
    // to this method.
    public void cancel() {
        canceled = true;
    }

    // Implementation of abstract method. Here, you tell install4j how many percent of
    // the status bar you would like to have for your action. Note that you always use the
    // full range of 1-100 in the ProgressInterface, regardless of this setting.
    public int getPercentOfTotalInstallation() {
        return 40;
    }

    // Helper method to write information from HelloScreen to a file.
    private void writeGreetingName(String greetingName, InstallerContext context) throws IOException {

        File propertiesFile = new File(context.getInstallationDirectory(), "hello.properties");
        Properties properties = new Properties();
        FileInputStream inputStream = new FileInputStream(propertiesFile);
        properties.load(inputStream);
        inputStream.close();

        properties.setProperty("greetingName", greetingName);

        FileOutputStream outputStream = new FileOutputStream(propertiesFile);
        properties.store(outputStream, "Hello World Suite properties file");
        outputStream.close();
    }

}
