/*
 * Copyright 2017, Reinhard Herzog (Fraunhofer IOSB) 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 nato.ivct.gui.client.sut;

import org.eclipse.scout.rt.client.context.ClientRunContexts;
import org.eclipse.scout.rt.client.job.ModelJobs;
import org.eclipse.scout.rt.client.ui.desktop.outline.IOutline;
import org.eclipse.scout.rt.platform.util.concurrent.IRunnable;
import org.eclipse.scout.rt.shared.notification.INotificationHandler;
import org.slf4j.LoggerFactory;

import nato.ivct.gui.client.Desktop;
import nato.ivct.gui.client.outlines.SuTOutline;
import nato.ivct.gui.shared.sut.TcStatusNotification;


public class TcStatusNotificationHandler implements INotificationHandler<TcStatusNotification> {

    org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public void handleNotification(TcStatusNotification notification) {
        // inform client about test case progress
        ModelJobs.schedule(new IRunnable() {
            @Override
            public void run() throws Exception {
                logger.trace("Test Case Status Notification " + notification.getTcId() + " is at " + notification.getPercent() + "%");
                for (final IOutline outline: Desktop.CURRENT.get().getAvailableOutlines()) {
                    if (outline instanceof SuTOutline) {
                        Desktop.CURRENT.get().findForms(SuTTcExecutionForm.class).forEach(form -> {
                            if (form.getSutId().equalsIgnoreCase(notification.getSutId()) && form.getTestCaseId().equalsIgnoreCase(notification.getTcId())) {
                                //                                // set TC execution status in detail form
                                //                                final ITable tbl = form.getTestCaseExecutionStatusTableField().getTable();
                                //                                tbl.discardAllRows();
                                //                                final ITableRow row = tbl.addRow();
                                //                                // set verdict
                                //                                ((Table) tbl).getTcStatusColumn().setValue(row, notification.getStatus());
                                //
                                //                                // set execution progress
                                //                                ((Table) tbl).getProgressColumn().setValue(row, notification.getPercent());
                                //
                                //                                //record status and progress in the form
                                //                                form.setTestCaseStatus(notification.getStatus());
                                //                                form.setTestCaseProgress(Integer.toString(notification.getPercent()));
                                //
                                //                                // set TC execution status in table
                                //                                final SuTTcNodePage tcNP = (SuTTcNodePage) outline.findNode(form.getBadgeId() + "." + form.getTestsuiteId());
                                //                                final SuTCbTablePage cbNode = (SuTCbTablePage) tcNP.getParentNode();
                                //                                for (final ITableRow tr: cbNode.getTable().getRows()) {
                                //                                    // find row with test case name
                                //                                    if (cbNode.getTable().getAbstractTCColumn().getValue(tr).equals(notification.getTcId())) {
                                //                                        cbNode.getTable().getTCresultColumn().setValue(tr, notification.getStatus() + ": " + notification.getPercent() + "%");
                                //                                    }
                                //                                }
                            }
                        });
                    }
                }
            }
        }, ModelJobs.newInput(ClientRunContexts.copyCurrent()));
    }
}

@@ -2154,42 +2154,8 @@
   //
-                                //
  set
@@ -2175,16 +2175,26 @@
  status 
+attribute 
 in detai
@@ -2236,68 +2236,15 @@
     
- //                                final ITable tbl =
  form.
-g
+s
 etTe
@@ -2253,535 +2253,43 @@
 Case
-ExecutionStatusTableField().getTable();%0D%0A                                //                                tbl.discardAllRows();%0D%0A                                //                                final ITableRow row = tbl.addRow();%0D%0A                                //                                // set verdict%0D%0A                                //                                ((Table) tbl).getTcStatusColumn().setValue(row, notification.getStatus());%0D%0A                                //%0D%0A                                //
+Status(notification.getStatus());%0D%0A
     
@@ -2323,16 +2323,19 @@
  // set 
+TC 
 executio
@@ -2340,339 +2340,37 @@
 ion 
-progress%0D%0A                                //                                ((Table) tbl).getProgressColumn().setValue(row, notification.getPercent());%0D%0A                                //%0D%0A                                //                                //record status and progress in the form%0D%0A                                //
+status field in detail form%0D%0A
     
@@ -2406,226 +2406,68 @@
 orm.
-setTestCaseStatus(notification.getStatus());%0D%0A                                //                                form.setTestCaseProgress(Integer.toString(notification.getPercent()));%0D%0A                                //
+getTcExecutionStatus().setValue(notification.getStatus());%0D%0A
 %0D%0A  
@@ -2502,50 +2502,13 @@
   //
-                                //
  set 
-TC 
 exec
@@ -2517,1060 +2517,159 @@
 ion 
-status in table%0D%0A                                //                                final SuTTcNodePage tcNP = (SuTTcNodePage) outline.findNode(form.getBadgeId() + %22.%22 + form.getTestsuiteId());%0D%0A                                //                                final SuTCbTablePage cbNode = (SuTCbTablePage) tcNP.getParentNode();%0D%0A                                //                                for (final ITableRow tr: cbNode.getTable().getRows()) %7B%0D%0A                                //                                    // find row with test case name%0D%0A                                //                                    if (cbNode.getTable().getAbstractTCColumn().getValue(tr).equals(notification.getTcId())) %7B%0D%0A                                //                                        cbNode.getTable().getTCresultColumn().setValue(tr, notification.getStatus() + %22: %22 + notification.getPercent() + %22%25%22);%0D%0A                                //                                    %7D%0D%0A                                //                                %7D
+progress%0D%0A                                //                                ((Table) tbl).getProgressColumn().setValue(row, notification.getPercent());
 %0D%0A  

Set TC execution status in form