/**
 * Copyright (c) 2008-2023, MOVES Institute, Naval Postgraduate School (NPS). All rights reserved.
 * This work is provided under a BSD open-source license, see project license.html or license.txt
 * @author brutzman@nps.edu for the original dis part
 * @author simonschnitzler for the simulation part
 */
package MV3500Cohort2024JulySeptember.homework3.Schnitzler;

import edu.nps.moves.dis7.pdus.*;
import edu.nps.moves.dis7.utilities.DisChannel;
import edu.nps.moves.dis7.utilities.PduFactory;
import java.awt.geom.Point2D;
import java.time.LocalDateTime;
import java.util.logging.Level;
import java.util.logging.Logger;
import simkit.Schedule;
import simkit.util.SimplePropertyDumper;

/** 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" target="_blank">ExampleSimulationProgramLog.txt</a>
 *  @see <a href="https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/blob/master/examples/src/OpenDis7Examples/ExampleSimulationProgramPduCaptureLog.dislog" target="_blank">ExampleSimulationProgramPduCaptureLog.dislog</a>
 *  @see <a href="https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/blob/master/examples/src/OpenDis7Examples/ExampleSimulationProgramFlowDiagram.pdf" target="_blank">ExampleSimulationProgramFlowDiagram.pdf</a>
 *  @see <a href="https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/blob/master/examples/src/OpenDis7Examples/ExampleSimulationProgramWireshark.png" target="_blank">ExampleSimulationProgramWireshark.png</a>
 *  @see <a href="https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/blob/master/examples/src/OpenDis7Examples/ExampleSimulationProgramSequenceDiagram.png" target="_blank">ExampleSimulationProgramSequenceDiagram.png</a>
 */
public class SchnitzlerSimulationProgram
{
    /* **************************** infrastructure code, modification is seldom needed ************************* */
                 
    private   String     descriptor = this.getClass().getSimpleName();
    /** DIS channel defined by network address/port combination includes multiple utility capabilities */
    protected DisChannel disChannel;
    /** Factory object used to create new PDU instances */
    protected PduFactory pduFactory;
    
    /** seconds per loop for real-time or simulation execution */
    private double  simulationTimeStepDuration  =  1.0; // seconds TODO encapsulate
    /** initial simulation time in seconds */
    double  simulationTimeInitial = 0.0;
    /** current simulation time in seconds */
    double  simulationTimeSeconds = simulationTimeInitial;
    /** Maximum number of simulation loops */
    int MAX_LOOP_COUNT = 20;
    
    String narrativeMessage1 = new String();
    String narrativeMessage2 = new String();
    String narrativeMessage3 = new String();
    
    /** 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;
    
    
    // hey programmer, what other state do you want?  this is a good place to declare it...
    
    /**
     * 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.
     */
    // base constructor is not invoked automatically by other constructors
    // https://stackoverflow.com/questions/581873/best-way-to-handle-multiple-constructors-in-java
    public SchnitzlerSimulationProgram()
    {
        initialize();
    }
    /**
     * Constructor to create an instance of this class.
     * @param newDescriptor describes this program, useful for logging and debugging
     */
    public SchnitzlerSimulationProgram(String newDescriptor)
    {
        descriptor = newDescriptor;
        initialize();
    }
    /**
     * 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 SchnitzlerSimulationProgram(String address, int port)
    {
        disChannel.setNetworkAddress            (address);
        disChannel.setNetworkPort               (port);
        disChannel.setVerboseComments           (true);       // TODO rename library method to disambiguate CommentPDU
                                                                            // TODO still seems really chatty... add silent mode?
        disChannel.setVerboseDisNetworkInterface(true);  // Default false
        disChannel.setVerbosePduRecorder        (true);       // default false
        initialize();
    }
    
    /** Initialize channel setup for OpenDis7 and report a test PDU
     * @see initializeDisChannel
     * @see initializeSimulationEntities */
    private void initialize()
    {
        initializeDisChannel(); // must come first, uses PduFactory
        
        //initializeSimulationEntities(); // set unchanging parameters
        
        disChannel.join(); // TODO further functionality expected
        
        String timeStepMessage = "Simulation timestep duration " + getSimulationTimeStepDuration() + " seconds";
        disChannel.sendCommentPdu(simulationTimeSeconds, DisChannel.COMMENTPDU_SIMULATION_TIMESTEP, timeStepMessage);
        // additional constructor initialization can go here
    }
    
