package org.kie.guvnor.jcr2vfsmigration.migrater.asset;

import java.util.HashMap;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.drools.guvnor.client.common.AssetFormats;
import org.drools.guvnor.client.rpc.Module;
import org.drools.guvnor.server.RepositoryAssetService;
import org.drools.repository.AssetItem;
import org.kie.commons.io.IOService;
import org.kie.commons.java.nio.base.options.CommentedOption;
import org.kie.commons.java.nio.file.NoSuchFileException;
import org.kie.guvnor.drltext.service.DRLTextEditorService;
import org.kie.guvnor.jcr2vfsmigration.migrater.PackageImportHelper;
import org.kie.guvnor.jcr2vfsmigration.migrater.util.MigrationPathManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.uberfire.backend.server.util.Paths;
import org.uberfire.backend.vfs.Path;

@ApplicationScoped
public class PlainTextAssetWithPackagePropertyMigrater {
    protected static final Logger logger = LoggerFactory.getLogger(PlainTextAssetWithPackagePropertyMigrater.class);

    @Inject
    protected RepositoryAssetService jcrRepositoryAssetService;

    @Inject
    private Paths paths;
    
    @Inject
    @Named("ioStrategy")
    private IOService ioService;
     
    @Inject
    protected MigrationPathManager migrationPathManager;
    
    @Inject
    DRLTextEditorService drlTextEditorServiceImpl;
    
    @Inject
    PackageImportHelper packageImportHelper;

    public void migrate(Module jcrModule, AssetItem jcrAssetItem) {        
        Path path = migrationPathManager.generatePathForAsset(jcrModule, jcrAssetItem);
        final org.kie.commons.java.nio.file.Path nioPath = paths.convert( path );

        Map<String, Object> attrs;
        try {
            attrs = ioService.readAttributes( nioPath );
        } catch ( final NoSuchFileException ex ) {
            attrs = new HashMap<String, Object>();
        }
        
        String content = jcrAssetItem.getContent();
        
        StringBuilder sb = new StringBuilder();
        if(AssetFormats.DRL.equals(jcrAssetItem.getFormat())) {
            sb.append("rule '" + jcrAssetItem.getName() + "'");     
            sb.append( "\n" );
        } else if (AssetFormats.FUNCTION.equals(jcrAssetItem.getFormat())) {
            sb.append("function '" + jcrAssetItem.getName() + "'"); 
            sb.append( "\n" );
        }
        String sourceWithImport = drlTextEditorServiceImpl.assertPackageName(content, path);
        sb.append(sourceWithImport);
        sb.append( "\n" );
        sb.append("end");
        sourceWithImport = packageImportHelper.assertPackageImportDRL(sourceWithImport, path);

        ioService.write( nioPath, sourceWithImport, attrs, new CommentedOption(jcrAssetItem.getLastContributor(), null, jcrAssetItem.getCheckinComment(), jcrAssetItem.getLastModified().getTime() ));
    }

 }

@@ -2418,32 +2418,41 @@
 n%22 );%0A        %7D%0A
+        %0A
         String s
@@ -2528,16 +2528,120 @@
  path);%0A
+        sourceWithImport = packageImportHelper.assertPackageImportDRL(sourceWithImport, path);%0A        %0A
         
@@ -2718,13 +2718,14 @@
 end(
-%22end%22
+ %22%5Cn%22 
 );%0A 
@@ -2736,91 +2736,22 @@
    s
-ourceWithImport = packageImportHelper.assertPackageImportDRL(sourceWithImport, path
+b.append(%22end%22
 );%0A%0A
@@ -2785,31 +2785,28 @@
 oPath, s
-ourceWithImport
+b.toString()
 , attrs,

update migrator for drl and function