/*
 * NIST
 * TestEngineMain.java Jun 20, 2008
 *
 * This code was produced by the National Institute of Standards and
 * Technology (NIST). See the "nist.disclaimer" file given in the distribution
 * for information on the use and redistribution of this software.
 */

package gov.nist.hl7.testengine.main;

import gov.nist.hl7.testengine.analyzeresults.AnalyzeResults;
import gov.nist.hl7.testengine.generation.MessageGeneration;
import gov.nist.hl7.testengine.testagent.TestAgentUtils;
import gov.nist.hl7.ws.testagent.TestAgentTestAgentSOAP12Port_httpStub;
import gov.nist.hl7.ws.testagent.TestAgentTestAgentSOAP12Port_httpStub.Send;
import gov.nist.hl7.ws.testagent.TestAgentTestAgentSOAP12Port_httpStub.Start;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import org.apache.axis2.client.Options;

/**
 * @author Roch Bertucat (NIST)
 *
 */
public final class TestEngineMain {

   //private static final String URL = "http://xreg2.nist.gov:8080/HL7WS/services/";
   //private static final String URL = "http://begonia.sdct.nist.gov:8080/HL7WS/services/";
   private static final String URL = "http://localhost:8080/NISTHl7WS/services/";
   private static final String XSLPATH = "files/XMLtoMessageReportHTML.xsl";

   private TestEngineMain() {
   }

   /**
    * @param args
    */
   public static void main(final String[] args) {
      try {

         // ----------------------
         // 1. Start the testAgent
         // ----------------------

         TestAgentTestAgentSOAP12Port_httpStub taStub = new TestAgentTestAgentSOAP12Port_httpStub(URL + "TestAgent");
         // Create the client instance
         // Set the timeout for 60 minutes (for large files)
         Options options = taStub._getServiceClient().getOptions();
         options.setTimeOutInMilliSeconds(600000000);

         TestAgentUtils taUtils = new TestAgentUtils();
         // i = 3 will start the manager first, i = 2 doesn't start it
         for (int i = 3; i >= 0; i--) {
            // get the appropriate test agent config based on its role
            int testAgentRole = i + 1;
            String testAgentConfigPath = taUtils.getConfigPath(testAgentRole);

            // get the oid
            String oid = taUtils.getOid(testAgentRole, testAgentConfigPath);

            // Start the test agent thru our WS with the conf
            Start swConf = new Start();

            swConf.setXmlConfig(Utils.getContents(new File(testAgentConfigPath)));
            taStub.start(swConf);
            // -------------------
            // 2. Generate message
            // -------------------
            String message = "";
            if (i == 3) {
               message = taUtils.generateMessage(taStub, testAgentRole, oid, taUtils.getMessageCriteria(testAgentRole));
            } else {
               MessageGeneration mg = new MessageGeneration(URL);
               message = mg.generate(oid, taUtils.getMessageCriteria(testAgentRole));
            }

            /* log what we're doing and send the message */
            if (!"".equals(message)) {
               taUtils.log(taUtils.getRole(testAgentRole));
               taUtils.log(": Sending: " + message);

               Send ss = new Send();
               ss.setMessage(message);
               saveStringToFile(message);
               // ---------------
               // 3. Send message
               // ---------------
               taStub.send(ss);
            }
         }

//       // Sleep for 15 sec to let Mirth to write in the DB
//       Thread.sleep(15000);

//       // 4. when the testAgents have ran, validate messages
//       // --------------------------------------------------
//       AnalyzeResults ar = new AnalyzeResults(URL, XSLPATH);
//       ar.validate();

//       // Terminate
         taStub.terminate();

      } catch (Exception e) {
         System.out.println(e);
      }
   }
   public static void saveStringToFile(String message){
      DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
      Date date = new Date();
      String filePath=dateFormat.format(date);
      File outputFile=new File(filePath+".hl7");

      PrintWriter out=null;
      try
      {
         out = new PrintWriter(new FileWriter(outputFile));
      }
      catch (IOException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      out.print(message);
      out.close();
   }

}