    /** Initialize channel setup for OpenDis7 and report a test PDU */
    private void initializeDisChannel()
    {
        if (disChannel == null)
            disChannel = new DisChannel();
        else
        {
            disChannel.printlnTRACE ("*** warning, duplicate invocation of initializeDisChannel() ignored");
            return;
        }
        pduFactory     = disChannel.getPduFactory();
        disChannel.setDescriptor(this.getClass().getSimpleName()); // ExampleSimulationProgram might be a superclass
        disChannel.setUpNetworkInterface();
        disChannel.printlnTRACE ("just checking: disChannel.getNetworkAddress()=" + disChannel.getNetworkAddress() +
                                                         ", getNetworkPort()="    + disChannel.getNetworkPort());
        disChannel.getDisNetworkInterface().setVerbose(true); // sending and receipt
        disChannel.printlnTRACE ("just checking: hasVerboseSending()=" + disChannel.getDisNetworkInterface().hasVerboseSending() +
                                              ", hasVerboseReceipt()=" + disChannel.getDisNetworkInterface().hasVerboseReceipt());
        disChannel.getPduRecorder().setVerbose(true);

        // TODO confirm whether recorder is explicitly started by programmer (or not)
        
//      disChannel.sendCommentPdu(VariableRecordType.OTHER, "DisThreadedNetworkInterface.initializeDisChannel() complete"); // hello channel, debug
    }
    
    /** 
     * Set up the DES. A single SimpleMoverOpenDis7 starting at (0.0,0.0)
     * with a maximum speed of 30.0 and the following
     * waypoints:<br>(20.0,100.0),<br>(80.0,10.0),<br>(140.0,150.0)
     */
    private void initializeSimulation(){
        Point2D startingPoint = new Point2D.Double(0.0,0.0);
        double maxSpeed = 30.0;
        SimpleMoverOpenDis7 simpleMover = new SimpleMoverOpenDis7(startingPoint, maxSpeed, disChannel);
        simpleMover.setName("Fred");
        
        Point2D[] path = new Point2D[]{new Point2D.Double(20.0,100.0),
            new Point2D.Double(80.0,10.0),new Point2D.Double(140.0,150.0)};
        
        SimplePathMoverManagerOpenDis7 simplePathMoverManager = new SimplePathMoverManagerOpenDis7(path,true);
        
        simplePathMoverManager.addSimEventListener(simpleMover);
        simpleMover.addSimEventListener(simplePathMoverManager);
        
        SimplePropertyDumper simplePropertyDumper = new SimplePropertyDumper();
        simplePathMoverManager.addPropertyChangeListener(simplePropertyDumper);
        simpleMover.addPropertyChangeListener(simplePropertyDumper);
        simplePropertyDumper.setDumpSource(true);
        
        System.out.println(simpleMover);
        System.out.println(simplePathMoverManager);
        
        Schedule.setVerbose(true);
        Schedule.reset();
        Schedule.setPauseAfterEachEvent(true);
        
    }
                 
