import java.net.*;
import java.util.*;

import edu.nps.moves.disutil.*;
import edu.nps.moves.dis.*;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 * Receives PDUs from the network creates a TCP connection, then 
 * sends information to through the connection.
 *
 * @author Angelopoulos/Blankenbeker
 * @version 8 MAR 2018
 */
public class AngelBlankEspduReceiverAtoTCPTest {

    public static final String TCP_DESTINATION_IP = "172.20.146.111";
    public static final int DIS_DESTINATION_PORT = 3000;
    public static final int TCP_DESTINATION_PORT = 2999;
    public static final int MAX_PDU_SIZE = 8192;
    public static final String GROUP = "239.1.2.4";
    
    public static void main(String args[]) {
        MulticastSocket socket;
        DatagramSocket dataGram;
        DatagramPacket packet;
        InetAddress address;
        PduFactory pduFactory = new PduFactory();
        try {
            // Specify the socket to receive data
            socket = new MulticastSocket(DIS_DESTINATION_PORT);
            socket.setBroadcast(true);
       
            address = InetAddress.getByName(GROUP);
            socket.joinGroup(address);
            
            //System.out.println("Joined Group.");
            // Loop infinitely, receiving datagrams
            while (true) {
                try
                {

                    byte buffer[] = new byte[MAX_PDU_SIZE];
                    packet = new DatagramPacket(buffer, buffer.length);

                    socket.receive(packet);
                    //System.out.println("Packet Received");

                    List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData());
                    //System.out.println("Bundle size is " + pduBundle.size());

                    Iterator it = pduBundle.iterator();

                    while(it.hasNext())
                    {
                        Pdu aPdu = (Pdu)it.next();

                       // System.out.print("got PDU of type: " + aPdu.getClass().getName());
                        if(aPdu instanceof EntityStatePdu)
                        {

                            EntityID eid = ((EntityStatePdu)aPdu).getEntityID();
                            if (eid.getSite() == 100)
                            {
                                dataGram = new DatagramSocket();
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                DataOutputStream dos = new DataOutputStream(baos);  
                                aPdu.marshal(dos);
                                byte[] data = baos.toByteArray();
                                
                                DatagramPacket DpSend =  new DatagramPacket(data, data.length,  InetAddress.getByName(TCP_DESTINATION_IP), TCP_DESTINATION_PORT);
                                dataGram.send(DpSend);
                                //Socket clientConnection = new Socket(TCP_DESTINATION_IP, TCP_DESTINATION_PORT); 
                                //OutputStream os = clientConnection.getOutputStream();
                                //PrintStream ps = new PrintStream(os);
                                System.out.println("Alpha Bridging Complete to: " + TCP_DESTINATION_IP);
                                //EntityType entityType = ((EntityStatePdu)aPdu).getEntityType();
                                /**ps.println(eid.getSite());
                                ps.println(eid.getApplication());
                                ps.println(eid.getEntity());
                                ps.println(entityType.getEntityKind());      // Platform (vs lifeform, munition, sensor, etc.)
                                ps.println(entityType.getCountry());              // USA
                                ps.println(entityType.getDomain());          // Land (vs air, surface, subsurface, space)
                                ps.println(entityType.getCategory());        // Tank
                                ps.println(entityType.getSubcategory());     // M1 Abrams
                                ps.println(entityType.getSpec());            // M1A2 Abrams**/
 
                            }
                            Vector3Double location = ((EntityStatePdu)aPdu).getEntityLocation();
                            System.out.print(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
                            //System.out.print(" Location in DIS coordinates: [" + location.getX() + ", " + location.getY() + ", " + location.getZ() + "]");
                            double c[] = {location.getX(), location.getY(), location.getZ()};
                            double lla[] = CoordinateConversions.xyzToLatLonDegrees(c);
                            System.out.println(" Location (lat/lon/alt): [" + lla[0] + ", " + lla[1] + ", " + lla[2] + "]");
                        }
                        System.out.println();
                    } // end trop through PDU bundle
                }
                catch (Exception e) {
                    System.out.println(e);
                }
            } // end while
        } // End try
        catch (Exception e) {
            System.out.println(e);
        }
    } // end main
} // end class