/**
 * Copyright (c) 2008-2022, MOVES Institute, Naval Postgraduate School (NPS). All rights reserved.
 * This work is provided under a BSD open-source license, see project license.html and license.txt
 * @author brutzman@nps.edu
 */
package OpenDis7Examples;

import edu.nps.moves.dis7.entities.swe.platform.surface._001Poseidon;
import edu.nps.moves.dis7.entities.swe.platform.surface._002Triton;
import edu.nps.moves.dis7.enumerations.*;
import edu.nps.moves.dis7.pdus.*;
import edu.nps.moves.dis7.utilities.PduFactory;
import java.util.logging.Level;
import java.util.logging.Logger;

/** The purpose of this inheritable class is to provide an easily modifiable example simulation program
 *  that includes DIS-capable entities performing tasks of interest, and then reporting activity via PDUs
 *  to the network.
 *  Default program initialization includes PDU recording turned on by default.
 *  @see <a href="https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/blob/master/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt">ExampleSimulationProgramLog.txt</a>
 */
public class ExampleSimulationProgram // extends ChannelOpenDis7
{
    protected ChannelOpenDis7 channelOpenDis7;
    protected PduFactory      pduFactory;
    
    /** seconds for real-time execution (not simulation time, which may or may not be .the same) */
    double  currentTimeStep  =  1.0; // seconds
    /** initial simulation time */
    double  initialTime = 0.0;
    /** current simulation time */
    double  simulationTime;
    
    /** EntityID settings for entity 1 */
    protected EntityID           entityID_1          = new EntityID();
    /** EntityID settings for entity 2 */
    protected EntityID           entityID_2          = new EntityID();
    /** ESPDU for entity 1 */
    protected EntityStatePdu     entityStatePdu_1;
    /** ESPDU for entity 2 */
    protected EntityStatePdu     entityStatePdu_2;
    /** FirePdu for entity 1 first  weapon (if any) */
    protected FirePdu            firePdu_1a;
    /** FirePdu for entity 1 second weapon (if any) */
    protected FirePdu            firePdu_1b;
    /** MunitionDescriptor for these weapons */
    protected MunitionDescriptor munitionDescriptor1;
    
    /** this class instantiated as an object */
    SimulationManager        simulationManager = new SimulationManager();
    
    /** Initialize channel setup for OpenDis7 and report a test PDU */
    private void initializeChannelOpenDis7()
    {
        channelOpenDis7.setUpNetworkInterface();
        channelOpenDis7.printlnTRACE ("opendis7.getNetworkAddress()=" + channelOpenDis7.getNetworkAddress() +
                                             ", getNetworkPort()="    + channelOpenDis7.getNetworkPort());
        
//      channelOpenDis7.sendCommentPdu(VariableRecordType.OTHER, "ArrivalProcessOpenDis7 initialized");
    }
    
    /** Get ready, get set... initialize simulation entities
     */
    public void initializeSimulationEntities()
    {
        initializeChannelOpenDis7();
        
        pduFactory          = channelOpenDis7.getPduFactory();
        entityStatePdu_1    = pduFactory.makeEntityStatePdu();
        entityStatePdu_2    = pduFactory.makeEntityStatePdu();
        firePdu_1a          = pduFactory.makeFirePdu();
        firePdu_1b          = pduFactory.makeFirePdu();
        munitionDescriptor1 = new MunitionDescriptor();
        
        // Your model setup: define participants.  who's who in this zoo?
        // Assuming you keep track of entity objects...  here is some support for for Entity 1.
        
        // PDU objects are already created, now set their values.
        entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID;
        
        entityID_2.setSiteID(1).setApplicationID(2).setEntityID(4); // made-up example ID; 
        // TODO someday, use enumerations; is there a unique site triplet for MOVES Institute?

        entityStatePdu_1.setEntityID(entityID_1);
        entityStatePdu_1.setForceId(ForceID.FRIENDLY);
        entityStatePdu_1.setEntityType(new _001Poseidon()); // note import statement above
        entityStatePdu_1.setMarking("Entity #1");
        entityStatePdu_1.getMarkingString(); // check

        entityStatePdu_2.setEntityID(entityID_2);
        entityStatePdu_2.setForceId(ForceID.OPPOSING);
        entityStatePdu_2.setEntityType(new _002Triton()); // note import statement above
        entityStatePdu_2.setMarking("Entity #2");

        // TODO how should we customize this munition?  what is it for your simulation?
        munitionDescriptor1.setQuantity(1);
        firePdu_1a.setDescriptor(munitionDescriptor1).setRange(1000.0f);
        
        // TODO simulation management PDUs for startup, planning to design special class support 
//        simulationManager.addEntity();
        simulationManager.setDescriptor("ExampleSimulationProgram");
        simulationManager.addHost(ChannelOpenDis7.getThisHostName());
        simulationManager.setDisThreadedNetworkInterface(channelOpenDis7.getDisNetworkInterface());
    }
                 
