/*
** Module   : ReadString.java
** Abstract : directive to read text from a single row into a variable
**
** Copyright (c) 2009-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20090126 Directive to read text from a single row into a variable.
** 002 CA  20210827 Refactored to allow other test types, beside terminal (which can be executed from the same 
**                  test suite).  Added SOAP and REST web service tests.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.test;

import com.goldencode.harness.*;
import com.goldencode.harness.terminal.*;

/**
 * Directive to read text from a single row into a variable. There is no
 * waiting for text to appear. If there is any screen buffer available, the
 * text will be assigned into the specified variable (handling any conversions
 * from text to the proper data type). 
 */
public class ReadString
extends Report
implements TerminalTestable
{
   /** Short name for the activity. */
   private static final String NAME = "READ-STRING";
   
   /** The variable in which to store the text. */
   private final Variable var;
   
   /** The row from which to read text. */
   private final int row;
   
   /** The column at which to start copying (inclusive). */
   private final int col;
   
   /** The number of characters to read from the screen. */
   private final int width;
   
   /**
    * Constructor.
    *
    * @param    var
    *           The variable in which to store the text. Must not be
    *           <code>null</code>.
    * @param    row
    *           The row variable from which to read text.
    * @param    col
    *           The column at which to start copying (inclusive).
    * @param    width
    *           The number of characters to read from the screen.
    */
   public ReadString(Variable var, int row, int col, int width)
   {
      super(NAME, "");
      this.var   = var;
      this.row   = row;
      this.col   = col;
      this.width = width;
   }
   
   /**
    * Execute the directive of the test using the given terminal as the data
    * sink and/or source for the operation.
    *
    * @param    term
    *           The terminal that represents the system under test.
    */
   @Override
   public void execute(Terminal term)
   {
      char[][] screen = term.getScreen(false, 0);
      
      if (screen == null)
      {
         setStatus(Status.FAILED, "No screen buffer is available.");
         return;
      }
      
      if (row < 0 || row >= screen.length)
      {
         setStatus(Status.FAILED, String.format("Invalid row %d.", row));
         return;
      }
         
      if (col < 0 || col >= screen[row].length)
      {
         setStatus(Status.FAILED, String.format("Invalid col %d.", col));
         return;
      }
      
      StringBuilder sb = new StringBuilder();
      
      // don't overrun the screen buffer
      int len = Math.min((col + width), screen[row].length); 
      
      // read the data into our buffer
      for (int i = col; i < len; i++)
      {
         sb.append(screen[row][i]);
      }
      
      String text = sb.toString();
      
      setDescription(String.format("String read from row %d, column %d, " +
                                   "width %d into variable '%s'. Text = '%s'.",
                                   row,
                                   col,
                                   width,
                                   var.getName(),
                                   text));
      
      // assign the data into the var
      var.fromString(text);
      
      setStatus(Status.PASSED, "");
   }
}
