package gitflow;

import com.intellij.execution.ExecutionException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

import git4idea.commands.GitCommand;
import git4idea.commands.GitLineHandler;


public class GitInitLineHandler extends GitLineHandler {
    private BufferedWriter writer;
    GitflowInitOptions _initOptions;

    public GitInitLineHandler(GitflowInitOptions initOptions, @NotNull Project project, @NotNull VirtualFile vcsRoot, @NotNull GitCommand command) {
        super(project, vcsRoot, command);
        _initOptions = initOptions;
    }

    @Nullable
    @Override
    protected Process startProcess() throws ExecutionException {
        Process p = super.startProcess();
        writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        return p;
    }

    protected void processTerminated(final int exitCode) {
        super.processTerminated(exitCode);
    }


    @Override
    protected void onTextAvailable(String s, Key key) {
        super.onTextAvailable(s, key);
        try {
            if (s.contains("Branch name for production releases")) {
                writer.write(_initOptions.getProductionBranch());
                myVcs.showCommandLine(_initOptions.getProductionBranch());
                writer.newLine();
                writer.flush();
            }

            if (s.contains("Branch name for \"next release\"")) {

                writer.write(_initOptions.getDevelopmentBranch());
                myVcs.showCommandLine(_initOptions.getDevelopmentBranch());
                writer.newLine();
                writer.flush();
            }

            if (s.contains("Which branch should be used for integration of the")) {
                writer.newLine();
                writer.flush();
            }

            if (s.contains("Feature branches")) {
                writer.write(_initOptions.getFeaturePrefix());
                myVcs.showCommandLine(_initOptions.getFeaturePrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Release branches")) {
                writer.write(_initOptions.getReleasePrefix());
                myVcs.showCommandLine(_initOptions.getReleasePrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Hotfix branches")) {
                writer.write(_initOptions.getHotfixPrefix());
                myVcs.showCommandLine(_initOptions.getHotfixPrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Support branches")) {
                writer.write(_initOptions.getSupportPrefix());
                myVcs.showCommandLine(_initOptions.getSupportPrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Version tag")) {
                writer.write(_initOptions.getVersionPrefix());
                myVcs.showCommandLine(_initOptions.getVersionPrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Hooks and filters")) {
                writer.newLine();
                writer.flush();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

@@ -1310,39 +1310,32 @@
 if (s.contains(%22
-Branch 
 name for product
@@ -1606,15 +1606,8 @@
 ns(%22
-Branch 
 name
@@ -1633,17 +1633,16 @@
 %5C%22%22)) %7B%0A
-%0A
         
@@ -1861,172 +1861,8 @@
  %7D%0A%0A
-            if (s.contains(%22Which branch should be used for integration of the%22)) %7B%0A                writer.newLine();%0A                writer.flush();%0A            %7D%0A
 %0A   
@@ -2115,32 +2115,294 @@
 ;%0A            %7D%0A
+            if (s.contains(%22Bugfix branches%22)) %7B%0A                writer.write(_initOptions.getBugfixPrefix());%0A                myVcs.showCommandLine(_initOptions.getBugfixPrefix());%0A                writer.newLine();%0A                writer.flush();%0A            %7D%0A
             if (

Fix git flow init bugs