package com.haskforce;

import com.intellij.ide.util.projectWizard.JavaModuleBuilder;
import com.intellij.ide.util.projectWizard.ModuleBuilderListener;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.ide.util.projectWizard.SettingsStep;
import com.intellij.ide.util.projectWizard.SourcePathsBuilder;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.projectRoots.SdkTypeId;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class HaskellModuleBuilder extends JavaModuleBuilder implements SourcePathsBuilder, ModuleBuilderListener {
    @Override
    public void setupRootModel(ModifiableRootModel rootModel) throws ConfigurationException {
        addListener(this);
        super.setupRootModel(rootModel);
    }

    /**
     * Returns the Haskell module type.
     */
    @Override
    public ModuleType getModuleType() {
        return HaskellModuleType.getInstance();
    }

    /**
     * Ensures that SDK type is a Haskell SDK.
     */
    @Override
    public boolean isSuitableSdkType(SdkTypeId sdkType) {
        return sdkType == HaskellSdkType.getInstance();
    }

    /**
     * Called after module is created.
     */
    @Override
    public void moduleCreated(@NotNull Module module) {
        // TODO
    }

    /**
     * Hook into the new project creation and set dist to the compiler output
     * directory.
     */
    @Override
    @Nullable
    public ModuleWizardStep modifySettingsStep(@NotNull SettingsStep settingsStep) {
        if (settingsStep.getContext().isCreatingNewProject() &&
                settingsStep.getContext().isProjectFileDirectorySet()) {
            WizardContext c = settingsStep.getContext();
            String path = c.getProjectFileDirectory();
            String out = StringUtil.endsWithChar(path, '/') ? path + "dist" : path + "/dist";
            c.setCompilerOutputDirectory(out);
        }
        return super.modifySettingsStep(settingsStep);
    }
}

@@ -384,16 +384,71 @@
 ontext;%0A
+import com.intellij.openapi.fileTypes.FileTypeManager;%0A
 import c
@@ -1633,16 +1633,803 @@
  // TODO
+ - We should probably do some project initialization here as well...%0A%0A        // Update the ignored files and folders to avoid file search showing compiled files.%0A        FileTypeManager fileTypeManager = FileTypeManager.getInstance();%0A        StringBuilder builder = new StringBuilder(fileTypeManager.getIgnoredFilesList());%0A        // Ensure the ignored file list ends with a semicolon.%0A        if (builder.charAt(builder.length() - 1) != ';') %7B%0A            builder.append(';');%0A        %7D%0A        for (String ignore : new String%5B%5D%7B%22*.dyn_hi%22, %22*.dyn_hi%22, %22*.dyn_o%22, %22*.hi%22, %22*.o%22%7D) %7B%0A            if (builder.indexOf(';' + ignore + ';') == -1) %7B%0A                builder.append(ignore).append(';');%0A            %7D%0A        %7D%0A        fileTypeManager.setIgnoredFilesList(builder.toString());
 %0A    %7D%0A%0A

Update ignored files on module creation. closes #57