    /**
     * This runSimulationLoops() method is for you, a customizable programmer-modifiable
     * code block for defining and running a new simulation of interest.
     * 
     * Welcome! Other parts of this program handle bookkeeping and plumbing tasks so that
     * you can focus on your model entities and activities.
     * Expandable support includes DIS EntityStatePdu, FirePdu and CommentPdu all available for 
     * modification and sending in a simulation loop.
     * Continuous improvement efforts seek to make this program as easy and straightforward
     * as possible for DIS simulationists to use and adapt.
     * All of the other methods are setup, teardown and configuration that you may find
     * interesting, even helpful, but don't really have to worry about.
     */
    @SuppressWarnings("SleepWhileInLoop") // yes we do that
    public void runSimulationLoops ()
    {
      try
      {              
        final int     SIMULATION_MAX_LOOP_COUNT = 10; // be deliberate out there!  also avoid infinite loops.
              int     simulationLoopCount = 0;        // variable, initialized at 0
              boolean simulationComplete = false;     // sentinel variable as termination condition, are we done yet?
        
        // TODO reset Clock Time to today's date and timestamp to zero, providing consistent outputs for each simulation run

        channelOpenDis7.getPduRecorder().setVerbose(true);
        
        initializeSimulationEntities();
        
        simulationManager.simulationJoin();
        simulationManager.simulationStart();
        
        // ===================================================================================================
        // loop the simulation while allowed, programmer can set additional conditions to break out and finish
        while (simulationLoopCount < SIMULATION_MAX_LOOP_COUNT)  // are we done yet?
        {
            simulationLoopCount++; // good practice: increment loop counter as first action in that loop
            
            // =============================================================================================
            // * your own simulation code starts here! *
            // =============================================================================================
            
            //  are there any other variables to modify at the beginning of your loop?
            
            // compute a track, update an ESPDU, whatever it is that your model is doing...
            
            // Where is my entity?  Insert changes in position; this sample only changes X position.
            entityStatePdu_1.getEntityLocation().setX(entityStatePdu_1.getEntityLocation().getX() + 1.0); // 1m per timestep
            
            // decide whether to fire, and then update the firePdu.  Hmmm, you might want a target to shoot at!
            
            // etc. etc. your code goes here for your simulation of interest
                
            // something happens between my simulation entities, la de da de da...
            System.out.println ("... My simulation just did something, no really...");
            System.out.flush();
            
            
            // make your reports: narrative code for CommentPdu here (set all to empty strings to avoid sending)
            narrativeMessage1 = "MV3500 ExampleSimulationProgram";
            narrativeMessage2 = "runSimulation() loop " + simulationLoopCount;
            narrativeMessage3 = ""; // intentionally blank for testing

            // your loop termination condition goes here
            if (simulationLoopCount > 4) // for example
            {
                simulationComplete = true;
            }      
            // =============================================================================================
            // * your own simulation code is finished here! *
            // =============================================================================================
            
            // staying synchronized with timestep: wait duration for elapsed time in this loop
            // Thread.sleep needs a (long) parameter for milliseconds, which are clumsy to use sometimes
            Thread.sleep((long)(currentTimeStep * 1000)); // seconds * (1000 msec/sec) = milliseconds
            System.out.println ("... [Pausing for " + currentTimeStep + " seconds]");
            
            // OK now send the status PDUs for this loop, and then continue
            System.out.println ("sending PDUs for simulation step " + simulationLoopCount + ", monitor loopback to confirm sent");
            System.out.flush();
            sendAllPdusForLoopTimestep(entityStatePdu_1, firePdu_1a, currentTimeStepComment, narrativeMessage1, narrativeMessage2, narrativeMessage3);
            channelOpenDis7.sendSinglePdu(entityStatePdu_2); // me too i.e. 2!
            System.out.println ("... [PDUs successfully sent for this loop]");
            System.out.flush();
            
            // ===============================
            // current loop now finished, check whether to terminate if simulation complete, otherwise continue
            if (simulationComplete || (simulationLoopCount > 10000)) // for example; including fail-safe condition is good
            {
                System.out.println ("... [loop termination condition met, simulationComplete=" + simulationComplete + "]"); // ", final loopCount=" + loopCount + 
                System.out.flush();
                break;
            }
        }   // end of simulation loop
        // ===================================================================================================

        narrativeMessage2 = "runSimulation() completed successfully"; // all done
        channelOpenDis7.sendCommentPdu(narrativeComment, narrativeMessage1, narrativeMessage2, narrativeMessage3);
        System.out.println ("... [final CommentPdu successfully sent for simulation]");
        
        // TODO simulation management PDUs
        simulationManager.simulationStop();
        simulationManager.simulationLeave();
      } 
      catch (InterruptedException iex) // handle any exception that your code might choose to provoke!
      {
        Logger.getLogger(ExampleSimulationProgram.class.getName()).log(Level.SEVERE, null, iex);
      }
    }
    /* **************************** infrastructure code, modification is seldom needed ************************* */
                 
