package com.redhat.ceylon.eclipse.imp.wizard;

import static org.eclipse.ui.ide.undo.WorkspaceUndoUtil.getUIInfoAdapter;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.undo.CreateFileOperation;

class FileCreationOp implements IRunnableWithProgress {

    private final IPackageFragmentRoot sourceDir;
    private final IPackageFragment packageFragment;
    private final String unitName;
    private final boolean includePreamble;
    private final String contents;
    private final Shell shell;
    
    private IFile result;
    
    IFile getResult() {
        return result;
    }
    
    FileCreationOp(IPackageFragmentRoot sourceDir,
            IPackageFragment packageFragment, String unitName,
            boolean includePreamble, String contents, Shell shell) {
        this.sourceDir = sourceDir;
        this.packageFragment = packageFragment;
        this.unitName = unitName;
        this.includePreamble = includePreamble;
        this.contents = contents;
        this.shell = shell;
    }
    
    public void run(IProgressMonitor monitor) {
        IPath path = packageFragment.getPath().append(unitName + ".ceylon");
        IProject project = sourceDir.getJavaProject().getProject();
        result = project.getFile(path.makeRelativeTo(project.getFullPath()));
        if (!result.exists()) {
            InputStream stream = new ByteArrayInputStream(contents.getBytes());
            CreateFileOperation op = new CreateFileOperation(result, null, 
                    getHeader(project), "Create Ceylon Unit");
            try {
                PlatformUI.getWorkbench().getOperationSupport().getOperationHistory()
                    .execute(op, monitor, getUIInfoAdapter(shell));
                result.appendContents(stream, false, false, monitor);
            } 
            catch (ExecutionException e) {
                e.printStackTrace();
            }
            catch (CoreException e) {
                e.printStackTrace();
            }
        }
    }
    
    private InputStream getHeader(IProject project) {
        IFile header = project.getFile("header.ceylon");
        InputStream his = new ByteArrayInputStream(new byte[0]);
        if (includePreamble && header.exists() && 
                header.isAccessible()) {
            try {
                his = header.getContents();
            }
            catch (CoreException e) {
                e.printStackTrace();
            }
            
        }
        return his;
    }
}
@@ -1828,24 +1828,42 @@
 ));%0A        
+try %7B%0A            
 if (!result.
@@ -1882,84 +1882,8 @@
     
-        InputStream stream = new ByteArrayInputStream(contents.getBytes());%0A
     
@@ -1974,16 +1974,20 @@
         
+    
 getHeade
@@ -2025,38 +2025,46 @@
 %22);%0A            
+  
+  
 try %7B%0A
+    
                 
@@ -2221,77 +2221,15 @@
     
-result.appendContents(stream, false, false, monitor);%0A            %7D %0A
+%7D %0A    
     
@@ -2275,32 +2275,36 @@
                 
+    
 e.printStackTrac
@@ -2312,38 +2312,196 @@
 ();%0A            
+  
+  
 %7D%0A    
+        %7D        %0A            result.appendContents(new ByteArrayInputStream(contents.getBytes()), %0A                    false, false, monitor);%0A        %7D%0A
         catch (C
@@ -2522,36 +2522,32 @@
 ) %7B%0A            
-    
 e.printStackTrac
@@ -2547,38 +2547,24 @@
 ackTrace();%0A
-            %7D%0A
         %7D%0A  

append to existing unit