/*
 * 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.projecteditor.client.forms;

import javax.inject.Inject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import org.jboss.errai.bus.client.api.RemoteCallback;
import org.jboss.errai.ioc.client.api.Caller;
import org.kie.guvnor.commons.ui.client.callbacks.HasBusyIndicatorDefaultErrorCallback;
import org.kie.guvnor.commons.ui.client.widget.HasBusyIndicator;
import org.kie.guvnor.projecteditor.client.resources.i18n.ProjectEditorConstants;
import org.kie.guvnor.services.metadata.model.Metadata;
import org.kie.guvnor.testscenario.service.ScenarioTestEditorService;
import org.uberfire.backend.vfs.Path;
import org.uberfire.client.annotations.OnStart;
import org.uberfire.client.common.BusyPopup;
import org.uberfire.client.common.FormStylePopup;
import org.uberfire.client.mvp.Command;

public class BulkRunTestScenarioEditor
        implements IsWidget, BulkRunTestScenarioEditorView.Presenter, HasBusyIndicator {

    @Inject
    private BulkRunTestScenarioEditorView view;
    private Path path;

    @Inject
    private Caller<ScenarioTestEditorService> scenarioService;

    //@Inject
    public void init( /*BulkRunTestScenarioEditorView view, Caller<ScenarioTestEditorService> scenarioService, */final Path path,
                      final boolean isReadOnly ) {
/*        this.view = view;
        this.scenarioService = scenarioService;*/
        this.path = path;
        if ( isReadOnly ) {
            view.setReadOnly();
        }
        
        view.setPresenter(this);
    }

    private void setTitle( final String titleText ) {
        if ( titleText == null || titleText.isEmpty() ) {
            view.setTitleText( ProjectEditorConstants.INSTANCE.ProjectModel() );
        } else {
            view.setTitleText( titleText );
        }
    }

    public void save( final String commitMessage,
                      final Command callback,
                      final Metadata metadata ) {
    }

    @Override
    public Widget asWidget() {
        return view.asWidget();
    }

    public String getTitle() {
        return view.getTitleWidget();
    }

    @Override
    public void onRunAllButton() {
        final FormStylePopup pop = new FormStylePopup();
        final TextBox sessionNameTextBox = new TextBox();        
        pop.addAttribute("session name" + ":", sessionNameTextBox);

        Button ok = new Button("OK");
        ok.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if(sessionNameTextBox.getText() == null || "".equals(sessionNameTextBox.getText())) {
                    Window.alert("TestScenarioConstants.INSTANCE.PleaseInputSessionName()");
                    return;
                }
                
                BusyPopup.showMessage("TestScenarioConstants.INSTANCE.BuildingAndRunningScenario()");

                scenarioService.call(new RemoteCallback<Void>() {
                    @Override
                    public void callback(Void v) {
                        pop.hide();
                        BusyPopup.close();
                    }
                },
                        new HasBusyIndicatorDefaultErrorCallback(BulkRunTestScenarioEditor.this)
                ).runAllScenarios(path, sessionNameTextBox.getText());                        
            }
        });
        pop.addAttribute( "", ok);
        pop.show();                
    }

    @Override
    public void showBusyIndicator(String message) {
        BusyPopup.showMessage(message);
    }

    @Override
    public void hideBusyIndicator() {
        BusyPopup.close();
    }
}
@@ -641,16 +641,98 @@
 forms;%0A%0A
+import javax.annotation.PostConstruct;%0Aimport javax.enterprise.context.Dependent;%0A
 import j
@@ -751,16 +751,16 @@
 Inject;%0A
-
 %0Aimport 
@@ -1628,16 +1628,72 @@
 nStart;%0A
+import org.uberfire.client.annotations.WorkbenchEditor;%0A
 import o
@@ -1820,16 +1820,27 @@
 mmand;%0A%0A
+@Dependent%0A
 public c
@@ -2128,16 +2128,111 @@
     
-//@Injec
+@PostConstruct%0A    public void init() %7B%0A        view.setPresenter(this);        %0A    %7D%0A    %0A    @OnStar
 t%0A  
@@ -2570,24 +2570,24 @@
 ReadOnly();%0A
+
         %7D%0A  
@@ -2588,50 +2588,8 @@
   %7D%0A
-        %0A        view.setPresenter(this);%0A
     

fix BulkRunTestScenarioEditor