/**
 * @file Image.java
 * 
 * @author Christos Choutouridis AEM:8997
 * @email  cchoutou@ece.auth.gr
 */
package net.hoo2.auth.vmodem;

/** @name imports */
/** @{ */
import java.io.*;
/** @} */

/**
 * @class Image
 * 
 * Class to used for the error free and non error free image requests
 */
class Image {
   static final int     IMAGE_BUFFER_SIZE    = 256*1024;    /** image buffer size */
   static final byte[]  IMAGE_BEGIN          = {(byte)0xFF, (byte)0xD8};   /** jpeg image begin pattern */
   static final byte[]  IMAGE_END            = {(byte)0xFF, (byte)0xD9};   /** jpeg image end pattern */

   private Com          com_;             /** Reference to communication module */
   private Log          log_;             /** Reference to logging module */
   private Transaction  transaction_;     /** A transaction object to used as a buffer for all requests */
   private String       filename_;        /** The filename to store */
   private int          items_;           /** how many images to fetch */
   private byte[]       code_;            /** The image request code for the virtual lab */

   /**
    * Basic constructor
    * @param com        The com module to use
    * @param log        The log module to use
    * @param code       The request code
    * @param filename   The filename
    * @param items      How many items to fetch
    */
   Image (Com com, Log log, byte[] code, String filename, int items) {
      com_        = com;
      log_        = log;
      // Allocate memory for the response
      transaction_= new Transaction(null, new byte[IMAGE_BUFFER_SIZE]);
      filename_   = filename;
      items_      = items;
      code_       = code;
   }

   /** Get function for the code */
   void code (byte[] code) { code_ = code; }

   /**
    * Functionality to drain the response buffer for the welcome message
    * from the server
    * @param code    The code that we will use (for printing, not sending)
    */
   void caption (byte[] code) {
      String line;

      line = "Running video decoder with: " + new String(code);
      log_.write(line, true);

      transaction_ = com_.request (transaction_, null, null, null, false);
      line = new String(transaction_.getResponse());
      log_.out(line);
   }

   /**
    * Main transaction loop. It send requests to server, get the response
    * and log the procedure while doing it.
    */
   void run () {
      String file, line;
      BufferedOutputStream ostream;

      for (int i =1 ; i<= items_ ; ++i) {
         // Make the filename string
         if (items_>1)
            file = filename_ + "_" + i + ".jpg";
         else
            file = filename_ + ".jpg";
         line = new String(code_) + ": ";
         log_.write(line);
         transaction_ = com_.request(transaction_, code_, IMAGE_BEGIN, IMAGE_END, true);
         line = "File= " + file
              + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]"
              + " Tt= " + (System.currentTimeMillis() - transaction_.departure) + " [msec]";
         log_.write(line);
         try {
            ostream = new BufferedOutputStream(new FileOutputStream(file));
            ostream.write(transaction_.response);
            ostream.flush();
            ostream.close();
         }
         catch (Exception exp) {
            System.err.println ("Error creating " + file + exp.getMessage());
            return;
         }
      }
   }
}