    /**
     * 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 might do that
    public void runSimulationLoops ()
    {
      try
      {              
          int     simulationLoopCount = 0;    
          boolean simulationComplete = false;     // sentinel variable as termination condition, are we done yet?
               
        // TODO reset Clock Time for today's date and timestamp to zero, providing consistent outputs for each simulation run
        String timeMessage = "Simulation time " + simulationTimeSeconds + " at LocalDateTime " + LocalDateTime.now();
        disChannel.sendCommentPdu(simulationTimeSeconds, DisChannel.COMMENTPDU_TIME, timeMessage);
        // TODO replace enumeration with disChannel.COMMENTPDU_TIME
        // TODO fix VariableRecordType.TIME_AMP_DATE_VALID

        // ===================================================================================================
        // loop the simulation while allowed, programmer can set additional conditions to break out and finish
        
        initializeSimulation();
        
        while (simulationLoopCount < 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! *****************************************************
            // =============================================================================================
            
            double simTime = Schedule.getSimTime();
            while(simTime < simulationLoopCount * getSimulationTimeStepDuration()){
                Schedule.startSimulation();
                simTime = Schedule.getSimTime();
            }
     
            
            // 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 >= MAX_LOOP_COUNT) // 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)(getSimulationTimeStepDuration() * 1000)); // units of seconds * (1000 msec/sec) = milliseconds
            System.out.println ("... [Pausing for " + getSimulationTimeStepDuration() + " seconds]");
            
            
            // ===============================
            // 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;
            }
            simulationTimeSeconds += getSimulationTimeStepDuration(); // good practice: increment simulationTime as lastst action in that loop
                    
        }   // end of simulation loop, continue until done
        // ===================================================================================================// ===================================================================================================// ===================================================================================================// ===================================================================================================

        narrativeMessage2 = "runSimulation() completed successfully"; // all done, so tell everyone else on the channel
        // TODO better javadoc needs to be autogenerated for VariableRecordType enumerations
        disChannel.sendCommentPdu(DisChannel.COMMENTPDU_NARRATIVE, narrativeMessage1, narrativeMessage2, narrativeMessage3);
        System.out.println ("... [final=completion CommentPdu successfully sent for simulation]");
        
//        disChannel.getPduRecorder(). TODO record XML as well
        disChannel.leave(); // embedded SimulationManager is expected to send appropriate PDUs for entity, application shutdown
      } 
      catch (InterruptedException iex) // handle any exception that your code might choose to provoke!
      {
        Logger.getLogger(SchnitzlerSimulationProgram.class.getSimpleName()).log(Level.SEVERE, null, iex);
      }
        
    }
    
    /**
     * 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 handleArguments (String[] args)
    {
        // initial execution: handle args array of initialization arguments here
        if (args.length == 2) 
        {
            if ((args[0] != null) && !args[0].isEmpty())
                thisProgram.disChannel.setNetworkAddress(args[0]);
            if ((args[1] != null) && !args[1].isEmpty())
                thisProgram.disChannel.setNetworkPort(Integer.parseInt(args[1]));
        }
        else if (args.length != 0) 
        {
            System.err.println("Usage: " + thisProgram.getClass().getSimpleName() + " [address port]");
            System.exit(-1);
        }
    }

    /**
     * Get simple descriptor (such as parent class name) for this network interface, used in trace statements
     * @return simple descriptor name
     */
    public String getDescriptor() {
        return descriptor;
    }

    /**
     * Set new simple descriptor (such as parent class name) for this network interface, used in trace statements
     * @param newDescriptor simple descriptor name for this interface
     */
    public void setDescriptor(String newDescriptor) {
        if (newDescriptor == null)
            newDescriptor = "";
        this.descriptor = newDescriptor;
    }

    /**
     * parameter accessor method
     * @return the simulationTimeStepDuration in seconds
     */
    public double getSimulationTimeStepDuration() {
        return simulationTimeStepDuration;
    }

    /**
     * parameter accessor method
     * @param timeStepDurationSeconds the simulationTimeStepDuration in seconds to set
     */
    public void setSimulationTimeStepDuration(double timeStepDurationSeconds) {
        this.simulationTimeStepDuration = timeStepDurationSeconds;
    }
    
    /** Locally instantiable copy of program, can be subclassed. */
    protected static SchnitzlerSimulationProgram 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" target="_blank">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 SchnitzlerSimulationProgram("test constructor"); // create instance of self within static main() method
        
        thisProgram.disChannel.printlnTRACE("main() started...");
        
        thisProgram.handleArguments(args); // process any command-line invocation arguments

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