    String narrativeMessage1 = new String();
    String narrativeMessage2 = new String();
    String narrativeMessage3 = new String();
          
    /* VariableRecordType enumerations have potential use with CommentPdu logs */
    /* TODO contrast to EntityType */
    VariableRecordType     descriptionComment = VariableRecordType.DESCRIPTION;
    VariableRecordType       narrativeComment = VariableRecordType.COMPLETE_EVENT_REPORT;
    VariableRecordType          statusComment = VariableRecordType.APPLICATION_STATUS;
    VariableRecordType currentTimeStepComment = VariableRecordType.APPLICATION_TIMESTEP;
    
    /**
     * Constructor to create an instance of this class.
     * Design goal: additional built-in initialization conveniences can go here
     * to keep your efforts focused on the runSimulation() method.
     */
    public ExampleSimulationProgram()
    {
        // additional constructor initialization goes here
        
        if (channelOpenDis7 == null)
            channelOpenDis7 = new ChannelOpenDis7();
        pduFactory      = channelOpenDis7.getPduFactory();
    }
    /**
     * Utility Constructor that allows your example simulation program to override default network address and port
     * @param address network address to use
     * @param port corresponding network port to use
     */
    public ExampleSimulationProgram(String address, int port)
    {
        super();
        
        channelOpenDis7.setNetworkAddress(address);
        
        channelOpenDis7.setNetworkPort(port);
    }

    /**
     * Send EntityState, Fire, Comment PDUs that got updated for this loop, reflecting state of current simulation timestep.
     * @see <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html">Passing Information to a Method or a Constructor</a> Arbitrary Number of Arguments
     * @param entityStatePdu the ESPDU to send, if any
     * @param firePdu        the FirePDU to send, if any
     * @param commentType    enumeration value describing purpose of the narrative comment
     * @param comments       String array of narrative comments
     */
    public void sendAllPdusForLoopTimestep(EntityStatePdu entityStatePdu,
                                   FirePdu firePdu,
                        VariableRecordType commentType,
                              // vararg... variable-length set of String comments can optionally follow
                                 String... comments)
    {
        if (entityStatePdu != null)
            channelOpenDis7.sendSinglePdu(entityStatePdu);
            
        if (firePdu != null)
            channelOpenDis7.sendSinglePdu(firePdu); // bang
        
        channelOpenDis7.sendCommentPdu(commentType, comments); // empty comments are filtered
    }
    
    /**
     * Initial execution via main() method: handle args array of command-line initialization (CLI) arguments here
     * @param args command-line parameters: network address and port
     */
    protected void handleArgs (String[] args)
    {
        // initial execution: handle args array of initialization arguments here
        if (args.length == 2) 
        {
            if ((args[0] != null) && !args[0].isEmpty())
                thisProgram.channelOpenDis7.setNetworkAddress(args[0]);
            if ((args[1] != null) && !args[1].isEmpty())
                thisProgram.channelOpenDis7.setNetworkPort(Integer.parseInt(args[1]));
        }
        else if (args.length != 0) 
        {
            System.err.println("Usage: " + thisProgram.getClass().getSimpleName() + " [address port]");
            System.exit(-1);
        }
    }
    
    /** Locally instantiable copy of program, can be subclassed. */
    protected static ExampleSimulationProgram thisProgram;
  
    /**
     * Main method is first executed when a program instance is loaded.
     * @see <a href="https://docs.oracle.com/javase/tutorial/getStarted/application/index.html">Java Tutorials: A Closer Look at the "Hello World!" Application</a>
     * @param args command-line parameters: network address and port.
     *    Command-line arguments are an array of optional String parameters that are passed from execution environment during invocation
     */
    public static void main(String[] args)
    {
        thisProgram = new ExampleSimulationProgram(); // create instance of self
        
        thisProgram.channelOpenDis7.setTRACE_PREFIX("[" + ExampleSimulationProgram.class.getName() + "] ");
        
        thisProgram.channelOpenDis7.printlnTRACE("main() started...");
        
        thisProgram = new ExampleSimulationProgram(); // creates instance of self within static main() method
        
        thisProgram.handleArgs (args); // process command-line invocation arguments

        thisProgram.channelOpenDis7.setUpNetworkInterface();
        
//        thisProgram.pduRecorder.setDescriptor (TRACE_PREFIX.replace("[","").replace("]","") + " pduRecorder");

        thisProgram.runSimulationLoops(); // ... your simulation execution code goes in there ...
        
        thisProgram.channelOpenDis7.tearDownNetworkInterface(); // make sure no processes are left lingering
        
        thisProgram.channelOpenDis7.printlnTRACE("complete."); // report successful completion
        
        System.exit(0); // ensure all threads and sockets released
    }
}