/*
 * Copyright 2012 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.kie.guvnor.guided.rule.backend.server;

import java.util.Collection;
import java.util.Date;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.errai.bus.server.annotations.Service;
import org.kie.commons.io.IOService;
import org.kie.guvnor.commons.data.workingset.WorkingSetConfigData;
import org.kie.guvnor.commons.service.validation.model.BuilderResult;
import org.kie.guvnor.commons.service.verification.model.AnalysisReport;
import org.kie.guvnor.datamodel.oracle.DataModelOracle;
import org.kie.guvnor.datamodel.service.DataModelService;
import org.kie.guvnor.guided.rule.backend.server.util.BRDRLPersistence;
import org.kie.guvnor.guided.rule.backend.server.util.BRLPersistence;
import org.kie.guvnor.guided.rule.backend.server.util.BRXMLPersistence;
import org.kie.guvnor.guided.rule.model.GuidedEditorContent;
import org.kie.guvnor.guided.rule.model.RuleModel;
import org.kie.guvnor.guided.rule.service.GuidedRuleEditorService;
import org.kie.guvnor.project.service.ProjectService;
import org.uberfire.backend.server.util.Paths;
import org.uberfire.backend.vfs.Path;

@Service
@ApplicationScoped
public class GuidedRuleEditorServiceImpl
        implements GuidedRuleEditorService {

    @Inject
    @Named("ioStrategy")
    private IOService ioService;

    @Inject
    private Paths paths;

    @Inject
    private ProjectService projectService;

    @Inject
    private DataModelService dataModelService;

    @Override
    public GuidedEditorContent loadContent( final Path path ) {
        final RuleModel model = loadRuleModel( path );
        final DataModelOracle dataModel = dataModelService.getDataModel( path );
        return new GuidedEditorContent( dataModel, model );
    }

    @Override
    public RuleModel loadRuleModel( Path path ) {
        return BRXMLPersistence.getInstance().unmarshal( ioService.readAllString( paths.convert( path ) ) );
    }

    @Override
    public void save( final Path path,
                      final RuleModel model ) {
        final BRLPersistence p = BRXMLPersistence.getInstance();
        final String xml = p.marshal( model );
        ioService.write( paths.convert( path ),
                         xml );
    }

    @Override
    public void save( final Path path,
                      final RuleModel factModels,
                      final String comment,
                      final Date when,
                      final String lastContributor ) {
        //TODO:

    }

    @Override
    public String[] loadDropDownExpression( final String[] valuePairs,
                                            final String expression ) {
        return new String[ 0 ];
    }

    @Override
    public String toSource( final RuleModel model ) {
        return BRDRLPersistence.getInstance().marshal( model );
    }

    @Override
    public AnalysisReport verify( final Path path,
                                  final RuleModel content,
                                  final Collection<WorkingSetConfigData> activeWorkingSets ) {
        //TODO {porcelli} verify
        return new AnalysisReport();
    }

    @Override
    public BuilderResult validate( final Path path,
                                   final RuleModel content ) {
        //TODO {porcelli} validate
        return new BuilderResult();
    }

    @Override
    public boolean isValid( final Path path,
                            final RuleModel content ) {
        return !validate( path, content ).hasLines();
    }
}

@@ -693,16 +693,87 @@
 l.Date;%0A
+import java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A
 import j
@@ -815,16 +815,16 @@
 Scoped;%0A
-
 import j
@@ -1727,32 +1727,99 @@
 ProjectService;%0A
+import org.mvel2.MVEL;%0Aimport org.mvel2.templates.TemplateRuntime;%0A
 import org.uberf
@@ -3367,32 +3367,62 @@
                 
+        String expression ) %7B%0A
         final St
@@ -3419,33 +3419,664 @@
   final 
+Map%3C
 String
- expression
+, String%3E context = new HashMap%3CString, String%3E();%0A%0A        for ( final String valuePair : valuePairs ) %7B%0A            if ( valuePair == null ) %7B%0A                return new String%5B 0 %5D;%0A            %7D%0A            final String%5B%5D pair = valuePair.split( %22=%22 );%0A            context.put( pair%5B 0 %5D,%0A                         pair%5B 1 %5D );%0A        %7D%0A        // first interpolate the pairs%0A        expression = (String) TemplateRuntime.eval( expression,%0A                                                    context );%0A%0A        // now we can eval it for real...%0A        Object result = MVEL.eval( expression );%0A        if ( result instanceof String%5B%5D
  ) %7B%0A   
@@ -4080,39 +4080,400 @@
         
+  
+  
 return 
-new String%5B 0 %5D;
+(String%5B%5D) result;%0A        %7D else if ( result instanceof List ) %7B%0A            List l = (List) result;%0A            String%5B%5D xs = new String%5B l.size() %5D;%0A            for ( int i = 0; i %3C xs.length; i++ ) %7B%0A                Object el = l.get( i );%0A                xs%5B i %5D = el.toString();%0A            %7D%0A            return xs;%0A        %7D else %7B%0A            return null;%0A        %7D
 %0A    %7D%0A%0A

Add implmentation of GuidedRuleEditorService.loadDropDownExpression